Powershell - Lync Change Services to Automatic / Manual / Stop and Start before patching

This is a
common problem I have come across in an enterprise pool, when you drain and
patch and reboot one of the front end nodes the services will start again and
users will re-home to the server, but you may not be finish working with the
server and this leads to additional disruption for your end users. I taught I
would create a very simple script which would allow you to stop the services
gracefully, put them to manual so they would start back up and then when
finished patching, put them back to automatic and start them again... making
the process a little better...

 

 

I did post this earlier but didn't realise It was the wrong version of the script :)

 

    #Lync Put servers in maintenance mode

 

    #Services Array To Process. All Lync 2010 Services

 

    [array]$servicearray += "RTCASMCU"
    $servicearray += "RTCATS"
    $servicearray += "RTCAVMCU"
    $servicearray += "RTCCAA"
    $servicearray += "RTCCAS"
    $servicearray += "RTCCPS"
    $servicearray += "RTCDATAMCU"
    $servicearray += "RTCIMMCU"
    $servicearray += "RTCMEETINGMCU"
    $servicearray += "RTCPDPAUTH"
    $servicearray += "RTCPDPCORE"
    $servicearray += "RTCRGS"
    $servicearray += "RTCSRV"
    $servicearray += "replica"
    $servicearray += "w3svc"

 

    function servicetomanual($service)
    {

 

    
        foreach ($serv in $service)
        {
        Set-Service $serv -StartupType Manual
        }

 

    }

 

    function servicetoautomatic($service)
    {
   
   
        foreach ($serv in $service)
        {
        set-service $serv -StartupType Automatic
        }
    }

 

    function stopservices($service)
    {
   
       
        foreach ($serv in $service)
        {
        Stop-CswindowsService $serv -graceful
        }
    }

 

    function startservices($service)
    {
   
       
        foreach ($serv in $service)
        {
        Stop-CsWindowsService $serv -graceful
        }
    }

 

 

 

    #Test if module is loaded before beginning script

 

    $module = get-module |where {$_.name -eq "Lync"}

 

    if ($module.count -eq 0)
    {
        import-module Lync
    }

 

    #Main Script Loop

 

    while ($x=0)
    {
        #Present a Menu
        cls
        write-host "Please choose from the following options"
        write-host "1.`t Put Services to Manual"
        write-host "2.`t Put Services to Automatic"
        write-host "3.`t Stop Services"
        write-host "4.`t Start Services"
        write-host "0.`t Exit"
        $answer = Read-host "Please Choose a number and press enter"

 

        switch ($answer)
        {
            1 {servicetomanual $servicearray}
            2 {servicetoautoamtic $servicearray}
            3 {Stopservices $servicearray}
            4 {startservices $servicearray}
            0 {exit 0}
        }
    }