Powershell stop service code improvement

Dear expert

Below code works fine, it stops win services and disable them, now I want to add line:
Get-Service | Where-Object {$_.Status -eq "Running"}

Before the Param, to show a output for user which services name is running and stop those.

However I tried add the line on the top of the script, the powershell complain about: [CmdletBinding()] and param.
Anyone has any idea? Thanks

[CmdletBinding()]     
param(            
 [string[]]$ComputerName = $env:ComputerName,            
 [parameter(Mandatory=$true)]            
 [string[]]$ServiceName            
)  
$Principal = New-Object -TypeName System.Security.Principal.WindowsPrincipal -ArgumentList ([System.Security.Principal.WindowsIdentity]::GetCurrent())
If (-not $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
	Write-Host "Not running elevated; restarting '$($MyInvocation.MyCommand.Path)' elevated ..." -ForegroundColor Yellow
	$ArgumentList = '-NoExit', '-Command', "& '$($MyInvocation.MyCommand.Path)'"
	$Argumentlist += $PSBoundParameters.GetEnumerator() | ForEach-Object {"-$($_.Key) '$($_.Value -join "', '")'"}
	Start-Process -Verb RunAs -FilePath 'powershell.exe' -ArgumentList $ArgumentList
	Write-Host "Non-elevated script ends now." -ForegroundColor Yellow
	Exit
}
Write-Host "Running elevated ..." -ForegroundColor Green  

 foreach($Computer in $ComputerName) {            
 Write-Host "Working on $Computer"            
 if(!(Test-Connection -ComputerName $Computer -Count 1 -quiet)) {            
  Write-Warning "$computer : Offline"            
  Continue            
 }            
             
 foreach($service in $ServiceName) {            
  try {            
   $ServiceObject = Get-WMIObject -Class Win32_Service -ComputerName $Computer -Filter "Name='$service'" -EA Stop            
   if(!$ServiceObject) {            
    Write-Warning "$Computer : No service found with the name $service"            
    Continue            
   }            
   if($ServiceObject.StartMode -eq "Disabled") {            
    Write-Warning "$Computer : Service with the name $service already in disabled state"            
    Continue            
   }            
               
   Set-Service -ComputerName $Computer -Name $service -EA Stop -StartMode Disabled            
   Write-Host "$Computer : Successfully disabled the service $service. Trying to stop it"            
   if($ServiceObject.Status -eq "Running") {            
    Write-Warning "$Computer : $service already in stopped state"            
    Continue            
   }            
   $retval = $ServiceObject.StopService()            
            
   if($retval.ReturnValue -ne 0) {            
    Write-Warning "$Computer : Failed to stop service. Return value is $($retval.ReturnValue)"            
    Continue            
   }            
               
   Write-Host "$Computer : Stopped service successfully"            
               
  } catch {            
   Write-Warning "$computer : Failed to query $service. Details : $_"            
   Continue            
  }            
            
 }            
         
}

  

Open in new window

LVL 1
WeTiAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Try this then:
$Principal = New-Object -TypeName System.Security.Principal.WindowsPrincipal -ArgumentList ([System.Security.Principal.WindowsIdentity]::GetCurrent())
If (-not $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
	Write-Host "Not running elevated; restarting '$($MyInvocation.MyCommand.Path)' elevated ..." -ForegroundColor Yellow
	$ArgumentList = '-NoExit', '-Command', "& '$($MyInvocation.MyCommand.Path)'"
	$Argumentlist += $PSBoundParameters.GetEnumerator() | ForEach-Object {"-$($_.Key) '$($_.Value -join "', '")'"}
	Start-Process -Verb RunAs -FilePath 'powershell.exe' -ArgumentList $ArgumentList
	Write-Host "Non-elevated script ends now." -ForegroundColor Yellow
	Exit
}
Write-Host "Running elevated ..." -ForegroundColor Green

$ComputerName = Read-Host "Computer name(s) (comma separated list accepted) [$($ENV:ComputerName)]"
If (-not $ComputerName) {
	$ComputerName = $ENV:ComputerName
}
$serviceFilter = Read-Host "Service name filter (wildcard * accepted)"
If (-not $serviceFilter) {
	$serviceFilter = '*'
}
ForEach ($computer in $ComputerName.Split(', ', [StringSplitOptions]::RemoveEmptyEntries)) {
	Write-Host "Working on $($Computer)"
	If (($computer -ne $ENV:ComputerName) -and (-not (Test-Connection -ComputerName $computer -Count 1 -Quiet))) {
		Write-Warning "$($computer): Offline"
		Continue
	}
	Try {
		If (-not ($serviceList = Get-WmiObject -Class Win32_Service -ComputerName $computer -Filter "(State='Running') AND (Name LIKE '$($serviceFilter.Replace('*', '%'))')" -ErrorAction Stop)) {
			Write-Warning "$($computer): no running service found matching '$($serviceFilter)'."
			Continue
		}
	} Catch {
		Write-Warning "$($computer): Error retrieving services: $($_.Exception.Message)"
		Continue
	}
	If (-not ($selectedServices = $serviceList | Select-Object -Property Name, DisplayName, State, StartMode | Out-GridView -Title "Select service(s) to disable on $($computer)" -OutputMode Multiple)) {
		Write-Warning "$($computer): no services selected."
		Continue
	}
	ForEach ($service in ($selectedServices | Select-Object -ExpandProperty Name)) {
		Write-Host "    Working on service $($service)"
		Try {
			$serviceObject = $serviceList | Where-Object {$_.Name -eq $service}
			If ($serviceObject.StartMode -eq 'Disabled') {
				Write-Warning "$($computer): Service '$($service)' is already in disabled state"
				Continue
			}
			Set-Service -ComputerName $Computer -Name $service -EA Stop -StartMode Disabled
			Write-Host "    $($computer): Successfully disabled the service $($service). Trying to stop it."
			If ($serviceObject.State -eq "Stopped") {
				Write-Warning "$($computer): Service '$($service)' is already in stopped state"
				Continue
			}
			$retval = $serviceObject.StopService()
			If ($retval.ReturnValue -ne 0) {
				Write-Warning "$($computer): Service '$($service)' could not be stopped. Return value is $($retval.ReturnValue)"
				Continue
			}
			Write-Host "    $($computer): Service '$($service)' stopped successfully"
		} Catch {
			Write-Warning "$($computer): Error configuring service '$($service)': $($_.Exception.Message)"
			Continue
		}
	}
}

Open in new window

0
 
oBdACommented:
Depends mostly on what it is exactly you want to do.
You should decide whether you want a script for interactive use (which is what you seem to want) and a script used for a parameterized call.
Listing the services for selection doesn't really fit together with the nested loop over computers and services - you'd have to query again for each server in the list, because the services and their state may be different for each server.
So what is the intended purpose of this script - interactive use, with the user being explicitly queried for the computer list and the services, or rather parameterized usage from the PS console?
0
 
WeTiAuthor Commented:
Hi again oBdA, well I would like to see all running services and stop/disable those I choose, the reason I do this is to easily stop services than go through the services.msc and right click on the services and stop and go to  Properties and click on the disable... I don't really want to loop anything, just for checking if service is enable or disable, if its enable then disable it and stop it.
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
WeTiAuthor Commented:
Many error:
The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command
name, a script block, or a CommandInfo object.
At line:1 char:3
+ & ''
+   ~~
    + CategoryInfo          : InvalidOperation: (:String) [], RuntimeException
    + FullyQualifiedErrorId : BadExpression

PS C:\WINDOWS\system32> The expression after '&' in a pipeline element produced an object that was not valid. It must re
sult in a command
The : The term 'The' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ The expression after '&' in a pipeline element produced an object tha ...
+ ~~~
    + CategoryInfo          : ObjectNotFound: (The:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\WINDOWS\system32> name, a script block, or a CommandInfo object.
At line:1 char:5
+ name, a script block, or a CommandInfo object.
+     ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument

Open in new window

0
 
oBdACommented:
You need to save this as .ps1 and start it as script, not paste it directly into a console window.
0
 
WeTiAuthor Commented:
Works, and wow... a service window pops... cool
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.