Powershell无法识别布尔参数
我有下面的PS脚本Powershell无法识别布尔参数,powershell,invalid-argument,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
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的新手,我在这里迷路了。有人能解释一下问题出在哪里吗?好吧,我找到了问题所在。 改变: -PasswordNeverExpires$true` 到 -PasswordNeverExpires$true`
(在true之后添加空格)Hmm,是否可以在
$true
中转义美元符号?另外,请使用(获取凭据)
作为获取域管理员用户和密码的手段,如果可能,不要将其作为命令行参数传递。如果是这样,可以添加一行$PNE=$true
,并将参数列表中的$PNE
添加到脚本块中?这样,您将把一个布尔值作为一个参数放入其中,并且在类型转换方面不应该有问题。对我来说,奇怪的是,它不会在启用了-Enabled$true
的情况下爆炸,而是在启用了-PasswordNeverExpires$true
的情况下爆炸。同样,重新排列顺序会改变错误吗?比如说-PNE不是这行的最后一个参数吗?亲爱的,我一开始是在建议这个技巧,但后来又想了别的,编辑了评论。遗憾:)