2017-03-28 8 views
0

2.ps1スクリプトを呼び出す1.ps1スクリプトを作成しました。 2.ps1を呼び出した後、$variableで何らかの結果が得られます。この$variableの結果を私の1.ps1の操作で使用します。2番目のpowershellスクリプトから1番目のPowerShellスクリプトに変数値を返しますか?

$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 
} 

今私は2番目のスクリプトがあります:私は最初のPowerShellスクリプトのコードでCSVファイルを作成するには、この$final出力を使用したい

Param([string]$servername) 

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

$final [email protected]() 
$hashes [email protected]() 

$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 

$final | Export-Csv C:\Users\Automation\Desktop\disk_space.csv -Force -NoType 
+0

'Get-WmiObject -Class Win32_logicaldisk -ComputerName $ servername' – JosefZ

答えて

1

を必要以上に複雑なものにしないでください。パイプラインと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 
関連する問題