【问题标题】:ConvertTo-Secure String ErrorConvertTo-安全字符串错误
【发布时间】:2017-03-31 02:55:59
【问题描述】:

我在创建用户脚本中收到以下错误。

ConvertTo-SecureString:无法将参数绑定到参数“String”,因为它是一个空字符串。 在 C:\AD_Scripts\psscripts\user_create.ps1:59 char:54 + -AccountPassword (convertto-securestring "$Password" -AsPlainText -F ... + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [ConvertTo-SecureString], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

# Import active directory module for running AD cmdlets
Import-Module activedirectory

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv userimport.csv
#Store report in log file in the $log variable
$log = "log.txt"

#Loop through each row containing user details in the CSV file 

foreach ($User in $ADUsers)
{
 #Read user data from each field in each row and assign the data to a  variable as below

$Username   = $User.ID
$Password   = $User.BDATE
$Firstname  = $User.FNAME
$Lastname   = $User.LNAME
$Department = $User.GRD
$Company    = $User.SCHID #This field refers to the OU the user account is to be moved to

# Choose OU
Switch ($Company)
{
    "1480" {$OU = 'OU=students,OU=users,ou=hs,dc=clasd,dc=net'}
    "1479" {$OU = 'OU=students,OU=users,ou=elem,dc=clasd,dc=net'}
    "1480" {$Folder = '\\hs-ss\students\hs'}
    "1479" {$Folder = '\\hs-ss\students\elem'}
}

#Check to see if the user already exists in AD
if (Get-ADUser -LDAPFilter {$Username=$user.$SamAccountName})
{
     #If user does exist, give a warning
     Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
    #User does not exist then proceed to create the new user account
    "Processing started (on " + $date + "): " | Out-File $log -append
    "--------------------------------------------" | Out-File $log -append

    #Account will be created in the OU provided by the $OU variable read from the CSV file
    New-ADUser `
        -SamAccountName $Username `
        -UserPrincipalName "$Username@clasd.net" `
        -Name "$Firstname $Lastname" `
        -GivenName $Firstname `
        -Department "$Department" `
        -Company "$Company" `
        -EmailAddress "$Username@clasd.net" `
        -Surname $Lastname `
        -Enabled $True `
        -Scriptpath "login.vbs" `
        -DisplayName "$Firstname $Lastname" `
        -Path $OU `
        -Homedrive "Z" `
        -homedirectory "$Folder\$username" `
        -AccountPassword (convertto-securestring "$Password" -AsPlainText -Force) `
        -ChangePasswordAtLogon $true   

}

}

在更改此行之前,我从未收到错误

if (Get-ADUser -F {SamAccountName -eq $Username})

if (Get-ADUser -LDAPFilter {$Username=$user.$SamAccountName})

我正在导入的 cvs 文件如下所示:

"ID","FNAME","LNAME","BDATE","GRD","SCHID" "111111","测试","student1","20001225","2016","1480" "333333","test","Student3","2001225","2025","1479"

我使用 Bdate 作为用户密码

【问题讨论】:

  • 建议调试。将 $Username=$user.$SamAccountName 分配移到 if 上方,您可以验证 ISE 中的值。此外,对 New-ADUser 的参数使用 splatting,然后您可以在 ISE 中设置断点并检查 $Password 和/或在其为空白时放入带有断点的 if。然后,您还可以将 convertto-securestring 调用放在参数的争吵之外,并分配给另一个变量,如 $securePassword 等。
  • 因为我是 Powershell 的新手,所以我得看看你在说什么:-)。你第二个说要使用 splatting 的人

标签: powershell active-directory


【解决方案1】:

所以这里有两个问题,首先你从不声明 $Password 这解释了你得到的错误,因为你将 null 值传递给 convertto-securestring,你还想在变量周围去掉引号,他们不会破坏任何东西,但它们不符合惯例。所以改变

-AccountPassword (convertto-securestring "$Password" -AsPlainText -Force)

-AccountPassword (convertto-securestring $User.BDATE -AsPlainText -Force)

您还应该查看您的 If 语句,该语句旨在防止您的脚本尝试创建已存在的用户,您的 LDAP 过滤器 {$Username=$user.$SamAccountName} 将始终返回 false,因为它不是有效格式,您真的应该比较运算符的两边都没有变量,并且 $user.$SamAccountName 在您的脚本中不存在。并不是说这些真的很重要,因为如果用户已经存在,new-aduser 无论如何都会自己出错。

【讨论】:

  • 那么您甚至需要脚本先查看用户是否存在?如果不是,那么我可以删除整个 if else 语句。
  • @JustinMerwin,有时请求原谅比请求许可更好。您可以通过什么都不做(或给出错误)来查看它是否抛出错误(Try)并处理它(Catch)。
  • 所以它现在(假设您修复了 if 语句以正常工作)如果用户存在,您将收到有关他们存在的警告并且脚本将继续。正如尼克建议的那样,您可以使用 try catch 来完成它,并且您将获得几乎相同的效果,并具有处理任何错误的额外好处,而不仅仅是处理已经存在的用户。哦,如果您保留 if 语句,您可以删除 LDAP 过滤器并将其替换为 get-aduser -identity $Username
  • 所以我想我什至不需要查看用户是否存在。只需运行脚本。
  • 正确,但如果你没有捕捉到错误,你的循环会在遇到错误时停止,除非你改变你的 $ErrorActionPreference
猜你喜欢
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多