Powershell 无法识别布尔参数

Mar*_*lov 3 powershell invalid-argument

我有以下 PS 脚本

param (
    # FQDN or IP address of the Domain Controller
    [Parameter(Mandatory=$True)]
    [string]$ADaddress,

    # Active directory domain name
    # example: directory.local
    [Parameter(Mandatory=$True)]
    [string]$ADDomainName,

    # Domain admin
    # example: administrator@directory.local
    [Parameter(Mandatory=$True)]
    [string]$domainAdmin,

    # Domain admin password
    [Parameter(Mandatory=$True)]
    [string]$domainAdminPassword,

    # User to be added
    # example: testUser
    [Parameter (Mandatory=$True)]
    [string]$newUsername,

    # Password of th user to be added
    # example: 1!2#4%6
    [Parameter (Mandatory=$True)]
    [string]$newPassword,

    # SAM account name of the user to added
    # example: testuser
    [Parameter (Mandatory=$True)]
    [string]$newSamAccountName,

    # Display name of the user to added
    # example: "Test user for test purposes"
    [Parameter (Mandatory=$True)]
    [string]$newUserDisplayName
)

$domainAdminSecurePassword = $domainAdminPassword | ConvertTo-SecureString -asPlainText -Force
$domainAdminCredential = New-Object System.Management.Automation.PSCredential($domainAdmin, $domainAdminSecurePassword)
$newUserSecurePassword = $newPassword | ConvertTo-SecureString -asPlainText -Force
$UPN= $newUsername+"@"+$ADDomainName


Invoke-Command -ComputerName $ADaddress -Credential $domainAdminCredential `
    -ScriptBlock {`
        param($newUsername, $newUserSecurePassword, $newSamAccountName, $newUserDisplayName, $UPN) `
        new-aduser -name $newUsername -AccountPassword $newUserSecurePassword -Enabled $true -SamAccountName $newSamAccountName -DisplayName $newUserDisplayName -UserPrincipalName $UPN -PasswordNeverExpires $true`
    } `
    -ArgumentList $newUsername, $newUserSecurePassword, $newSamAccountName, $newUserDisplayName, $UPN
Run Code Online (Sandbox Code Playgroud)

调用此脚本时遇到的问题是:

Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]' required by parameter 'PasswordNeverExpires'.
Run Code Online (Sandbox Code Playgroud)

我尝试传递 1,传递 [bool]$true 但结果保持不变。我是 PS 新手,我在这里迷路了。任何人都可以阐明可能存在的问题吗?

Mar*_*lov 5

好吧,我找到问题所在了。更改:-PasswordNeverExpires $true`

-PasswordNeverExpires $true `(在 true 后添加一个空格)