我正在尝试使用.csv文件中包含的信息运行PowerShell脚本。
我的脚本如下:
Import-CSV \\chem-fp01\shared areas\IT\New IT Folder (Do Not Delete \\Powershell Scripts\Create New User.csv | ForEach-Object {
$Password = ConvertTo-SecureString $_.password -AsPlainText -Force
New-Mailbox -Name $_.Name
-FirstName $_.FirstName
-LastName $_.LastName
-Alias $_.Alias
-UserPrincipalName $_.UserPrincipalName
-Password $password
-ResetPasswordOnNextLogon $false
}
我收到以下错误,不知道这意味着什么
Missing expression after unary operator '-'. At C:\Scripts\CreateNewUser.psl:7 char:3 + - <<<<LastName $_.LastName + CategoryInfo : ParserError: (-:String) [], ParseException + FullyQualifiedErrorId : MissingExpressionAfterOperator
答案 0 :(得分:3)
您必须将所有参数写入一行:
New-Mailbox -Name $_.Name -FirstName $_.FirstName -LastName $_.LastName -Alias $_.Alias -UserPrincipalName $_.UserPrincipalName -Password $password -ResetPasswordOnNextLogon $false
$parameters = @{
Name = $_.Name
FirstName = $_.FirstName
LastName = $_.LastName
Alias = $_.Alias
UserPrincipalName = $_.UserPrincipalName
Password = $password
ResetPasswordOnNextLogon = $false
}
New-Mailbox @parameters
另一个解决方案是使用行尾的`字符(不推荐):
New-Mailbox -Name $_.Name `
-FirstName $_.FirstName `
-LastName $_.LastName `
-Alias $_.Alias `
-UserPrincipalName $_.UserPrincipalName `
-Password $password `
-ResetPasswordOnNextLogon $false
答案 1 :(得分:0)
此命令失败的原因是,您的CSV文件路径包含空格,并且没有用双引号或单引号引起来。
只要您的路径中包含空格,就可以在路径的开头和结尾添加引号,如下所示:
Import-CSV "\\chem-fp01\shared areas\IT\New IT Folder (Do Not Delete \\Powershell Scripts\Create New User.csv"