Question about Filter function in PowerShell



  • I am trying to automate a script to change the startup type of the Exchange services.
    I can do it crudely, and it suffices, but I would like to make it better and publish it. The reason is related to KB4072650 updating Hyper-Integration services. It breaks Exchange if the services are running when it applies.

    My problem is the -Filter function. I cannot seem to get it to take multiple parameters when Google seems to tell me that it should.

    This works.

    Get-WMIObject win32_service -Filter "name like 'MSExchange%'" | Format-Table Name, StartMode
    

    This does not work.

    Get-WMIObject win32_service -Filter {name like 'MSExchange%' -Or name like 'HostControllerService'} | Format-Table Name, StartMode
    

    I am piping it into Set-Service in the script, but piping it into the table to test that Get-WMIObject is returning what I need.



  • I tried wrapping that in various parenthesis to no avail also.



  • @jaredbusch

    I'd run something like this, which works in my cases (well, I used it with Get-Process, but should work the same with Services):

    Get-Service | Where-Object {($_.Name -like "MSExchange" -or $_.Name -like "HostControllerService")} | blah blah blah
    

    If you can't pipe it right after the filtering, you can store them in variables and that should work.



  • @tim_g I'll play with it more tomorrow. But I specifically cannot use Get-Service as that does not contain the current startup status.

    Once I got the above working, the next bit I will need is to add a filter on current startup. because I have some services disabled. I don't want them set to manual. and I don't want then set to automatic when I am done either.

    0_1525581826270_06282a03-9b1f-402e-aabe-4af525ab5786-image.png



  • For reference here is the entire manual method that I am trying to turn into a clean script.

    Turn things off

    # CHeck the status of hte Exchange Services
    Get-WMIObject win32_service -Filter "name like 'MSExchange%'" | Format-Table Name, StartMode
    # Set all the services to manual startup
    Get-WMIObject win32_service -Filter "name like 'MSExchange%'" | Set-Service -StartupType manual
    Set-Service -Name HostControllerService -StartupType manual
    

    Turn things back on

    # Set all of the services back to automatic
    Get-WMIObject win32_service -Filter "name like 'MSExchange%'" | Set-Service -StartupType automatic
    Set-Service -Name HostControllerService -StartupType automatic
    # Set IMAP and SMTP back to disabled
    Set-Service -Name MSExchangeImap4 -StartupType disabled
    Set-Service -Name MSExchangeIMAP4BE -StartupType disabled
    Set-Service -Name MSExchangePop3 -StartupType disabled
    Set-Service -Name MSExchangePOP3BE -StartupType disabled
    


  • @jaredbusch said in Question about Filter function in PowerShell:

    But I specifically cannot use Get-Service as that does not contain the current startup status

    Depends on the OS / version of PowerShell... what you doing this on?

    Anyways, try yoru filter using my format.



  • Get-Service | Where-Object {($_.Name -like "MSExchange*" -or $_.Name -like "HostControllerService*")} | FT -Property Name,StartType
    
    Get-Service | Where-Object {($_.Name -like "MSExchange*" -or $_.Name -like "HostControllerService*")} | Set-Service -StartupType Disabled
    


  • And if you really can't use Get-Service because of older PS version:

    Get-WMIObject win32_service | Where-Object {($_.Name -like "MSExchange*" -or $_.Name -like "HostControllerService*")} | FT -Property Name,StartMode
    

    WMI version = StartMode
    Service version = StartType



  • @jaredbusch said in Question about Filter function in PowerShell:

    because I have some services disabled. I don't want them set to manual. and I don't want then set to automatic when I am done either.

    Just seen this bit...

    You can add the ones you have disabled using this added in there at the end:

    -and $_.Name -ne "MSExchangeImap4"

    So:

    Get-WMIObject win32_service | Where-Object {($_.Name -like "MSExchange*" -or $_.Name -like "HostControllerService*" -and $_.Name -ne "MSExchangeImap4" -and $_.Name -ne "MSExchangeIMAP4BE" -and $_.Name -ne "MSExchangePop3" -and $_.Name -ne "MSExchangePOP3BE")} | Set-Service -StartupType Automatic
    

    I'm sure that's enough for you to get the point and change it to fit your needs.



  • A bit outside your specific question; however on my Exchange servers I just disabled the MSExchange ActiveDirectory Topology Service; doing that prevented the rest of the Exchange services from loading and KB40720650 loaded without issue