0

I created 1.ps1 script which calls 2.ps1 script. After calling 2.ps1 it give some result in $variable. I want this $variable result to be used in my 1.ps1 for manipulation.

$csv = Get-Content \\10.46.198.141\try\windowserver.csv
foreach ($servername in $csv) {
    $TARGET = $servername
    $ProfileName = "CustomPowershell"
    $SCRIPT = "powershell.exe -ExecutionPolicy Bypass -File '\\10.46.198.141\try\disk_space.ps1' '$servername'"
    $HubRobotListPath = "C:\Users\Automation\Desktop\hubrobots.txt"
    $UserName = "aaaaa"
    $Password = "aaaaaaa"
    $Domain = "SW02111_domain"
    $HubOne = "sw02111"
    #lots of code here
}

Now I have a second script which is:

Param([string]$servername)

$hash = New-Object PSObject -Property @{
    Servername = "";
    UsedSpace = "";
    DeviceID = "";
    Size = "";
    FreeSpace = ""
}

$final =@()
$hashes =@()

$hash = New-Object PSObject -Property @{
    Servername = $servername;
    UsedSpace = "";
    DeviceID = "";
    Size = "";
    FreeSpace = ""
}

$hashes += $hash
$space = Get-WmiObject Win32_LogicalDisk

foreach ($drive in $space) {
    $a = $drive.DeviceID
    $b = [System.Math]::Round($drive.Size/1GB) 
    $c = [System.Math]::Round($drive.FreeSpace/1GB) 
    $d = [System.Math]::Round(($drive.Size - $drive.FreeSpace)/1GB) 
    $hash = New-Object PSObject -Property @{
        Servername = "";
        UsedSpace = $d;
        DeviceID = $a;
        Size = $b;
        FreeSpace = $c
    }
    $hashes += $hash
}

$final += $hashes

return $final

I want to use this $final output to create a CSV file with code in the first PowerShell script:

$final | Export-Csv C:\Users\Automation\Desktop\disk_space.csv -Force -NoType
1
  • Get-WmiObject -Class Win32_logicaldisk -ComputerName $servername Commented Mar 28, 2017 at 7:26

1 Answer 1

1

Don't make things more complicated than they need to be. Use the pipeline and calculated properties.

Get-Content serverlist.txt |
    ForEach-Object { Get-WmiObject Win32_LogicalDisk -Computer $_ } |
    Select-Object PSComputerName, DeviceID,
        @{n='Size';e={[Math]::Round($_.Size/1GB)}},
        @{n='FreeSpace';e={[Math]::Round($_.FreeSpace/1GB)}},
        @{n='UsedSpace';e={[Math]::Round(($_.Size - $_.FreeSpace)/1GB)}} |
    Export-Csv disksize.csv -Force -NoType
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.