josh187
josh187

Reputation: 65

Stop-Service won't stop

I am making a menu and one of the options is to stop or start a service based on it's display name.
When I want to stop it, it just starts the service again.

$Display = Read-Host -Prompt 'Please enter displayname: '
$Choice =  Read-Host -Prompt 'Would you like to start or stop the service'
If (($Choice = 'start')) {
    Start-Service -displayname $Display
    Write-Host $Display "Starting..." -ForegroundColor Green 
}
ElseIf (($Choice = 'stop')) { 
    Stop-Service -displayname $Display
    Write-Host $Display "Stopping..." -ForegroundColor Green
}

Upvotes: 2

Views: 153

Answers (1)

Mark Wragg
Mark Wragg

Reputation: 23355

The issue is in your if statements you are using = when you should be using -eq to test the equality.

Your code is setting the variable and so both statements are always true. E.g you should instead be doing:

If ($Choice -eq 'stop') {

Upvotes: 2

Related Questions