how to retrieve string value in this array??

Welcome Forums General PowerShell Q&A how to retrieve string value in this array??

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
8 months, 4 weeks ago.

  • Author
    Posts
  • #94266

    Participant
    Points: -19
    Rank: Member

    I've got
    $services = Get-WMIObject Win32_Service -Filter "StartName='$account'"
    Which returns:
    \\SQL6\root\cimv2:Win32_Service.Name="MSSQLSERVER"
    \\SQL6\root\cimv2:Win32_Service.Name="ReportServer"
    \\SQL6\root\cimv2:Win32_Service.Name="SQLBrowser"
    \\SQL6\root\cimv2:Win32_Service.Name="SQLSERVERAGENT"

    The foreach is like this:

    ForEach ($service in $services) {

    # if ($service.Name() CONTAINS "ReportServer") {
    # continue (do not do anything, go on to the next item in the array)
    # }

    write-host $service

    }

    What is the correct way to test the service string to continue/leave the foreach loop??
    I also need to test whether the service is set to manual or not...I know 'state' is stopped or running.
    Would status be the right thing to test?? for manual or not??

    Thank you, Tom

  • #94287

    Participant
    Points: 1
    Rank: Member

    Hi Tom,

    You can do what you are after like this:

    foreach ($service in $services) {
        if (-not ($service.Name -eq 'ReportServer')) {
            Write-Host $service
        }
    }

    Alternatively you can do like this:

    foreach ($service in $services) {
        if ($service.Name -eq 'ReportServer') {
            continue
        }
        Write-Host $service
    }

    And if you wanted to mach on multiple service names, you could do it like this:

    $skipServices = 'ReportServer', 'SQLBrowser'
    
    foreach ($service in $services) {
        if (-not ($skipServices -contains $service.Name)) {
            Write-Host $service
        }
    }

    You can use the 'StartMode' property of the service object to check if the service start mode is set to manual.

  • #94296

    Participant
    Points: 226
    Helping Hand
    Rank: Participant

    Well, all you want to know to start with is in the built-in help files

        # Get parameters, examples, full and Online help for a cmdlet or function
    
        (Get-Command -Name Get-Content).Parameters
        Get-help -Name Get-Content -Examples
        Get-help -Name Get-Content -Full
        Get-help -Name Get-Content -Online
    
        Get-Help about_*
        Get-Help about_For
        Get-Help about_ForEach*
        Get-Help about_Operators
    
    
        # All Help topics locations
        explorer "$pshome\$($Host.CurrentCulture.Name)"
    

    Tyr to avoid the use of contains on simple strings (though you can do it), as a rule, as its use case is to look at a collection or stuff. Use -match or Select-String instead. See the above help on operators.

    So, based on what you are after. How about this approach.

        ($account = 'LocalSystem')
        Get-WmiObject Win32_service `
        | Where {$_.StartName -eq $account -and $_.Name -notmatch '.*SQL.*|ReportServer'} `
        | Select Name,State,StartMode,StartName `
        | Format-Table -AutoSize
    
    
        Name                                     State   StartMode StartName  
        ----                                     -----   --------- ---------  
        AdobeARMservice                          Running Auto      LocalSystem
        AESMService                              Running Auto      LocalSystem
        Appinfo                                  Running Manual    LocalSystem
        AppMgmt                                  Stopped Manual    LocalSystem
        ...
    
    
        ($account = 'LocalSystem')
        Get-WmiObject Win32_service `
        | Where {$_.StartName -eq $account -and $_.Name -match '.*SQL.*|ReportServer'} `
        | Select Name,State,StartMode,StartName `
        | Format-Table -AutoSize
    
    
        Name      State   StartMode StartName  
        ----      -----   --------- ---------  
        SQLWriter Running Auto      LocalSystem
    

    Lastly, depending on you PoSH version; never use Write-Host, except for outputting colorized text to the screen or other custom formatting needs. Output to the screen is the default.

    So, this...

    write-host $service

    and this...
    $service

    and this...

    "$service"

    and this...
    ($service)

    ...will do exactly the same thing, output to the screen.
    The Write-Host specifically clears the buffer so, anything in that varible is gone. So, no pipeline when you use it.

    See

    Write-Host Considered Harmful – by PowerShell founder Jeffrey Snover
    'jsnover.com/blog/2013/12/07/write-host-considered-harmful'

    However, now, in PoSHv5 Jeffrey Snover now says...
    With PowerShell v5 Write-Host no longer "kills puppies". data is captured into info stream
    'docs.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Write-Information?view=powershell-5.1'

  • #94297

    Participant
    Points: -19
    Rank: Member

    Thank you, everyone...
    I never knew that about Write-Host.
    I use it for 'debug' printing things to the screen.
    I then remove it when I'm done with the script.
    Thank you, Tom

The topic ‘how to retrieve string value in this array??’ is closed to new replies.