0

I need to identify if the installed software (several times per host) is an 32bit or 64bit version. To do this I want to check the Execution Folder of the Service via powershell.

This is my first powershell script and I'm a bit lost. I would like to store the information of Get-WmiObject win32_service to a multidimensional array.

If I run the command selecting PathName, State and DisplayName the PathName will be shortened, for that I run this command several times. But don't know how to get is in the right fields of the array or get the right fields in my foreach

Here is what I got so far:

`$ServiceArray = @()
$ServiceArray[] = Get-WmiObject win32_service | ?{$_.Name -like 'foo_*'} | 
Select PathName
$ServiceArray[][] = Get-WmiObject win32_service | ?{$_.Name -like 'foo_*'} | 

Select State
$ServiceArray[][][] = Get-WmiObject win32_service | ?{$_.Name -like 'foo_*'} 
| Select DisplayName
foreach($array in $ServiceArray[]) 
   {
    if ($array.Contains(\bin\test\win64\test.exe)
    {
     $ServiceArray[][][][] = "win64"
 }    
 else 
 {
     $ServiceArray[][][][] = "win32"
 }  `

I know that it does not work this way, but I don't know how it works correct, either.

1
  • "I would like to store the information [in] a multidimensional array" - why? What fields/properties are you interested in? Commented Feb 12, 2018 at 13:54

2 Answers 2

2

You can select multiple properties in the same statement with Select-Object:

$ServiceArray = Get-WmiObject Win32_Service | 
  Where-Object {$_.Name -like 'foo_*'} |
  Select PathName,State,DisplayName

You can also use Select-Object with a calculated property to add the bitness based on the PathName argument if needed:

$ServiceArray = Get-WmiObject Win32_Service | 
  Where-Object {$_.Name -like 'foo_*'} |
  Select PathName,State,DisplayName,@{Name='Bitness';Expression={if($_.PathName -like "*Win64*"){"Win64"}else{"Win32"}}}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that you can get better performance with Get-WmiObject Win32_Service -Filter 'Name LIKE "foo_%"'. It probably won't matter much for a local query, but over a remote connection it's about 10 times faster to filter with Get-WmiObject.
0

You can select multiple properties. You were almost there. Edited your code a little.

$ServiceArray = @()
$ServiceArray = Get-WmiObject win32_service | ?{$_.Name -like 'foo_*'} | Select PathName,State,DisplayName,Architecture
foreach($Element in $ServiceArray) 
{
    if ($Element.PathName.Contains('\bin\test\win64\test.exe'))
    {
        $Element.Architecture = 'win64'
    }    
    else 
    {
        $Element.Architecture = 'win32'
    }
    $Element
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.