我想在我在附带脚本中创建的.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) {
答案 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)
另一种方法是保持在管道内。
你想做什么:
看起来像这样:
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-Location
和Pop-Location
允许您使用脚本其余部分中的相对路径For-Each
脚本块中的最后一个值放在输出管道New-Object –TypeName PSObject –Prop $line
它会生成PSObject
,这是Export-Csv
可以理解的对象