ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Question about Filter function in PowerShell

    IT Discussion
    powershell exchange windows update kb4072650
    3
    10
    1.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JaredBuschJ
      JaredBusch
      last edited by

      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.

      ObsolesceO 1 Reply Last reply Reply Quote 2
      • JaredBuschJ
        JaredBusch
        last edited by

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

        1 Reply Last reply Reply Quote 0
        • ObsolesceO
          Obsolesce @JaredBusch
          last edited by Obsolesce

          @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.

          JaredBuschJ 1 Reply Last reply Reply Quote 0
          • JaredBuschJ
            JaredBusch @Obsolesce
            last edited by JaredBusch

            @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

            ObsolesceO 2 Replies Last reply Reply Quote 0
            • JaredBuschJ
              JaredBusch
              last edited by

              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
              
              1 Reply Last reply Reply Quote 1
              • ObsolesceO
                Obsolesce @JaredBusch
                last edited by

                @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.

                1 Reply Last reply Reply Quote 0
                • ObsolesceO
                  Obsolesce
                  last edited by

                  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
                  
                  1 Reply Last reply Reply Quote 0
                  • ObsolesceO
                    Obsolesce
                    last edited by Obsolesce

                    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

                    1 Reply Last reply Reply Quote 0
                    • ObsolesceO
                      Obsolesce @JaredBusch
                      last edited by

                      @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.

                      1 Reply Last reply Reply Quote 0
                      • jt1001001J
                        jt1001001
                        last edited by

                        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

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post