Author Posts

April 12, 2017 at 8:48 am

Hello world!

I'm completely newbie in Powershell and i need your help ...
I created 3 variables containing values separated by ";" :

$process = processname
$Sresult = Sdata1;Sdata2;Sdata3;Sdata4;Sdata5 ....
$Iresult = Idata1;Idata2;Idata3;Idata4;Idata5 ....

I would like to have an array or hash table like this (i don't know what is the best solution) :

Process Sresult Iresult
processname Sdata1 Idata1
processname Sdata2 Idata2

and so on ... at the end of course a small CSV output is planned. 🙂 ..
Thanks !

April 12, 2017 at 9:48 am

It might be helpful to know how you created this variables. It might be easier to create what you're looking for directly in the process of getting the information than stitching it together afterwards. (sorry for my poor english)

April 12, 2017 at 11:49 am

Hi Olaf

I retrieve these values from a complex xml-like file with several tags with a regex ... these tags don't have any ID ...
e.g. :

suppliers
COM-09 To perform Operative Planning

inputs

Validated Operative Plan (full view & Cascaded)

I use this "really-dirty" foreach loop to parse all the lines :

# Retrieve the Suppliers
"**************** Suppliers **************************"
$pattern = "(?:)(.*)(?:)"
foreach ($suppliers in $XmlDocument.metadata.entry.map.entry.map.entry)
   {
    if ($suppliers.string -eq "suppliers")
      {
      $result = [Regex]::Match($suppliers.string, $pattern)
      $Sresult = $Sresult += $result.Value.replace('','').replace('','')
      $Sresult = $Sresult -replace "&","&"
      $Sresult = $Sresult + ";"
      }
    if ($suppliers.string -eq "inputs")
      {
      $result = [Regex]::Match($suppliers.string, $pattern)
      $Iresult = $Iresult += $result.Value.replace('','').replace('','')
      $Iresult = $Iresult -replace "&","&"
      $Iresult = $Iresult + ";"
    }
  }

April 12, 2017 at 11:55 am

#region Input
$Process = 'one','two','three'
$Color   = 'green','blue','yellow'
$Memory  = 2,4,8
#endregion

#region Process
# Put them in a Powershell Object:
$myOutput = 0..2 | foreach {
    [PSCustomObject]@{
        Process = $Process[$_]
        Color   = $Color[$_]
        Memory  = $Memory[$_]
    }
}
#endregion

#region Output
$myOutput | Format-Table -AutoSize                      # output to console
$myOutput | Out-GridView                                # output to ISE gridview
$myOutput | Export-Csv .\mystuff.csv -NoTypeInformation # dump to CSV
#endregion

April 12, 2017 at 12:11 pm

Works like a charm !

thanks you so much !

April 12, 2017 at 12:12 pm

For more information on arrays see this article..