Advertisement
Guest User

Untitled

a guest
Apr 16th, 2016
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function serviceStartStop{
  2.     Do {
  3.         $choice = Read-Host 'Do you want to do this locally (l) or remotely (r)?'
  4.     } Until ($x -match '^(l|r)$')
  5.        
  6.     If ($choice -eq 'r') {
  7.         Do {
  8.             $computer = Read-Host 'Enter remote computer name.'
  9.         } Until ($computer -ne '')
  10.     }
  11.  
  12.     $serviceName = Read-Host "Please enter the service name."
  13.     $title = "Start / Stop Service"
  14.     $message = "Do you want to start or stop the service"
  15.     $start = New-Object System.Management.Automation.Host.ChoiceDescription "&start","Starts the service"
  16.     $stop = New-Object System.Management.Automation.Host.ChoiceDescription "&stop","Stops the service"
  17.     $options = [System.Management.Automation.Host.ChoiceDescription[]]($start, $stop)
  18.     $result = $host.ui.PromptForChoice($title, $message, $options, 0)
  19.  
  20.     Switch ($choice) {
  21.         'r' # Remote Comptuer.
  22.             {Switch ($result) {
  23.                 # Start.
  24.                 0 {
  25.                     try {
  26.                         Get-Service -ComputerName $computer -Name $serviceName | Start-Service -WhatIf
  27.                     } catch {
  28.                         Write-Warning -Message "Cannot find any service with the service name $serviceName."
  29.                     }
  30.                 }
  31.                 # Stop
  32.                 1 {
  33.                     try {
  34.                         Get-Service -ComputerName $computer -Name $serviceName | Stop-Service -WhatIf
  35.                     } catch {
  36.                         Write-Warning -Message "Cannot find any service with the service name $serviceName."
  37.                     }
  38.                 }
  39.             }
  40.         } # End Nested Switch #1.
  41.         'l' # Local Computer.
  42.             {Switch ($result) {
  43.                 # Start.
  44.                 0 {
  45.                     try {
  46.                         Get-Service -Name $serviceName -ErrorAction Stop | Start-Service -WhatIf
  47.                     } catch {
  48.                         Write-Warning -Message "Cannot find any service with the service name $serviceName."
  49.                     }
  50.                 }
  51.                 # Stop.
  52.                 1 {
  53.                     try {
  54.                         Get-Service -Name $serviceName -ErrorAction Stop | Stop-Service -WhatIf
  55.                     } catch {
  56.                         Write-Warning -Message "Cannot find any service with the service name $serviceName."
  57.                     }
  58.                 }
  59.             }
  60.         } # End Nested Switch #2.
  61.     } # End External Switch.
  62. }
  63. serviceStartStop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement