I'm new to PowerShell. I want to create a script that will Stop a running service then start it back up and display the status of the service but having trouble combining them. Currently, I was able to use Get service to start and stop the service and as well as checking the status of it individually. Is there any way to combine them into one script. Ideally, a script that will check if the service is on or off and do an if/else function. (such as if the service is off, have it stay off and if its on turn if off etc)
$servers = Get-Content -Path "C:\Scripts\Servers.txt
$services = Get-COntent -Path "C:\Scripts\Services.txt
foreach($server in $servers){
foreach($service in $services){
(Get-Service -computername $Servers -name $service).Stop()
(Get-Service -computername $Servers -name $service).Start()
Write-Host (Get-Service -ComputerName S$Servers -Name $service).Status
}
}
.stop()
method or Start-Service
cmdlet. Untested
$servers = Get-Content -Path "C:\Scripts\Servers.txt"
$services = Get-COntent -Path "C:\Scripts\Services.txt"
foreach($server in $servers){
foreach($service in $services){
(Get-Service -computername $Server -name $service).Stop()
(Get-Service -computername $Server -name $service).Start()
Write-Host (Get-Service -ComputerName $Server -Name $service).Status
}
}
$servers = Get-Content -Path "C:\Scripts\Servers.txt"
$services = Get-COntent -Path "C:\Scripts\Services.txt"
foreach($server in $servers){
foreach($service in $services){
Get-Service -ComputerName $Server -Name $service | Stop-Service
Get-Service -ComputerName $Server -Name $service | Start-Service
Write-Host (Get-Service -ComputerName $Server -Name $service).Status
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.