START/ STOP services and rename any folder

Hey Guys, What's up? 

 

Today we are going to automate and deploy the below tasks.

Stop Services

Perform Operations (rename folder)

Start Services

 

How?

below script is designed to aim at the goal.

 

<#

    .SYNOPSIS

    .DESCRIPTION

    .NOTES

    =============================================================================================================================================

    Created with:     Windows PowerShell ISE

    Created on:       Sunday, May 8, 2022 10:25:46 AM

    Created by:       Dashrath Chate

    Organization:     Organization Name

    =============================================================================================================================================

#>

$maxRepeat = 20

$status = "Stopped" # change to Stopped if you want to wait for services to start

$dtm = get-date -Format mm_dd_yyyy_hhmmsstt

function waitiuntilserverstatus($services)

{

do

{

    Stop-Service -Name $services -Force -ErrorAction SilentlyContinue

    $count = (Get-Service $services | ? {$_.status -eq $status}).count

    $maxRepeat--

    sleep -Milliseconds 600

} until ($count -eq 1 -or $maxRepeat -eq 0)

 

}

$services = 'ccmexec','bits' # provide the service name in '','' format to stop the service

foreach ($services in $services)

{

waitiuntilserverstatus $services

}

Ren C:\Windows\SoftwareDistribution SoftwareDistribution.$dtm

Ren C:\Windows\System32\catroot2 Catroot2.$dtm

 

Additional in this script, create one more function to start the service and use at the end of this script

 

function waitiuntilserverstatus($services)

{

do

{

    Stop-Service -Name $services -Force -ErrorAction SilentlyContinue

    $count = (Get-Service $services | ? {$_.status -eq $status}).count

    $maxRepeat--

    sleep -Milliseconds 600

} until ($count -eq 0 -or $maxRepeat -eq 0)

 

}

 

Enjoy the scripting, do let me know where you stuck!

 

No comments:

Post a Comment

Leave your valuable words here for improve better.