PowerShell: GUI to manage service of your choice

Today we will be going over how to stop and start a service using GUI with popups. We will be using the Get-Service, Stop-Service, and Start-Service cmdlets. To provide the popups we will be using a wscript.shell.

First we start by grabbing the list of services that are running using the cmdlet Get-Service with the following pipeline expressions where {$_.status -eq 'running'} | Out-GridView -OutputMode Single. Out-Gridview expression with the single outputmode switch will allow the user to only select one Service at a time. This is handy when restarting a service for a program during an upgrade.

$Service=Get-Service | where {$_.status -eq 'running'} | Out-GridView -OutputMode Single

We will also need to setup the popup menu

To add some error handling we will ask the user if the service they picked is correct:

if ($Service.Status -eq "Running"){
$ServiceName=$Service.DisplayName
$Yes_Cancle = $GUI.popup("Do you want to turn stop $ServiceName ?", `
0,"Manage Service",3)
}

If the wrong service was selected we will notify the user that it will not be stopped.

else {
$GUI.popup("You decided to cancel. $ServiceName will not be stopped")
Start-Service $ServiceName
cls
}

How long doe we want to stop it is based on the script, but we will say 5 minutes or 300000 millisecond. We will also notify the user once the service has stopped and started again. I understand that something could have came up during the update or you need more time. The service will not actually start until you it OK!. If the update or anything you are doing requires a restart. Depending on the service setting to manual or auto will bring that service back online, once the machine is rebooted.

If ($Yes_Cancle -eq 6) {
Stop-Service $ServiceName 
$GUI.popup("$ServiceName service has been STOPPED and will START again in 5 minutes, soon as you hit OK")
start-Sleep -s 300000
$GUI.popup("Time is up! $ServiceName service will now start as soon as you hit OK")
Start-Service $ServiceName
cls
}

The complete code is listed here:

Function ManageService{
$Service=Get-Service | where {$_.status -eq 'running'} | Out-GridView -OutputMode Single
$GUI = new-object -comobject wscript.shell
if ($Service.Status -eq "Running"){
$ServiceName=$Service.DisplayName
$Yes_Cancle = $GUI.popup("Do you want to turn stop $ServiceName ?", `
0,"Manage Service",3)
If ($Yes_Cancle -eq 6) {
Stop-Service $ServiceName 
$GUI.popup("$ServiceName service has been STOPPED and will START again in 5 minutes, soon as you hit OK")
start-Sleep -s 300000
$GUI.popup("Time is up! $ServiceName service will now start as soon as you hit OK")
Start-Service $ServiceName
cls
}
else {
$GUI.popup("You decided to cancel. $ServiceName will not be stopped")
Start-Service $ServiceName
cls
}}}
cls
ManageService

Hope you all enjoyed this PowerShell: Managed Service GUI. See you next time.

Advertisements

PowerShell Script: Bulk install MSP or MSI without zipped folder

This is another version of Installing MSP or MSI files, but with the manual unzip. You can read about how to do it with the zipped folders here.

write-host "Install bulk MSI"
## Location of the msi files
## PARAM ##
$path="C:\PROGRAM\Experiance Packs\"


## DO NOT EDIT ##
cd $path
$iisreset = Read-Host 'Do you want to stop IIS: Yes or No'

## IIS STOP ##
If (($iisreset -eq "yes") -or ($iisreset -eq "Yes") -or ($iisreset -eq "Y") -or ($iisreset -eq "y"))
{
	invoke-command -scriptblock {iisreset /stop}
}
Elseif (($iisreset -eq "no") -or ($iisreset -eq "No") -or ($iisreset -eq "N") -or ($iisreset -eq "n"))
{
	 write-host "IIS wasn't stopped, but IIS restarts automatically at 24:00"
}

## Installs MSI from Unzipped ##
$files = Get-ChildItem -Path $path -Recurse -File -Filter '*.ms*' 
foreach ($msifile in $files)
    {
        write-host "Finished msi "$msifile
        $arguments= "/qn /norestart"
        Start-Process  -file  $msifile.FullName -arg $arguments -passthru | wait-process
    }
    write-host "Finished applying all msi's"

## IIS Start##
If (($iisreset -eq "yes") -or ($iisreset -eq "Yes") -or ($iisreset -eq "Y") -or ($iisreset -eq "y"))
{
	invoke-command -scriptblock {iisreset /start}
}
Elseif (($iisreset -eq "no") -or ($iisreset -eq "No") -or ($iisreset -eq "N") -or ($iisreset -eq "n"))
{ 
}