Class Challenge
Create a function that takes an input of a service name and prompt the user to stop or start that service
Serviceprompt.ps1
Function serviceprompt { $serviceName = Read-Host "Please enter the service name " $serviceStatus =(Get-Service $serviceName).status if ($serviceStatus -match "Running") { ` $stopPrompt =Read-Host "This service is running do you want to stop it? Y\N " if ($stopPrompt -eq "Y") { ` stop-service $serviceName Write-Output "stopping the $servicename Service"} ` Else {Write-output "Leaving Service $serviceStatus"}} ` ElseIF ($serviceStatus -match "Stopped"){ ` $startPrompt =Read-Host "This service is stopped do you want to start it? Y\N " if ($startPrompt -eq "Y") { ` start-service $serviceName Write-Output "starting the $servicename Service"} ` Else {Write-output "Leaving Service $serviceStatus"}} ` Else { Write-Output "Service is in $ServiceState State"} } serviceprompt
http://bartdesmet.net/blogs/bart/archive/2006/09/16/4429.aspx
Not working yet
Servicepromptgui.ps1
Function serviceprompt { [system.reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") #[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $serviceName = Read-Host "Please enter the service name " $serviceStatus =(Get-Service $serviceName).status if ($serviceStatus -match "Running") { ` $strMessage = "This service is running do you want to stop it?" $stopmsgBox = [Windows.Forms.MessageBox]::Show($strMessage, "Stop Service?", ` [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question) #$stopmsgBox -eq [Windows.Forms.DialogResult]::Yes if ($stopPrompt -eq "Yes") { ` #stop-service $serviceName Write-Output "stopping the $servicename Service"} ` Else {Write-output "Leaving Service $serviceStatus"}} ` ElseIF ($serviceStatus -match "Stopped"){ ` $strMessage = "This service is stopped do you want to start it?" $stopmsgBox = [Windows.Forms.MessageBox]::Show($strMessage, "Start Service?", ` [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question) if ($startPrompt -eq "Y") { ` start-service $serviceName Write-Output "starting the $servicename Service"} ` Else {Write-output "Leaving Service $serviceStatus"}} ` Else { Write-Output "Service is in $ServiceState State"} } serviceprompt
ClassChallenge.ps1
Function StopStart-Service { $sService = $args[0] $baction = $args[1] $response = $args[2] Write-Host $response if ($response -eq "Yes") { if ($baction -eq "Stop") { Get-Service $sservice | Stop-Service } ElseIf ($baction -eq "Start") { Get-Service $sService | Start-Service } Else { Write-Host "User terminated!" } } Else { Write-Host "No Action required " } } #uncomment the next two lines to read from a passed argument and comment the 2 lines after that #$sService = $args[0] #Write-Host "Service Name: $service" [system.reflection.assembly]::loadwithpartialname("microsoft.visualbasic") $serviceName = [microsoft.visualbasic.interaction]::InputBox("please enter a service name.", "Service Name") $oService = Get-Service $sService $Prompt = " The service $($oService.DisplayName) is currently" $iButtons = 4 # yes/no buttons $sTitle = "Action Required!" #$oService.status if ($oService.Status -eq "Stopped") { $Prompt = $Prompt + " Stopped. Would you like to start it?" $sResponse = [microsoft.visualbasic.interaction]::MsgBox($prompt,$iButtons,$sTitle) stopstart-service $sService "Start" $sResponse (Get-Service $sService).status } Elseif ($oService.Status -eq "Running") { $Prompt = $Prompt + " Running. Would you like to stop it?" $sResponse = [microsoft.visualbasic.interaction]::MsgBox($prompt,$iButtons,$sTitle) stopstart-service $sService "Stop" $sResponse (Get-Service $sService).status } Else { Write-Host "blah" }