将列添加到CSV的开头

时间:2016-09-26 16:27:35

标签: xml powershell csv

我想在我在附带脚本中创建的.csv文件的开头添加一列,用于foreach循环中值为$ serverName的“服务器名称”。

这样,CSV将包含“服务器名称”,服务器名称位于每行的开头,然后将显示从xml文件中提取的信息。

最好的方法是什么?谢谢!

$localPath = "C:\Temp\MPOS"

$serverList = (Get-ADComputer -Filter "Name -like 'Q0*00*'" -SearchBase "OU=MPOS,OU=Prod,OU=POS,DC=company,DC=NET").name | Sort-Object | Out-File C:\Temp\MPOS\MPOSServers.txt

$serverNames = Get-Content $serverList

foreach ($serverName in $serverNames) {

    Add-Content $logfile "Processing $serverName" 

    $serverCsv = $serverNames | 

        #Check if the server is online before doing the remote command
        If (Test-Connection -ComputerName $serverName -Quiet -count 1) {

            #copy config file from MPOS print to local server for processing
            $configPath = "\\$($serverName)\D$\mposdevices\deviceconfig.xml"
            Copy-Item $configPath $localPath 

            #process xml file to output data to csv file
            $xml = Get-Content C:\Temp\MPOS\DeviceConfig.xml
            $xmldata = [xml]$xml
            $xmldata.DeviceConfig.ChildNodes | Export-Csv -Path C:\Temp\MPOS\MDATInfo.csv -NoTypeInformation -Append

        } #If (Test-Connection -ComputerName $serverName -Quiet -count 1) {

} #foreach ($serverName in $serverNames) {

2 个答案:

答案 0 :(得分:1)

尝试下面修改过的脚本。我删除了$serverCsv = $serverNames |行,因为管道似乎没有任何东西。

我在你的csv列表和导出之间添加了这个| Select @{Name="ServerName"; Expression={ $serverName }}, *。这引入了一个新的ServerName变量,用于保存循环中的当前$serverName*会从您的csv列表中显示其余字段。

$localPath = "C:\Temp\MPOS"

$serverList = (Get-ADComputer -Filter "Name -like 'Q0*00*'" -SearchBase "OU=MPOS,OU=Prod,OU=POS,DC=company,DC=NET").name | Sort-Object | Out-File C:\Temp\MPOS\MPOSServers.txt

$serverNames = Get-Content $serverList

foreach ($serverName in $serverNames) {

    Add-Content $logfile "Processing $serverName"

        #Check if the server is online before doing the remote command
        If (Test-Connection -ComputerName $serverName -Quiet -count 1) {

            #copy config file from MPOS print to local server for processing
            $configPath = "\\$($serverName)\D$\mposdevices\deviceconfig.xml"
            Copy-Item $configPath $localPath 

            #process xml file to output data to csv file
            $xml = Get-Content C:\Temp\MPOS\DeviceConfig.xml
            $xmldata = [xml]$xml
            $xmldata.DeviceConfig.ChildNodes | Select @{Name="ServerName"; Expression={ $serverName }}, * | Export-Csv -Path C:\Temp\MPOS\MDATInfo.csv -NoTypeInformation -Append

        } #If (Test-Connection -ComputerName $serverName -Quiet -count 1) {

} #foreach ($serverName in $serverNames) {

答案 1 :(得分:0)

另一种方法是保持在管道内。

你想做什么:

  • 在AD中找到计算机
  • 对它们进行排序
  • 为每个人生成输出对象(名称和值)
  • 将这些导出为CSV

看起来像这样:

Push-Location "C:\Temp\MPOS"

Get-ADComputer -Filter "Name -like 'Q0*00*'" | Sort-Object Name | ForEach-Object {
    $serverName = $_.Name
    Add-Content MDATInfo.log "Processing $serverName"
    Write-Host "Processing $serverName"

    $line = @{
        serverName = $_.Name
        foo = ""
        bar = ""
        baz = ""
    }

    if (Test-Connection -ComputerName $_.Name -Quiet -Count 1) {
        $deviceConfigXml = [xml](Get-Content "\\$serverName\D$\mposdevices\deviceconfig.xml")
        $deviceConfig = $DeviceConfigXml.DeviceConfig
        $line.foo = $deviceConfig.foo
        $line.bar = $deviceConfig.bar
        $line.baz = $deviceConfig.baz
    }

    New-Object –TypeName PSObject –Prop $line
} | Export-Csv MDATInfo.csv -NoTypeInformation

Pop-Location

注意:

  • Push-LocationPop-Location允许您使用脚本其余部分中的相对路径
  • 无需将服务器名称写入文件,您可以直接使用服务器对象
  • 无需从服务器复制XML文件,您可以直接从远程位置打开它
  • 该脚本确保输出对象始终是统一的 - 无论XML内容或服务器可用性如何,相同数量的属性
  • For-Each脚本块中的最后一个值放在输出管道
  • 关键是New-Object –TypeName PSObject –Prop $line 它会生成PSObject,这是Export-Csv可以理解的对象