我创建了调用1.ps1
脚本的2.ps1
脚本。致电2.ps1
后,会在$variable
中提供一些结果。我希望在我的$variable
中使用此1.ps1
结果进行操作。
$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
}
现在我有了第二个脚本:
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
我想使用此$final
输出在第一个PowerShell脚本中创建包含代码的CSV文件:
$final | Export-Csv C:\Users\Automation\Desktop\disk_space.csv -Force -NoType
答案 0 :(得分: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