【问题标题】:Add time in output file在输出文件中添加时间
【发布时间】:2019-10-03 15:41:17
【问题描述】:

此脚本通过 PowerShell 将域用户添加到任何其他或远程域计算机/系统的“管理员”组。

这会在 csv 中返回包含三列(计算机名称、可用性、状态)的最终状态

我需要在包含时间和日期的输出文件中添加第四列。

#Create a file in the required path and update in the below command line
$Output = "C:\CSV\Output.csv" 
#The output field of the computer will blank if the user is already exist in the group
Add-Content -Path $Output -Value "ComputerName,Availability,Status"
$status = $null
$availability = $null
#Save the CSV (Comma seperated) file with the server host name and the username to be added
Import-Csv C:\CSV\Computer.csv | ForEach-Object {
    $Computer=$_.Computer
    $User=$_.user
    if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
        Write-Verbose "$Computer : Online"
        $availability="Oniline"
        try {
            $GroupObj=[ADSI]"WinNT://$Computer/Administrators,group"
            $GroupObj.PSBase.Invoke("Add",([ADSI]"WinNT://jdom.edu/$User").Path)
            $status="Success"
            #Update the status in the output file
            Add-Content -Path $Output -Value ("{0},{1},{2}" -f $Computer, $availability, $status)
        } catch {
            Write-Verbose "Failed"
        }
    } else {
        Write-Warning "$Computer : Offline"
        $availability = "Offline"
        $status = "failed"
        #Update the status in the output file
        Add-Content -Path $Output -Value ("{0},{1},{2}" -f $Computer, $availability, $status)
    }
}

这是输出文件的样子,这是我想在第四列添加日期和时间的地方:

计算机名称、可用性、状态 TD123696WJN339P,在线,成功 TD123419WJN339P,在线,成功 计算机名称、可用性、状态 5VERF9097LTIO01,离线,失败 ZF001024DJH706G,离线,失败 5MICF9017LTIO01,离线,失败

【问题讨论】:

    标签: powershell


    【解决方案1】:

    简单的方法是在输出中添加另一个字段,即

    Add-Content -Path $Output -Value "ComputerName,Availability,Status,Timestamp"
    

    "{0},{1},{2},{3}" -f $Computer, $availability, $status, (Get-Date)
    

    但是,除非您确实希望在输出文件中有多个标题行(为什么?),否则您应该使用 calculated propertiesExport-Csv

    Import-Csv 'input.csv' |
        Select-Object Computer, User, @{n='Status';e={
            if (Test-Connection -ComputerName $_.Computer -Count 1 -Quiet) {
                ...
            } else {
                ...
            }
        }}, @{n='Timestamp';e={Get-Date}} |
        Export-Csv 'output.csv' -NoType
    

    【讨论】:

      【解决方案2】:

      这是一种非常有趣的方法,你在那里使用 CSV 并且它使场景有点过于复杂(从我的角度来看,没有不尊重!)。

      为什么不尝试使用 PowerShell 自定义对象?

      #Create a file in the required path and update in the below command line
      $Output = "C:\CSV\Output.csv" 
      #The output field of the computer will blank if the user is already exist in the group
      Add-Content -Path $Output -Value "ComputerName,Availability,Status"
      $status = $null
      $availability = $null
      #Save the CSV (Comma seperated) file with the server host name and the username to be added
      $result = Import-Csv C:\CSV\Computer.csv | ForEach-Object {
          $Computer=$_.Computer
          $User=$_.user
          if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
              Write-Verbose "$Computer : Online"
              $availability="Oniline"
              try {
                  $GroupObj=[ADSI]"WinNT://$Computer/Administrators,group"
                  $GroupObj.PSBase.Invoke("Add",([ADSI]"WinNT://jdom.edu/$User").Path)
                  $status="Success"
                  #Update the status in the output file
                  [PSCustomObject]@{
                      Computer = $Computer
                      Availability = $availability
                      Status = $status
                      Date = Get-Date
                  }
              } catch {
                  Write-Verbose "Failed"
              }
          } else {
              Write-Warning "$Computer : Offline"
              $availability = "Offline"
              $status = "failed"
              #Update the status in the output file
              [PSCustomObject]@{
                  Computer = $Computer
                  Availability = $availability
                  Status = $status
                  Date = Get-Date
              }
          }
      }
      
      $result | Export-Csv -Path $Output -NoTypeInformation
      

      这样,您会将结果存储到$result 变量中,并且可以将其导出为 CSV,而不会产生任何复杂性。 使用 PowerShell 自定义对象是存储来自不同来源的数据并以您希望的方式提供输出的好方法。

      如果您愿意,请尝试并提供反馈:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-12
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        • 2016-12-27
        • 2015-04-23
        • 1970-01-01
        • 2014-02-23
        相关资源
        最近更新 更多