【问题标题】:Add content to csv with Add-Member使用 Add-Member 将内容添加到 csv
【发布时间】:2018-01-26 11:36:03
【问题描述】:

我从 Active Directory 导出了一个 csv,如下所示:

姓名,“GivenName”,“姓氏”,“部门”,“登录工作站” jdoe,"John","Doe","财务","pc1" fbueller,"Ferris","Bueller","Sales","pc2"

现在我想在每个工作站上搜索,检查是否存在文件夹并将该信息写回 csv。

$input = Import-Csv "C:\Scripts\input.csv"

ForEach ($line in $input) {
    $wst = $line.LogonWorkstations
    If ($wst -ne "") {
        If (Test-Connection $wst -Count 1 -Quiet -ErrorAction SilentlyContinue) {
            If (Test-Path "\\$wst\c$\Program Files\xyz" -ErrorAction SilentlyContinue) {
                Add-Member -InputObject $line -NotePropertyName "xyz" -NotePropertyValue "exists"
            }
        }
    }
}

$input | Export-Csv "C:\Scripts\output.csv" -NoTypeInformation -Encoding Unicode

正如您在代码中看到的,Add-Member 将附加信息添加到$input,但是将其导出到 csv 后该信息不可用。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    它有效。问题在于 If 条件。

    $input = Import-Csv "C:\Scripts\input.csv"
    
    ForEach ($line in $input) {
        Add-Member -InputObject $line -NotePropertyName "xyz" -NotePropertyValue "exists"
    }
    
    $input # here I get the information
    
    $input | Export-Csv "C:\Scripts\output.csv" -NoTypeInformation -Encoding Unicode 
    

    我建议将 Add-Member 调用作为循环中的第一个命令,并使用默认值(可能为空),然后在条件中设置该值。

    ForEach ($line in $input) 
    {
        Add-Member -InputObject $line -NotePropertyName "xyz" -NotePropertyValue $null
        $wst = $line.LogonWorkstations
    
        If ($wst -ne "") 
        {
    
            If (Test-Connection $wst -Count 1 -Quiet -ErrorAction SilentlyContinue) 
            {
                If (Test-Path "\\$wst\c$\Program Files\xyz" -ErrorAction SilentlyContinue) 
                {
                    $line.xyz = 'exists'
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您将新成员添加到变量$line 并期望它被写回$input .. 事实并非如此。

      试试这个:

      $input = Import-Csv "C:\Scripts\input.csv"
      
      $counter=0
      
      ForEach ($line in $input) {
          $wst = $line.LogonWorkstations
          If ($wst -ne "") {
              If (-not ( Test-Connection $wst -Count 1 -Quiet -ErrorAction SilentlyContinue)) {
                  If (-not (Test-Path "\\$wst\c$\Program Files\xyz" -ErrorAction SilentlyContinue)) {
                      Add-Member -InputObject $input[$counter] -NotePropertyName "xyz" -NotePropertyValue "exists"
                  }
              }
          }
          $counter++
      }
      
      $input # here I get the information
      
      $input | Export-Csv "C:\Scripts\output.csv" -NoTypeInformation -Encoding Unicode
      

      我更改了InputObject-参数以将新成员写入input 变量。

      【讨论】:

        【解决方案3】:

        我建议使用calculated property 进行这种修改:

        Import-Csv "C:\Scripts\input.csv" |
            Select-Object *, @{n='xyz';e={
                $wst = $_.LogonWorkstations
                if ($wst) {
                    if (Test-Connection $wst -Count 1 -Quiet -EA SilentlyContinue) {
                        $path = "\\$wst\c$\Program Files\xyz"
                        [bool](Test-Path $path -EA SilentlyContinue)
                    }
                }
            }} |
            Export-Csv 'C:\Scripts\output.csv' -NoType -Encoding Unicode
        

        【讨论】:

          猜你喜欢
          • 2023-03-10
          • 1970-01-01
          • 2017-07-10
          • 2021-09-06
          • 1970-01-01
          • 2017-05-21
          • 1970-01-01
          • 1970-01-01
          • 2019-03-22
          相关资源
          最近更新 更多