【发布时间】: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