The dar file
Download dar file
The powershell code
param( [Parameter(mandatory=$false,helpmessage="The computer name (default localhost)")] [string]$ComputerName="localhost", [Parameter(mandatory=$false,helpmessage="How long to wait before we check the status")] [int]$WaitStep = 2, [Parameter(mandatory=$false,helpmessage="How long to wait before we flag a timeout")] [int]$WaitFail = 10, [Parameter(mandatory=$true,helpmessage="The service name")] [string]$ServiceName, [Parameter(mandatory=$false,helpmessage="The startup type : manual or automatic (optional)")] [ValidateSet("manual","automatic")] [string]$StartupType, [Parameter(mandatory=$false,helpmessage="The name of credentials to find in WFA, in case of remote computer")] [string]$CredentialName, [Parameter(mandatory=$true,helpmessage="What to do : stop,start or restart")] [ValidateSet("start","stop","restart")] [string]$Action ) $mycreds = Get-WfaCredentials $CredentialName $ErrorActionPreference = "stop" $workingremote = $false $session = $null function getservice($name,$session){ if(-not $session){ $service = get-service -Name $name }else{ $service = Invoke-Command -Session $session -ScriptBlock {param($name)get-service -Name $name} -ArgumentList $name } return $service } function startService($name,$session){ $waited=0 $service = getService -name $name -session $session if($service){ while($service.Status -ne "Running" -and $waited -lt $waitfail){ Get-WfaLogger -info -message "Starting $name [$($service.status)]..." if(-not $session){ start-service -Name $name }else{ Invoke-Command -Session $session -ScriptBlock {param($name)start-service -Name $name} -ArgumentList $name } sleep $waitstep $service = getservice -name $name -session $session $waited +=$waitstep } } $service = getService -name $name -session $session if($service.Status -ne "Running"){ throw "Could not start $name service in a timely manner" }else{ Get-WfaLogger -info -message "$name is running" } } function stopService($name,$session){ $waited=0 $service = getService -name $name -session $session if($service){ while($service.Status -ne "Stopped" -and $waited -lt $waitfail){ Get-WfaLogger -info -message "Stopping $name [$($service.status)]..." if(-not $session){ stop-service -Name $name }else{ Invoke-Command -Session $session -ScriptBlock {param($name)stop-service -Name $name} -ArgumentList $name } sleep $waitstep $service = getService -name $name -session $session $waited +=$waitstep } } $service = getService -name $name -session $session if($service.Status -ne "Stopped"){ throw "Could not stop $name service in a timely manner" }else{ Get-WfaLogger -info -message "$name is stopped" } } function setServiceStartup($name,$session,$type){ Get-WfaLogger -info -message "Setting startup type [$StartupType]" if($session){ Invoke-Command -Session $session -ScriptBlock {param($name,$type)set-service -Name $name -startuptype $type} -ArgumentList $Servicename,$StartupType }else{ Set-Service -Name $name -StartupType $type } } try{ if($ComputerName -ne "localhost"){ Get-WfaLogger -info -message "Connecting to remote computer" $session = New-PSSession -ComputerName $ComputerName -Credential $mycreds $workingremote = $true Get-WfaLogger -info -message "Working remote on [$ComputerName]" }else{ Get-WfaLogger -info -message "Working local" } if($Action -in "stop","restart"){ stopService -name $Servicename -session $session } if($Action -in "start","restart"){ startService -name $Servicename -session $session } if($StartupType){ setServiceStartup -name $Servicename -session $session -type $StartupType } }catch{ throw $_.Exception }finally{ if($workingremote){ $out = Disconnect-PSSession -Session $session -OutVariable dummy $out = Remove-PSSession -Session $session -OutVariable dummy } }
No comments :
Post a Comment