需要在 Invoke-command 的 return 值中包含计算机名称,以便我知道输出来自哪台计算机
Need to include the computer name in return values from Invoke-command so I know which computer the output is from
我有一个简单的单行脚本,它 运行 针对计算机列表和 returns 文件路径(如果文件包含特定文本)。这行得通。
但是,我需要 运行 针对数百台计算机进行此操作,当我使用 Invoke 命令 运行 它时,我不知道哪台计算机返回了路径...这里这是我需要你帮助的地方。
这是最简单形式的命令
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
获取计算机名
这应该能满足您的需求,但有一些注意事项。
- 如果您不在脚本块上使用
-ExpandProperty
,Invoke-Command
的结果应该是具有 属性 PSComputerName
的 object[]
附加到它。如果您希望使用特定的 属性 名称,例如 "ComputerName",除了使用 [=] 之外,您还可以使用 calculated property 15=]开关。
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
pscomputername 通常由带有对象的 invoke-command 输出。把最后的“-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
我有一个简单的单行脚本,它 运行 针对计算机列表和 returns 文件路径(如果文件包含特定文本)。这行得通。
但是,我需要 运行 针对数百台计算机进行此操作,当我使用 Invoke 命令 运行 它时,我不知道哪台计算机返回了路径...这里这是我需要你帮助的地方。
这是最简单形式的命令
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
获取计算机名
这应该能满足您的需求,但有一些注意事项。
- 如果您不在脚本块上使用
-ExpandProperty
,Invoke-Command
的结果应该是具有 属性PSComputerName
的object[]
附加到它。如果您希望使用特定的 属性 名称,例如 "ComputerName",除了使用 [=] 之外,您还可以使用 calculated property 15=]开关。 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
pscomputername 通常由带有对象的 invoke-command 输出。把最后的“-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