大家好,我不确定为什么我的代码在从远程桌面用户添加用户时无法正常工作,但它在管理员组下完美运行,下面是我的代码
$serversList ="C:\Users\Administrator\Desktop\test.csv"
$servers= Import-Csv -Path $serversList `
-Delimiter "," `
-Header ComputerNM,UserGroup,Domain
foreach($Computer in $servers)
{
$Status=$null
$Comment = $null
$ComputerName= $Computer.ComputerNM
$Usrgroup = $Computer.UserGroup #"Here we get username"
$ObjName = $Computer.UserGroup
$Domain = $Computer.Domain
$count++
if(Test-Connection -ComputerName $ComputerName -Count 1 -Quiet)
{
Write-Output "$ComputerName : Online"
try {
#$Domain = $env:userdomain
$GroupObj = [ADSI]('WinNT://'+$ComputerName+'/"Remote Desktop users",group')
$GroupObj.psbase.Invoke('Add',([ADSI]"WinNT://$Domain/$ObjName").path)
$Status = "Success"
$Comment = "Added $ObjName $ObjectType to Local administrators group on [$ComputerName]"
Write-Output "Successfully added $ObjName $ObjectType to $ComputerName"
}
catch {
$Status = "Failed"
$Comment = $ObjectType + "[$ObjName] - " + $_.toString().replace("`n","").replace("`r","")
Write-Output "Failed to add $ObjName $ObjectType to $ComputerName"
}
$ResultsFile = "C:\Users\Administrator\Desktop\t1.txt"
Add-Content -Path $ResultsFile -Value ("{0},{1},{2}" -f $ComputerName,$Status,$Comment )
}
}
这是我尝试在管理员和远程桌面用户上手动添加它的情况,正如您在此处看到的相同情况,如果我在远程桌面用户处添加用户,我会遇到错误“使用“2”个参数调用“Invoke”时出现异常:“指定的参数数量与预期数量不匹配”
C:\Windows\system32>powershell -c "Add-LocalGroupMember -Group "Administrators" -Member "domain\account""
C:\Windows\system32>powershell -c "Add-LocalGroupMember -Group "Remote Desktop Users" -Member "domain\account""
Add-LocalGroupMember : A positional parameter cannot be found that accepts argument 'Desktop'.
At line:1 char:1
+ Add-LocalGroupMember -Group Remote Desktop Users -Member domain\account ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-LocalGroupMember], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand