的,我想將列添加到我創建的腳本包含.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) {
但這在該.csv的開頭添加請求的列,但它還在末尾添加了一堆不必要的列。我怎樣才能擁有ServerName列?感謝你的快速回復! – LilithGoddess
這將是導致這種情況的'*'。我不確定'$ xmldata.DeviceConfig.ChildNodes'中有什麼。而不是'*',添加特定的列 – TechSpud
Nevermind--想通了! :)謝謝 – LilithGoddess