如何让PowerShell忽略空白的CSV数值?

huangapple go评论67阅读模式
英文:

How do you get PowerShell to ignore blank CSV values?

问题

我正在编写一个脚本,该脚本应该根据CSV文件更新Active Directory中的每个属性。到目前为止,我的代码如下:

  1. $employees = Import-Csv -Path .\ADFieldsTest.csv
  2. foreach ($User in $employees)
  3. {
  4. $Username = $User.username
  5. $Department = $User.department
  6. $Displayname = $User.displayname
  7. $Fax = $User.fax
  8. $Manager = $User.manager
  9. $Mobilephone = $User.mobilephone
  10. $Officephone = $User.officephone
  11. $Title = $User.title
  12. $server = $User.server
  13. Set-ADUser -Identity $username -Department $department -DisplayName $displayname -Fax $fax -Manager $manager -MobilePhone $mobilephone -Title $title -Server $server
  14. }

当脚本处理CSV中的空单元格时,它会出现问题。
有没有办法让PowerShell传递一个“null”值或类似的方式以使其继续运行?

我还尝试了在脚本中使用像!null值和-nq ''之类的方法,但仍然在处理空值时出现错误。

英文:

I'm writing a script that is supposed to update each attribute in Active Directory based on a CSV file. This is what I have so far:

  1. $employees = Import-Csv -Path .\ADFieldsTest.csv
  2. foreach ($User in $employees)
  3. {
  4. $Username = $User.username
  5. $Department = $User.department
  6. $Displayname = $User.displayname
  7. $Fax = $User.fax
  8. $Manager = $User.manager
  9. $Mobilephone = $User.mobilephone
  10. $Officephone = $User.officephone
  11. $Title = $User.title
  12. $server = $User.server
  13. Set-ADUser -Identity $username -Department $department -DisplayName $displayname -Fax $fax -Manager $manager -MobilePhone $mobilephone -Title $title -Server $server
  14. }

It kind of breaks when it reaches a cell that is blank in the CSV.
Is there a way to get PowerShell to just pass a "null" value or something so it keeps running?

I also tried running this with like !null values and like -nq '' but it still fails when it hits the the blank values

答案1

得分: 1

没有一种方法可以告诉 Import-Csv 不要为空值创建属性,验证必须在循环中完成。您可以使用 String.IsNullOrWhiteSpace(String) 方法 进行验证并跳过空值,然后创建一个 splatting hashtable 以与 Set-ADUser 一起使用。

此外,由于您的 CSV 列似乎具有与 Set-ADUser 参数完全相同的名称,您可以避免执行多个 if 条件,并将逻辑替换为内部循环。

  1. foreach($User in Import-Csv -Path .\ADFieldsTest.csv) {
  2. if(-not [string]::IsNullOrWhiteSpace($User.username)) {
  3. # 如果没有用户名,那么必须跳过此行
  4. continue
  5. }
  6. $setSplat = @{}
  7. foreach($prop in $User.PSObject.Properties) {
  8. if([string]::IsNullOrWhiteSpace($prop.Value)) {
  9. # 如果值为空字符串,则跳过
  10. continue
  11. }
  12. $setSplat[$prop.Name] = $prop.Value
  13. }
  14. Set-ADUser @setSplat
  15. }
英文:

There isn't a way of telling Import-Csv to not create properties for empty values, the validation has to be done in your loop. You can use String.IsNullOrWhiteSpace(String) Method to validate and skip empty values and create a splatting hashtable to use with Set-ADUser.

Also, since your CSV columns seem to have the exact same names as the Set-ADUser parameters, you could avoid doing multiple if conditions and replace the logic with an inner loop.

  1. foreach($User in Import-Csv -Path .\ADFieldsTest.csv) {
  2. if(-not [string]::IsNullOrWhiteSpace($User.username)) {
  3. # if there is no username then this line has to be skipped
  4. continue
  5. }
  6. $setSplat = @{}
  7. foreach($prop in $User.PSObject.Properties) {
  8. if([string]::IsNullOrWhiteSpace($prop.Value)) {
  9. # if the value is empty string then skip
  10. continue
  11. }
  12. $setSplat[$prop.Name] = $prop.Value
  13. }
  14. Set-ADUser @setSplat
  15. }

huangapple
  • 本文由 发表于 2023年3月1日 11:13:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定