I am trying to create a script for my job and I need to read a xml file to check if there are any duplicated parameter. So far I can output the table but I haven't been able to export as any kind of file (log, csv, etc)
The idea is to run the script and get this information for many remote servers.
This is my current progress.
$SectionName = 'np'
$path = "S:\Docker\NP6\Volumes\smartupdate\config\SmartUpdateLocal.xml"
[xml] $xml = Get-Content $path
$param = $xml.SelectSingleNode("//Section[@name='$SectionName']")
$table = $param.Parameter
$table
And the table can be seen:
But since I will be running this script for thousand of servers I would like to export something like:
---------Server A------------
Name Value
ena.. true
base.. 0
sto.. 22592
ena.. EAI
---------Server B------------
Name Value
ena.. true
base.. 0
sto.. 22592
ena.. EAI
The XML format is
<SmartUpdate>
<Settings>
<Section name="np">
<Parameter name="enableStatusFileCreation" value="true" />
<Parameter name="baseStatusCode" value="0" />
<Parameter name="storeId" value="43001" />
<Parameter name="enableTransportMechanismAndPackage" value="true"/>
<Parameter name="statusTransportMechanism" value="EAI" />
</Section>
</Settings>
</SmartUpdate>
Also I am looping the remote servers
$computerList = Get-Content $DeviceListFilePath
foreach($computer in $computerList)
{
$SectionName = 'np'
Write-Host "OPENING XML FILE" -ForegroundColor Yellow
$path = "\\$server_ip\$FileName"
[xml] $xml = Get-Content $path
$param = $xml.SelectSingleNode("//Section[@name='$SectionName']")
Thank you for the help.
This is what I would recommend you to do to have an output you can export to CSV and have it very easy to sort and filter:
$SectionName = 'np'
$result = foreach($computer in Get-Content $DeviceListFilePath) {
Write-Host "OPENING XML FILE" -ForegroundColor Yellow
# Unclear where `$server_ip` and `$FileName` are coming from,
# assuming they're valid values to construct the UNC Path
# or that you meant: `\\$computer\$FileName`
$path = "\\$server_ip\$FileName"
$xml = [xml]::new()
$xml.Load($path)
# This Dictionary is used to construct the output.
$out = [ordered]@{
Server = $computer
}
$xml.SelectSingleNode("//Section[@name='$SectionName']").Parameter |
ForEach-Object { $out[$_.name] = $_.Value }
[pscustomobject] $out
}
$result | Export-Csv path\to\export.csv -NoTypeInformation
Since I don't have access to your UNC Paths, I would show how the output would look like using the XML presented in question with 2 servers:
$SectionName = 'np'
$result = foreach($computer in 'server A', 'server B') {
# the example XML is stored in my current directory
$path = '.\file.xml'
$xml = [xml]::new()
$xml.Load($path)
$out = [ordered]@{
Server = $computer
}
$xml.SelectSingleNode("//Section[@name='$SectionName']").Parameter |
ForEach-Object { $out[$_.name] = $_.Value }
[pscustomobject] $out
}
$result
The output from this example would look like this:
Server : server A
enableStatusFileCreation : true
baseStatusCode : 0
storeId : 43001
enableTransportMechanismAndPackage : true
statusTransportMechanism : EAI
Server : server B
enableStatusFileCreation : true
baseStatusCode : 0
storeId : 43001
enableTransportMechanismAndPackage : true
statusTransportMechanism : EAI
And as CSV:
PS \> $result | ConvertTo-Csv
"Server","enableStatusFileCreation","baseStatusCode","storeId","enableTransportMechanismAndPackage","statusTransportMechanism"
"server A","true","0","43001","true","EAI"
"server B","true","0","43001","true","EAI"