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

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

How do you get PowerShell to ignore blank CSV values?

问题

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

$employees = Import-Csv -Path .\ADFieldsTest.csv
foreach ($User in $employees)
{
    $Username = $User.username
    $Department = $User.department
    $Displayname = $User.displayname
    $Fax = $User.fax
    $Manager = $User.manager
    $Mobilephone = $User.mobilephone
    $Officephone = $User.officephone
    $Title = $User.title
    $server = $User.server

    Set-ADUser -Identity $username -Department $department -DisplayName $displayname -Fax $fax -Manager $manager -MobilePhone $mobilephone -Title $title -Server $server
}

当脚本处理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:

$employees = Import-Csv -Path .\ADFieldsTest.csv
foreach ($User in $employees)
{
    $Username = $User.username
    $Department = $User.department
    $Displayname = $User.displayname
    $Fax = $User.fax
    $Manager = $User.manager
    $Mobilephone = $User.mobilephone
    $Officephone = $User.officephone
    $Title = $User.title
    $server = $User.server

    Set-ADUser -Identity $username -Department $department -DisplayName $displayname -Fax $fax -Manager $manager -MobilePhone $mobilephone -Title $title -Server $server
}

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 条件,并将逻辑替换为内部循环。

foreach($User in Import-Csv -Path .\ADFieldsTest.csv) {
    if(-not [string]::IsNullOrWhiteSpace($User.username)) {
        # 如果没有用户名,那么必须跳过此行
        continue
    }
    
    $setSplat = @{}
    foreach($prop in $User.PSObject.Properties) {
        if([string]::IsNullOrWhiteSpace($prop.Value)) {
            # 如果值为空字符串,则跳过
            continue
        }
        $setSplat[$prop.Name] = $prop.Value
    }

    Set-ADUser @setSplat
}
英文:

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.

foreach($User in Import-Csv -Path .\ADFieldsTest.csv) {
    if(-not [string]::IsNullOrWhiteSpace($User.username)) {
        # if there is no username then this line has to be skipped
        continue
    }
    
    $setSplat = @{}
    foreach($prop in $User.PSObject.Properties) {
        if([string]::IsNullOrWhiteSpace($prop.Value)) {
            # if the value is empty string then skip
            continue
        }
        $setSplat[$prop.Name] = $prop.Value
    }

    Set-ADUser @setSplat
}

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:

确定