Skip to main content
Became Hot Network Question
added 2 characters in body
Source Link
toolic
  • 6.2k
  • 4
  • 20
  • 73

This is part of a larger script but, I feel like this part could be streamlined a bit. The code currently works but I feel like doing a Get-Process call twice is inefficient. Am I wrong?

# Variables:
$ServiceNames = @("HealthTLService","ThreatLockerService")

# Stop & disable some services - if they're running
ForEach ($Service in $ServiceNames) {
    Get-Service | Where-Object {($_.Name -eq $Service)} | Stop-Service -Force -ErrorAction SilentlyContinue -Verbose
    Get-Service | Where-Object {($_.Name -eq $Service)} | Set-Service -StartupType Disabled -ErrorAction SilentlyContinue -Verbose
}
```

This is part of a larger script but, I feel like this part could be streamlined a bit. The code currently works but I feel like doing a Get-Process call twice is inefficient. Am I wrong?

# Variables:
$ServiceNames = @("HealthTLService","ThreatLockerService")

# Stop & disable some services - if they're running
ForEach ($Service in $ServiceNames) {
    Get-Service | Where-Object {($_.Name -eq $Service)} | Stop-Service -Force -ErrorAction SilentlyContinue -Verbose
    Get-Service | Where-Object {($_.Name -eq $Service)} | Set-Service -StartupType Disabled -ErrorAction SilentlyContinue -Verbose
}
```

This is part of a larger script but, I feel like this part could be streamlined a bit. The code currently works but I feel like doing a Get-Process call twice is inefficient. Am I wrong?

# Variables:
$ServiceNames = @("HealthTLService","ThreatLockerService")

# Stop & disable some services - if they're running
ForEach ($Service in $ServiceNames) {
    Get-Service | Where-Object {($_.Name -eq $Service)} | Stop-Service -Force -ErrorAction SilentlyContinue -Verbose
    Get-Service | Where-Object {($_.Name -eq $Service)} | Set-Service -StartupType Disabled -ErrorAction SilentlyContinue -Verbose
}
Source Link

Powershell - Trying to streamline a Get-Service Command

This is part of a larger script but, I feel like this part could be streamlined a bit. The code currently works but I feel like doing a Get-Process call twice is inefficient. Am I wrong?

# Variables:
$ServiceNames = @("HealthTLService","ThreatLockerService")

# Stop & disable some services - if they're running
ForEach ($Service in $ServiceNames) {
    Get-Service | Where-Object {($_.Name -eq $Service)} | Stop-Service -Force -ErrorAction SilentlyContinue -Verbose
    Get-Service | Where-Object {($_.Name -eq $Service)} | Set-Service -StartupType Disabled -ErrorAction SilentlyContinue -Verbose
}
```