输入中:我有一个\ users \ myself \ desktop \ test \ file.xslx,包含像这样的倍数列:
ColumnA ColumnB ... ColumnQ(总共17列)
每列都有一些数据。
在输出中:
我想要一个这样的cmd:
New-ADUser -Name $(columnAdata) -GivenName "$(columnBdata)" -Surname "$(columnCdata)" -DisplayName "$(columnDdata)" -SamAccountName "$(columnEdata)" ... etc until -blabla "$(ColumnQdata)"
是否可以将de columndata存储在变量中以将它们插入命令中?
非常感谢。
我建议首先将列标题更改为与打算与New-ADUser
cmdlet一起使用的参数相同。具有匹配的标题将大大有助于避免出错。
下一步,将您的Excel文件另存为CSV,假设文件名为NewUsers.csv
然后,代码可以非常简单且易于维护:
# import the CSV file using the same separator character as Excel uses on your system
Import-Csv -Path 'X:\NewUsers.csv' -UseCulture | ForEach-Object {
# use Splatting: create a Hashtable with all properties needed taken from the CSV
# see: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting
$userProperties = @{
Name = $_.Name # as opposed to $_.columnAdata
GivenName = $_.GivenName # as opposed to $_.columnBdata
Surname = $_.Surname
DisplayName = $_.DisplayName
SamAccountName = $_.SamAccountName
# etcetera
}
New-ADUser @userProperties
}