none
Checking multiple Service status using PowerShell Workflow RRS feed

  • Question

  • Hello,

    I am planning to check status of multiple services for a list of servers using Parallel. I am finding it difficult to map an object for a computer with service's status with the main object. 

    Below is the code:

    Workflow Check-Services
    {
        PARAM([string[]]$SvCName,[string[]]$SRVList)
    
        
        foreach -parallel ($computer in $SRVList)
        {
            InlineScript
            {
            $Out = @()
                foreach ($service in $SvCName)
                {
                     $props=[ordered]@{
                     ComputerName=$USING:computer;
                     ServiceName=$USING:service
                     StartUpMode=$null
                     State=$null }
    
                     Try
                     {
                        $props.StartUpMode=Get-WmiObject -class Win32_Service -ComputerName $USING:computer -ErrorAction 'Stop' | Where-Object{$_.Name -eq $USING:service} | Select-Object -ExpandProperty StartMode
                        if ($props.StartUpMode -eq $null) { $props.StartUpMode = "Service does not exist" }
                     }
                     Catch
                     {
                        $props.StartUpMode="Error"
                     }
    
                     Try
                     {
                        $props.State=Get-WmiObject -class Win32_Service -ComputerName $USING:computer -ErrorAction 'Stop' | Where-Object{$_.Name -eq $USING:service} | Select-Object -ExpandProperty State
                        if ($props.State -eq $null) { $props.State = "Service does not exist" }
                     }
                     Catch
                     {
                        $props.State="Error"
                     }
                     
                }
            $Out += New-Object -TypeName PSObject -Property $props 
            }
        }
    }

    Please help.

    Thanks in advance.

    Rajiv

    Thursday, May 22, 2014 9:53 AM

Answers

  • YOU have to learn how variables work in a workflow and how to structure PowerShell correctly.  You are writing too much code because you have not understood the basics yet.

    Workflow Check-Services{
        Param(
            [string[]]$SvCName,
            [string[]]$SRVList
        )
        
        foreach -parallel ($computer in $SRVList){
            InlineScript{
                foreach ($service in $using:SvCName){
                    $props=[ordered]@{
                        ComputerName=$USING:computer
                        ServiceName=$service
                        StartMode=$null
                        State='Not Found'
                    }
                    if($s=Get-WmiObject -Filter "Name='$service'" -class Win32_Service -ComputerName $USING:computer){
                        $props.State=$s.State
                        $props.StartMode=$s.StartMode
                    }                 
                    New-Object -TypeName PSObject -Property $props 
                }
            }
        }
    }
    When you can make your code more readable it will be easier to see the mistakes.  You had numerous errors.  Go back and compare the code line by line very carefully until you understand what I changed.  Ask if you cannot understand why I made the change.


    ¯\_(ツ)_/¯

    • Marked as answer by Rajiv IR Thursday, May 22, 2014 11:20 AM
    Thursday, May 22, 2014 10:56 AM

All replies

  • Don't use both parallel and wmi remoting.


    ¯\_(ツ)_/¯

    Thursday, May 22, 2014 10:18 AM
  • What do you mean by "map" and object?  That is not a PowerShell thing.

    ¯\_(ツ)_/¯

    Thursday, May 22, 2014 10:19 AM
  • I already gave you the solution to this in another thread a week ago.  The answer has not changed.  Asking a slight variation on the same question is not useful.


    ¯\_(ツ)_/¯

    Thursday, May 22, 2014 10:20 AM
  • Hi,

    I am dealing with multiple computers and services. Hence we have a m:m relationship. I wondering if its possible to generate an object for each computer against the services and in the end club all the data into a master object.

    Rajiv

    Thursday, May 22, 2014 10:40 AM
  • How can I get to check the list of service status for every computer inside parallel?
    Thursday, May 22, 2014 10:46 AM
  • YOU have to learn how variables work in a workflow and how to structure PowerShell correctly.  You are writing too much code because you have not understood the basics yet.

    Workflow Check-Services{
        Param(
            [string[]]$SvCName,
            [string[]]$SRVList
        )
        
        foreach -parallel ($computer in $SRVList){
            InlineScript{
                foreach ($service in $using:SvCName){
                    $props=[ordered]@{
                        ComputerName=$USING:computer
                        ServiceName=$service
                        StartMode=$null
                        State='Not Found'
                    }
                    if($s=Get-WmiObject -Filter "Name='$service'" -class Win32_Service -ComputerName $USING:computer){
                        $props.State=$s.State
                        $props.StartMode=$s.StartMode
                    }                 
                    New-Object -TypeName PSObject -Property $props 
                }
            }
        }
    }
    When you can make your code more readable it will be easier to see the mistakes.  You had numerous errors.  Go back and compare the code line by line very carefully until you understand what I changed.  Ask if you cannot understand why I made the change.


    ¯\_(ツ)_/¯

    • Marked as answer by Rajiv IR Thursday, May 22, 2014 11:20 AM
    Thursday, May 22, 2014 10:56 AM
  • Hi Jrv,

    Yes you are right. I need to learn more. I admit I need to improve on my PowerShell Basics. Actually I am fairly new to PowerShell. 

    Thanks very much for your advice. Now it makes more clear on the usage.

    I never knew you could use $USING:SvcName in foreach also... I guess that has made a big difference on the way the code executes.

    Thanks a lot

    Rajiv

    Thursday, May 22, 2014 11:23 AM
  • #1 - stop capitalizing everything.  It is distracting.  The keywords only have first characters capped except for steering logic. It is more readable when you do it that way.

    #2 - all references to workflow variables in an inline script must use the "using" scope.

    You cannot use using on local variables which is what you did with $service.


    ¯\_(ツ)_/¯

    Thursday, May 22, 2014 11:30 AM