Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function serviceStartStop{
- Do {
- $choice = Read-Host 'Do you want to do this locally (l) or remotely (r)?'
- } Until ($x -match '^(l|r)$')
- If ($choice -eq 'r') {
- Do {
- $computer = Read-Host 'Enter remote computer name.'
- } Until ($computer -ne '')
- }
- $serviceName = Read-Host "Please enter the service name."
- $title = "Start / Stop Service"
- $message = "Do you want to start or stop the service"
- $start = New-Object System.Management.Automation.Host.ChoiceDescription "&start","Starts the service"
- $stop = New-Object System.Management.Automation.Host.ChoiceDescription "&stop","Stops the service"
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($start, $stop)
- $result = $host.ui.PromptForChoice($title, $message, $options, 0)
- Switch ($choice) {
- 'r' # Remote Comptuer.
- {Switch ($result) {
- # Start.
- 0 {
- try {
- Get-Service -ComputerName $computer -Name $serviceName | Start-Service -WhatIf
- } catch {
- Write-Warning -Message "Cannot find any service with the service name $serviceName."
- }
- }
- # Stop
- 1 {
- try {
- Get-Service -ComputerName $computer -Name $serviceName | Stop-Service -WhatIf
- } catch {
- Write-Warning -Message "Cannot find any service with the service name $serviceName."
- }
- }
- }
- } # End Nested Switch #1.
- 'l' # Local Computer.
- {Switch ($result) {
- # Start.
- 0 {
- try {
- Get-Service -Name $serviceName -ErrorAction Stop | Start-Service -WhatIf
- } catch {
- Write-Warning -Message "Cannot find any service with the service name $serviceName."
- }
- }
- # Stop.
- 1 {
- try {
- Get-Service -Name $serviceName -ErrorAction Stop | Stop-Service -WhatIf
- } catch {
- Write-Warning -Message "Cannot find any service with the service name $serviceName."
- }
- }
- }
- } # End Nested Switch #2.
- } # End External Switch.
- }
- serviceStartStop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement