【发布时间】:2016-09-26 16:27:35
【问题描述】:
我想在包含的脚本中创建的 .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) {
【问题讨论】:
标签: xml powershell csv