Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Filtering XML from Powershell

$
0
0

I have the below XML

<!-- XML Configuration File to check standard of SQL Server Build -->

<serverInstances>
 <InstanceConfiguration>SMALL_SERVER

        <SERVER_TYPE>SMALL</SERVER_TYPE> 

                   <PROCESSOR_QTY>1</PROCESSOR_QTY>
     <PROCESSOR_QTYCORE>4</PROCESSOR_QTYCORE>
     <MEMORY_MIN>16GB</MEMORY_MIN>
     <MEMORY_MAX>64GB</MEMORY_MAX>
     <STORAGE_DATADRIVE_COUNT>2</STORAGE_DATADRIVE_COUNT>
     <STORAGE_TOTALDATADRIVE_SIZE>600GB</STORAGE_TOTALDATADRIVE_SIZE>

 </InstanceConfiguration>
 
 <InstanceConfiguration>MEDIUM_SERVER
               <SERVER_TYPE>MEDIUM</SERVER_TYPE> 

     <PROCESSOR_QTY>2</PROCESSOR_QTY>
     <PROCESSOR_QTYCORE>8</PROCESSOR_QTYCORE>
     <MEMORY_MIN>16GB</MEMORY_MIN>
     <MEMORY_MAX>128GB</MEMORY_MAX>
     <STORAGE_DATADRIVE_COUNT>2</STORAGE_DATADRIVE_COUNT>
     <STORAGE_TOTALDATADRIVE_SIZE>600GB</STORAGE_TOTALDATADRIVE_SIZE>


 </InstanceConfiguration>

        <InstanceConfiguration>LARGE_SERVER
       <SERVER_TYPE>LARGE</SERVER_TYPE> 

     <PROCESSOR_QTY>2</PROCESSOR_QTY>
     <PROCESSOR_QTYCORE>8</PROCESSOR_QTYCORE>
     <MEMORY_MIN>192GB</MEMORY_MIN>
     <MEMORY_MAX>384GB</MEMORY_MAX>
     <STORAGE_DATADRIVE_COUNT>3</STORAGE_DATADRIVE_COUNT>
     <STORAGE_TOTALDATADRIVE_SIZE>900GB</STORAGE_TOTALDATADRIVE_SIZE>


 </InstanceConfiguration>

</serverInstances> 

Which i am reading from the below powershell script

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
$XmlClusterDiskLabels=@() #Array to hold Disk Label names 

$serverName= $env:COMPUTERNAME 
$rootFolder = "C:\xml_powershell"
$ServerXML =  "$rootFolder\$serverName.xml"


[xml]$Configxml=Get-Content $ServerXML
		foreach ($ReadConfigxml in $Configxml.serverInstances.InstanceConfiguration)
		{
			$ServerType=$ReadConfigxml.SERVER_TYPE
			Write-Host $ServerType
			$PQty=$ReadConfigxml.PROCESSOR_QTY
			#Write-Host $PQty
			
					#Get Server system information
						$Systeminfo = get-wmiobject Win32_ComputerSystem -cn $serverName

						[float]$PhysicalMem=$Systeminfo.TotalPhysicalMemory / 1GB
						
						Write-Host "Physical no of Processors: "$Systeminfo.NumberOfProcessors						
						Write-Host "Logical no of processors : "$Systeminfo.NumberOfLogicalProcessors
						Write-Host "Total Physical Memory    : "$PhysicalMem
						$cpuinfo="NumberOfCores"
						$cpudata = Get-WmiObject -class win32_processor –computername $serverName -Property $cpuinfo 
						
						Write-Host "Number of Processor Cores : "$cpudata.NumberOfCores
						$DriveInfo=Get-WmiObject Win32_volume -ComputerName LocalHost |Where-Object {$_.Label -like '*Local*'}#| Format-Table Name, Label, @{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.capacity/1gb))}}, @{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, @{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.capacity/1gb))}} –AutoSize 
							foreach ($DataDrive in $DriveInfo)
							{
							Write-Host "Drive Name :" $DataDrive.Name
							Write-Host "Drive Lebel:" $DataDrive.Label 
							[float]$TDS=$DataDrive.Capacity / 1GB
							Write-Host "Drive Total Size:" $TDS " GB"
							}
		}

Is there a way that i an read the XML data based on Filter (example)

Condition ='LARGE_SERVER'

Expected Result

      <SERVER_TYPE>LARGE</SERVER_TYPE> 

     <PROCESSOR_QTY>2</PROCESSOR_QTY>
     <PROCESSOR_QTYCORE>8</PROCESSOR_QTYCORE>
     <MEMORY_MIN>192GB</MEMORY_MIN>
     <MEMORY_MAX>384GB</MEMORY_MAX>
     <STORAGE_DATADRIVE_COUNT>3</STORAGE_DATADRIVE_COUNT>
     <STORAGE_TOTALDATADRIVE_SIZE>900GB</STORAGE_TOTALDATADRIVE_SIZE>



Mohd Sufian www.sqlship.wordpress.com Please mark the post as Answered if it helped.




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>