我有以下脚本。
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
Get-ChildItem从pipe道等待path ,而不是计算机名称。 首先你需要从CSV文件中获取计算机对象的属性(名称,types,描述):
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。