我有一段代碼可以洗掉遠程機器上所有用戶組態檔的 Google Chrome 快取。
為了實作這一點,我有函式 GetMachineUserProfiles,它回傳遠程機器上所有用戶組態檔的 ArrayList。在其他函式中,我需要運行 Invoke-Command 并回圈遍歷 $ListOfUserProfiles 給出的所有用戶組態檔,并洗掉每個組態檔的 Chrome 快取。
但是我遇到了一個問題,我的 Invoke-Command 中的 $ListOfUserProfiles 為空/空。我嘗試了幾種解決方案,但每次都失敗了。我的最后一次嘗試顯示在示例中:
$ListOfUserProfiles = GetMachineUserProfiles
$ListOfUserProfiles.count
Function Delete-Chrome-Temp-Files {
WriteLog "--------------------------------`n"
WriteLog "COMMAND: Delete Chrome temporary files"
$diskSpaceBeforeC = Disk-Free-Space
$ListOfUserProfiles.count
Invoke-Command -ComputerName $machine -ArgumentList (, $ListOfUserProfiles) -ScriptBlock {
$ListOfUserProfiles.count
foreach ($UserProfile in $ListOfUserProfiles){
Write-Host $UserProfile
Get-ChildItem -Path "C:\Users\"$UserProfile"\AppData\Local\Google\Chrome\User Data" -Filter "*.tmp" | foreach {
Remove-Item -Path $_.FullName
WriteLog "INFO: Deleting $($_.FullName)"
}
}
}
Delete-Chrome-Temp-Files
我的機器上有 6 個組態檔,你可以看到我在這里使用了 3 次計數方法,它們回傳:
6
6
0(我希望這里有 6 個)
uj5u.com熱心網友回復:
該變數$ListOfUserProfiles僅存在于您的本地范圍內 - 當您$ListOfUserProfiles作為 的一部分傳遞時-ArgumentList,PowerShell 會將變數的值傳遞給遠程會話,但它不會重新創建變數本身。
為此,請取消參考相應的$args專案:
Invoke-Command -ComputerName $machine -ArgumentList (, $ListOfUserProfiles) -ScriptBlock {
$ListOfUserProfiles = $args[0]
# ... rest of scripblock as before
}
...或將其宣告為位置引數并讓 PowerShell 為您系結該值:
Invoke-Command -ComputerName $machine -ArgumentList (, $ListOfUserProfiles) -ScriptBlock {
param([System.Collections.ArrayList]$ListOfUserProfiles)
# ... rest of scripblock as before
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331894.html
