我有一個簡單的單行腳本,它針對計算機串列運行,如果檔案包含某些文本,則回傳檔案的路徑。這行得通。
但是,我確實需要在數百臺計算機上運行它,并且當我使用 Invoke-command 運行它時,我不知道哪臺計算機回傳了路徑……這是我需要你幫助的地方。
這是最簡單形式的命令
invoke-command -computername $computers -erroraction silentlycontinue -scriptblock {get-childitem 'C:\' -rec -force -filter *.txt -ea silentlycontinue | foreach {select-string "<lookup.text>" $_} | select -exp Path}
輸出以這種形式出現:
c:\path\filename
但是沒有跡象表明該路徑來自哪個服務器,我知道我看到來自多臺計算機的多個路徑只是來自我為早期測驗而洗掉的檔案。
是否可以修改-scriptblock以獲取計算機名稱。
uj5u.com熱心網友回復:
這應該可以為您提供所需的內容,有一些注意事項。
- 如果您不在
-ExpandProperty腳本塊上使用,則結果Invoke-Command應該是附加object[]了屬性的PSComputerName。如果您希望使用特定的屬性名稱,例如本例中的"ComputerName",除了使用switch之外,您還可以使用計算屬性-HideComputerName。 Select-String可以直接用管道輸送Get-ChildItem,沒有必要使用ForEach-Object。
Invoke-Command -ComputerName $computers -ScriptBlock {
Get-ChildItem 'C:\' -Recurse -Force -Filter *.txt -EA SilentlyContinue |
Select-String "<lookup.text>" |
Select-Object Path, @{
Name = 'ComputerName'
Expression = { $env:ComputerName }
}
} -HideComputerName -ErrorAction SilentlyContinue
uj5u.com熱心網友回復:
Pscomputername 通常由帶有物件的呼叫命令輸出。只需在最后取出“-exp”即可。
# elevated
invoke-command localhost,localhost,localhost { dir -r ..\foo *.txt -force -ea 0 |
select-string hi | select path }
Path PSComputerName RunspaceId
---- -------------- ----------
C:\Users\admin\foo\file.txt localhost b7b6ca8b-f35e-41fc-ad98-ff2b6a51ae44
C:\Users\admin\foo\file.txt localhost 2454784b-b56b-4e90-92fb-49b31dcc1745
C:\Users\admin\foo\file.txt localhost 49e61d03-a68c-4308-a823-246cb3698f6a
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406810.html
標籤:
