我创建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
不要让事情变得比需要的更复杂。使用管道和计算属性 https://technet.microsoft.com/en-us/library/ff730948.aspx.
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)