为什么此PowerShell脚本无法找到本地用户或组?

时间:2016-01-19 21:42:14

标签: windows security powershell scripting

我从在线提取this脚本声称可以将整个用户组转换为本地管理员,这似乎完全符合我的目标(将所有标准用户转变为本地管理员)。但是,它会出现以下错误。

  

异常通话"添加"用" 1"参数:"由于该成员不存在,因此无法将成员添加到本地组或从中删除。"

我按照网站上给出的例子,尝试了"用户"用于参数的组,以及单独定位我按名称制作的标准用户配置文件,名为" Test01"。在这两种情况下,总是抛出相同的错误。我已经在系统控制台中的“组”管理单元下进行了检查,并且帐户和组肯定都拼写正确。这是为方便起见的脚本,以及我尝试在顶部执行它的两种不同方式。我将计算机名称保留,因为如果未指定,脚本会成功找到它。

PS C:\> Set-LocalAdminGroupMembers.ps1 -ObjectType User -ObjectName "WORKGROUP\Test01"

PS C:\> Set-LocalAdminGroupMembers.ps1 -ObjectType Group -ObjectName "WORKGROUP\Users"

[CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,Position=1)]
    [ValidateSet("User","Group")]
    [String]
    $ObjectType,
    [Parameter(Mandatory=$true,Position=2)]
    [ValidateScript({($_.split("\").count -eq 2)})]
    [string]$ObjectName,
    [Parameter(Position=3)]
    [String[]]$ComputerName=$env:COMPUTERNAME
    )

$ResultsFile = "c:\temp\ResultsofLocalGroupAddition.csv"
$ObjDomain = $ObjectName.Split("\")[0]
$ObjName = $ObjectName.Split("\")[1]
$ComputerCount = $ComputerName.Count
$count = 0
Add-Content -Path $ResultsFile -Value "ComputerName,Status,Comments"
foreach($Computer in $ComputerName) {
    $count++
    $Status=$null
    $Comment = $null
    Write-Host ("{0}. Working on {1}" -f $Count, $Computer)
    if(Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
        Write-Verbose "$Computer : Online"
        try {
            $GroupObj = [ADSI]"WinNT://$Computer/Administrators"
            $GroupObj.Add("WinNT://$ObjDomain/$ObjName")
            $Status = "Success"
            $Comment = "Added $ObjectName $ObjectType to Local administrators group"
            Write-Verbose "Successfully added $ObjectName $ObjectType to $Computer"
        } catch {
            $Status = "Failed"
            $Comment = $_.toString().replace("`n","").replace("`r","")
            Write-Verbose "Failed to add $ObjectName $ObjectType to $Computer"
        }

        Add-Content -Path $ResultsFile -Value ("{0},{1},{2}" -f $Computer,$Status,$Comment )    

    } else {
        Write-Warning "$Computer : Offline"
        Add-Content -Path $ResultsFile -Value ("{0},{1}" -f $Computer,"Offline")
    }

}

0 个答案:

没有答案