![添加文件路径时出现 Powershell 错误](https://linux22.com/image/604601/%E6%B7%BB%E5%8A%A0%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84%E6%97%B6%E5%87%BA%E7%8E%B0%20Powershell%20%E9%94%99%E8%AF%AF.png)
脚本如下:
$searchOU='ou=Servers,dc=mydomain,dc=NET'
Get-ADComputer -filter * -SearchBase $searchOU |
Foreach-Object {
$server = $_.Name
([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke('Members') |
ForEach-Object {
$user = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
New-Object 'PSObject' -property @{'Server'=$server; 'Admin'=$user} | Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
}
}
如果我之后删除此部分New-Object
...
| Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
...脚本运行完美。添加上述行时,我收到此错误,它找到的所有服务器/成员均出现此错误:
使用“2”个参数调用“Invoke”时发生异常:“未找到网络路径。”位于 C:\scripts\myscript.ps1:5 char:62 + ([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke <<<< ('Members') | + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
答案1
一个解决方法:格式和输出需要遵循外部的管道Foreach-Object
:
$searchOU='ou=Servers,dc=mydomain,dc=NET'
Get-ADComputer -filter * -SearchBase $searchOU |
Foreach-Object {
$server = $_.Name
([ADSI]"WinNT://$($_.Name)/Administrators").psbase.invoke('Members') |
ForEach-Object {
$user = $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null)
New-Object 'PSObject' -property @{'Server'=$server; 'Admin'=$user}
}
} | Format-Table -AutoSize Server, Name | Out-File C:\Scripts\servers.txt
第二:错误来自ADSIInvoke
调用,可能无法解析机器名。