该成员不存在? Powershell脚本

时间:2018-02-09 14:19:38

标签: powershell adsi

我一直在阅读一些问题,可以找到有关创建脚本的信息,但似乎没有使用对象类型“计算机”。

如果这更适合超级用户,请道歉。但这仍然是脚本级别的,因此我认为它最好放在这里。

这是我的脚本。我想将域注册的服务器(计算机)添加到一系列服务器上的性能监视器用户组。

$ComputerName = Read-Host "Remote Computer name:"
$PmuGroup = [ADSI]"WinNT://$ComputerName/Performance Monitor Users,group"
$User = [ADSI]"WinNT://DOMAIN/ServerName,computer"
$PmuGroup.Add($User.Path) 

显示以下错误:

Exception calling "Add" with "1" argument(s): "A member could not be added to 
or removed from the local group because
the member does not exist.
"
At line:1 char:1
+ $AdminGroup.Add($User.Path)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

如果我在PS中输入$user,它会返回Path并且看似找到机器。

在评论中建议没有.path尝试;

PS C:\WINDOWS> $PmuGroup.Add($User)
Exception calling "Add" with "1" argument(s): "Type mismatch. (Exception 
from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))"
At line:1 char:1
+ $PmuGroup.Add($User)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

这是我的脚本错误/任何人都可以建议任何错误吗? 有关信息:Domain / ServerName不是正在使用的。我从这里删除了实际名称。

1 个答案:

答案 0 :(得分:1)

计算机帐户的samAccountName始终以$

结尾
$ComputerName = Read-Host "Remote Computer name:"
$PmuGroup = [ADSI]"WinNT://$ComputerName/Performance Monitor Users,group"
$Computer = [ADSI]"WinNT://DOMAIN/ServerName$"
$PmuGroup.Add($Computer.Path)