【问题标题】:Why does this return -2为什么返回 -2
【发布时间】:2020-04-04 03:10:43
【问题描述】:

我正在尝试按照教程进行操作,但我不明白为什么会返回 -2。该脚本工作并完成了它应该做的所有事情,但控制台输出-2。想不通为什么。有什么想法吗?

没有错误会打印到控制台。只是-2。

param($FirstName,$MiddleInitial,$LastName,$Location = 'OU=Corporate Users',$Title)

$DefaultPassword = 'p@$$word12'
$DomainDn = (Get-ADDomain).DistinguishedName
$DefaultGroup = 'Gigantic Corporation Inter-Intra Synergy Group'

### ------------------------------------------------------------------------------------------------
### Figure out what the username should be
###

$Username = "$($FirstName.SubString(0,1))$LastName"

$EaPrefBefore = $ErrorActionPreference 
$ErrorActionPreference = 'SilentlyContinue'

    if (Get-ADUser $Username) {
        $Username = "$($FirstName.SubString(0,1))$MiddleInitial$LastName"

        if (Get-ADUser $Username) {
            Write-Warning "No acceptable username schema could be created"
            return
        }
    }


### ----------------------------------------------------------------------------------------------
### Create the user account 
###

$ErrorActionPreference - $EaPrefBefore
$NewUserParams = @{
    'UserPrincipalName' = $Username
    'Name' = $Username
    'GivenName' = $FirstName
    'Surname' = $LastName
    'Title' = $Title
    'SamAccountName' = $Username
    'AccountPassword' = (ConvertTo-SecureString $DefaultPassword -AsPlainText -Force)
    'Enabled' = $true
    'Initials' = $MiddleInitial
    'Path' = "$Location,$DomainDn"
    'ChangePasswordAtLogon' = $true
}

New-ADUser @NewUserParams

### ----------------------------------------------------------------------------------------------
### Add the user account to the company standard group
###

Add-ADGroupMember -Identity $DefaultGroup -Members $Username

就像我说的那样,它可以工作并且做它应该做的任何事情。创建新用户并将用户发送到组,所有规则都可以正常工作。但它返回 -2。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    当 PowerShell 中的表达式返回值且未分配给变量时,它会打印到控制台。看起来这是来自您的线路
    $ErrorActionPreference - $EaPrefBefore,它只是减去两个代码并且对结果没有任何作用。我认为您的意思是在这里使用等号 (=) 而不是减号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2013-02-01
      • 2012-08-04
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多