gpt4 book ai didi

powershell - 从.csv运行Powershell脚本时出错

转载 作者:行者123 更新时间:2023-12-03 01:12:13 25 4
gpt4 key购买 nike

我正在尝试使用.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
}

我收到下面的错误,不知道这意味着什么

一元运算符“-”后缺少表达式。
在C:\ Scripts \ CreateNewUser.psl:7 char:3
+-<<<<姓$ _。姓
+ CategoryInfo:ParserError:(-:String)[],ParseException
+ FullyQualifiedErrorId:MissingExpressionAfterOperator

最佳答案

您必须将所有参数都写到一行:

New-Mailbox -Name $_.Name -FirstName $_.FirstName -LastName $_.LastName -Alias $_.Alias -UserPrincipalName $_.UserPrincipalName -Password $password -ResetPasswordOnNextLogon $false 

或者,您可以使用 splatting(感谢 Frode F.):
$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

关于powershell - 从.csv运行Powershell脚本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37268343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com