此功能應在Windows Server 2003和2008 R2上運行 使用命令行逐行執行它是SUCCESSFULL!腳本執行失敗。將用戶添加到本地組
function addUser2Group([string]$user,[string]$group)
{
$cname = gc env:computername
$objUser = [ADSI]("WinNT://$user")
$objGroup = [ADSI]("WinNT://$cname/$group,group")
$members = $objGroup.PSBase.Invoke('Members')
$found = $false
foreach($m in $members)
{
if($m.GetType().InvokeMember('Name', 'GetProperty', $null, $m, $null) -eq $user)
{
$found = $true
}
}
if(-not $found)
{
$objGroup.PSBase.Invoke('Add',$objUser.PSBase.Path)
}
$members = $objGroup.PSBase.Invoke('Members')
$found = $false
foreach($m in $members)
{
if($m.GetType().InvokeMember('Name', 'GetProperty', $null, $m, $null) -eq $user)
{
$found = $true
}
}
return $found
}
addUser2Group('MyGlobalMonitoringUser',"SomeDBGroup")
它應該將用戶添加到本地組。但它只是給了我以下錯誤:
Exception calling "Invoke" with "2" argument(s): "Unknown error (0x80005000)"
+ $members = @($objGroup.PSBase.Invoke <<<< ("Members"))
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
編輯:隨着出現錯誤消息/加是
The following exception occurred while retrieving member "Add": "Unknown error (0x80005000)"
代碼是:
function addUser2Group([string]$user,[string]$group)
{
$cname = gc env:computername
try
{
([adsi]"WinNT://$cname/$group,group").Add("WinNT://$cname/$user,user")
}
catch
{
write2log($_)
return $false
}
return $true
}
您是否正在使用管理員權限運行腳本? –
我使用管理員權限從CMD.exe運行它(因爲我總是必須取消限制和重新限制PowerShell腳本) –
你是如何從cmd.exe調用它的?你使用的是什麼確切的命令行? – Goyuix