2016-09-26 41 views
0

的,我想將列添加到我創建的腳本包含.csv文件的開頭,「服務器名稱」有$ servername而在foreach循環中的價值。添加列開始CSV

這樣,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) { 

回答

1

請嘗試下面的修改腳本。我已經刪除了$serverCsv = $serverNames |這條管道,因爲管道似乎沒有管道到任何東西。

我已經添加了這個| Select @{Name="ServerName"; Expression={ $serverName }}, *您的CSV列表和出口之間。這引入了一個新的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) { 
+0

但這在該.csv的開頭添加請求的列,但它還在末尾添加了一堆不必要的列。我怎樣才能擁有ServerName列?感謝你的快速回復! – LilithGoddess

+0

這將是導致這種情況的'*'。我不確定'$ xmldata.DeviceConfig.ChildNodes'中有什麼。而不是'*',添加特定的列 – TechSpud

+0

Nevermind--想通了! :)謝謝 – LilithGoddess

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可以理解