我有以下脚本。
Get-Content comps.csv | Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse | Get-ItemProperty -name Version,Release -EA 0 | Where { $_.PSChildName -match '^(?!S)\p{L}'} | Select PSChildName, Version, Release
我的 comps.csv 内容
Name,Type,Description, ALYTAUS-PC,Computer,, AUGUSTE-PC,Computer,, AUSRA-PC,Computer,, BIRZU-PC,Computer,, VYTAUTO-PC1,Computer,,
我收到了 csv 中每个对象的消息:
Get-ChildItem : The input object cannot be bound to any parameters for the command either because the command does n ot take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:38 + Get-Content comps.csv | Get-ChildItem <<<< 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse | + CategoryInfo : InvalidArgument: (VYTAUTO-PC1,Computer,,:PSObject) [Get-ChildItem], ParameterBindingE xception + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetChildItemCommand
答案1
Get-ChildItem 等待小路来自管道,而不是计算机名称。您需要做的第一件事是从 CSV 文件中获取计算机对象及其属性(名称、类型、描述):
Get-Content -Path "c:\temp\servers.csv" | ConvertFrom-Csv | ForEach-Object -Process {
Write-Host "Server name: " -NoNewline
Write-Host $_.Name
}
接下来需要使用 Invoke-Command 远程执行命令:
Invoke-Command -ComputerName $_.Name -ScriptBlock {
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP'
}
最后:
Get-Content -Path "c:\temp\servers.csv" | ConvertFrom-Csv | ForEach-Object -Process {
Invoke-Command -ComputerName $_.Name -ScriptBlock {
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP'
}
}
要使用指定的凭据运行脚本块,请在 Invoke-Command 中使用参数 -Credential。