【发布时间】:2021-01-14 16:28:31
【问题描述】:
我想使用下面的脚本对 500 个 VDI 执行磁盘清理,并使用下面的脚本创建一个包含 VDI 状态和磁盘空间详细信息的正确报告。但是我想在所有 VDI 上并行运行清理,而不是像在每个循环..因此我尝试了 foreach -parallel 但它不起作用。请建议我如何修改此脚本以在所有 VDI 上并行执行清理
$InputFile="H:\Ignio\Lastlogon\vdi list 3.csv"
$AllVDI = Get-Content $InputFile
$collection = @()
$file = "C:\report.csv"
$Starters = (Get-Date)
ForEach-Object -parallel ($VDI in $AllVDI){
if((Test-Connection -ComputerName $VDI -count 1 -ErrorAction 0)) {
$Isonline = "Online"
if((Test-WSMan -ComputerName $VDI -ErrorAction 0)){
$Remoting = "Enabled"
Invoke-Command -ComputerName $VDI -Credential $cred -ScriptBlock {
Stop-Service -Name wuauserv -Force -Confirm:$false -ErrorAction Ignore -WarningAction Ignore
Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force -ErrorAction Ignore
Remove-Item -Path "C:\Windows\SoftwareDistribution\download\*" -Recurse -Force -ErrorAction Ignore
Start-Service -Name wuauserv -Confirm:$false -ErrorAction Ignore }
$Space = Invoke-Command -ComputerName $VDI -Credential $cred -ScriptBlock {Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"}|Select-Object SystemName,
@{ Name = "Drive" ; Expression = { ( $_.DeviceID ) } },
@{ Name = "Size (GB)" ; Expression = {"{0:N1}" -f ( $_.Size / 1gb)}},
@{ Name = "FreeSpace (GB)" ; Expression = {"{0:N1}" -f ( $_.Freespace / 1gb ) } },
$free = $Space.'FreeSpace (GB)'
$Size = $Space.'Size (GB)'
$Drive=$Space.Drive
$Action=" Yes"
}
else {$Remoting = "Disabled";}
}
else {$Isonline = "Offline";}
$object = [PSCustomObject]@{
'VDI' = $VDI
'Status'="$Isonline"
'WinRM'="$Remoting"
'CleanUp Performed'=$Action
'Drive'=$Drive
'Size (GB)'=$Size
'FreeSpace (GB)'= $free
}
$collection += $object
}
【问题讨论】:
-
请比“它不工作”更具描述性——它只运行在某些设备上吗?它完全失败了吗?电脑着火了?有任何错误信息吗?如果是这样,他们怎么说?请注意,只有您可以看到您的屏幕:-)
标签: powershell powershell-remoting