Vote count:
0
Greatings, I'm trying to extract WMI informations with Powershell in order to push them in a Mysql DB via PHP. If a machine has no access to the internet, I export an XML file (that is working).
$filePath = $PSScriptRoot + '\processors.xml'
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)
$processor = Get-WmiObject Win32_Processor
$xmlWriter.WriteStartElement("Processor")
foreach ($item in $processor){
$xmlWriter.WriteStartElement($item.DeviceID)
$xmlWriter.WriteElementString("Name",$item.Name)
$xmlWriter.WriteElementString("AddressWidth",$item.AddressWidth)
$xmlWriter.WriteElementString("NumberOfCores",$item.NumberOfCores)
$xmlWriter.WriteElementString("NumberOfLogicalProcessors",$item.NumberOfLogicalProcessors)
$xmlWriter.WriteElementString("L2CacheSize",$item.L2CacheSize)
$xmlWriter.WriteElementString("L3CacheSize",$item.L3CacheSize)
$xmlWriter.WriteEndElement()
}
$xmlWriter.WriteEndElement()
$xmlWriter.Finalize
$xmlWriter.Flush()
$xmlWriter.Close()
But if a machine has access to the internet, I prefer to push informations directly to a page, via HTTP POST, which will verify informations and store them in the DB. Here is my problem, as I don't know the number of items (here processors but imagine with memories, hard drives... etc.) I use a foreach statement to create the XML file (and it works) but, I don't know how to create the equivalent structure to create and fill a multi dimensionnal arry $postprocessor that stores CPUs informations. I have tried the following code but I'm stuck (that is not working) :
$processor = Get-WmiObject Win32_Processor
$postprocessor = @()
foreach ($item in $processor){
$itemarr = @()
$itemarr += 'Name="'+$item.Name+'"'
$itemarr += 'AddressWidth="'+$item.AddressWidth+'"'
$itemarr += 'NumberOfCores="'+$item.NumberOfCores+'"'
$itemarr += 'NumberOfLogicalProcessors="'+$item.NumberOfLogicalProcessors+'"'
$itemarr += 'L2CacheSize="'+$item.L2CacheSize+'"'
$itemarr += 'L3CacheSize="'+$item.L3CacheSize+'"'
$postprocessor.Add($item.DeviceID+'='+$itemarr)
# write-host $itemarr
Remove-Variable itemarr
}
$postParams = $postprocessor
$sending = Invoke-WebRequest -Uri http://localhost:8080/wmi/test.php -Method POST -Body $postParams
In fact I just wanted to store those informations like this :
$postParam[]
$postProcessors[]
->Processor1[]
->Name = value
...
->L3Cachesize = value
->Processor2[]
->Name = value
...
->L3Cachesize = value
postMemories[...]
postHardDrives[...]
etc
Maybe I have not found previous answer that solve my problem, if it is the case excuse me by advance and I'll be glad to read as many links that you'll send to me. Best regards !
Powershell Multidimensional associative table
Aucun commentaire:
Enregistrer un commentaire