【问题标题】:Powershell not recognizing boolean argumentPowershell 无法识别布尔参数
【发布时间】:2015-06-08 06:58:54
【问题描述】:

我有以下 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

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

Cannot convert 'System.String' to the type 'System.Nullable`1[System.Boolean]' required by parameter 'PasswordNeverExpires'.

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

【问题讨论】:

  • 嗯,也许可以避开$true 中的美元符号?另外,请使用(Get-Credential) 作为获取域管理员用户和密码的方法,如果可能,不要将这些作为命令行参数传递。
  • 如果是这样,也许添加一行$PNE=$true 并在参数列表中添加$PNE 到您的脚本块?这样您就可以将布尔值作为参数放入其中,并且不会出现类型转换问题。
  • 又出现了错误。对我来说奇怪的是它不会在-Enabled $true 上爆炸,而是在同一行和同一命令的一部分的-PasswordNeverExpires $true 上爆炸
  • 另外,重新排列顺序会改变错误吗?就像 -PNE 不是该行的最后一个参数一样?

标签: powershell invalid-argument


【解决方案1】:

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

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

【讨论】:

  • 亲爱的,我一开始是在建议这个技巧,但后来又不以为然并编辑了评论。可惜:)
【解决方案2】:

用变量替换 $true 为我做了。 所以这个:

$command = 'New-CMApplicationDeployment -Name $Name -CollectionName $Col -OverrideServiceWindow $true  -Comment $Com -AvailableDateTime $Adt -DeployAction Install -DeployPurpose Available -UserNotification DisplaySoftwareCenterOnly'
Invoke-Expression -Command "& $command"

成为:

$t = $true
$command = 'New-CMApplicationDeployment -Name $Name -CollectionName $Col -OverrideServiceWindow $t  -Comment $Com -AvailableDateTime $Adt -DeployAction Install -DeployPurpose Available -UserNotification DisplaySoftwareCenterOnly'
Invoke-Expression -Command "& $command"

它很愚蠢,但它有效。

【讨论】:

    猜你喜欢
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-08
    • 2022-10-19
    • 2019-08-07
    相关资源
    最近更新 更多