This repository has been archived by the owner on Nov 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unquoted_service_paths_mini.ps1
1 lines (1 loc) · 1.95 KB
/
unquoted_service_paths_mini.ps1
1
$ss = $null; $i = $null; try { Write-Host "Fetching the list of services, this may take a while..."; $ss = Get-WmiObject -Class Win32_Service | Where-Object { $_.PathName -inotmatch "`"" -and $_.PathName -inotmatch ":\\Windows\\" -and ($_.StartMode -eq "Auto" -or $_.StartMode -eq "Manual") -and ($_.State -eq "Running" -or $_.State -eq "Stopped") }; if ($($ss | Measure).Count -lt 1) { Write-Host "`nNo unquoted service paths were found"; } else { $ss | Sort-Object -Property ProcessId, Name | Format-List -Property ProcessId, Name, DisplayName, PathName, StartName, StartMode, State; $n = $(Read-Host -Prompt "Enter service name").Trim(); Write-Host ""; if ($n.Length -lt 1) { Write-Host "Service name is rquired"; } else { $e = $false; foreach ($s in $ss) { if ($s.Name -eq $n) { $e = $true; break; } } if ($e) { Write-Host "[1] Start `n[2] Stop `n[3] Restart `n------------"; $c = $(Read-Host -Prompt "Your choice").Trim(); Write-Host ""; if ($c -eq "1" -or $c -eq "2" -or $c -eq "3") { $i = Get-Service -Name $s.Name; if ($c -eq "2" -or $c -eq "3") { if ($i.Status -eq "Stopped") { Write-Host "Service is not running"; } elseif ($s.StopService().ReturnValue -ne 0) { Write-Host "Cannot stop the service"; } else { do { Start-Sleep -Milliseconds 200; $i.Refresh(); } while ($i.Status -ne "Stopped"); Write-Host "Service has been stopped successfully"; } } if ($c -eq "3") { Write-Host ""; } if ($c -eq "1" -or $c -eq "3") { if ($i.Status -eq "Running") { Write-Host "Service is already running"; } elseif ($s.StartService().ReturnValue -ne 0) { Write-Host "Cannot start the service"; } else { do { Start-Sleep -Milliseconds 200; $i.Refresh(); } while ($i.Status -ne "Running"); Write-Host "Service has been started successfully"; } } } else { Write-Host "Invalid choice"; } } else { Write-Host "Service does not exists"; } } } } catch { Write-Host $_.Exception.InnerException.Message; } finally { if ($ss -ne $null) { $ss.Dispose(); } if ($i -ne $null) { $i.Close(); $i.Dispose(); } }