請你能幫幫我嗎?我缺少一些資訊。
為什么當我執行代碼時:
Invoke-Command -ComputerName domainpc -ScriptBlock {Get-ChildItem -path C:\Users -Filter "username1"}
結果是:username1
當我執行這個腳本時:
$user = Read-Host "Please enter username"
Invoke-Command -ComputerName domainpc -ScriptBlock {Get-ChildItem -path C:\Users -Filter "$user"}
結果是包含在 C:\Users 中的檔案夾串列
我不明白為什么執行腳本得到的結果與執行沒有變數的代碼不同。似乎問題出在變數上。請你能解釋一下嗎?
謝謝。
uj5u.com熱心網友回復:
您遇到了范圍“問題”。腳本塊內
的變數未知。
看看這里:https : //docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-7.1#using-local-variables$user-ScriptBlock
使用$Using:user得到解決。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345209.html
標籤:电源外壳
