Reputation: 61
I'm trying to stop an iis site using PS, but also trying to skip the annoying confirmation box
If I run the following script accepting the confirmation it will stop it just fine
SAPS PowerShell -Verb RunAs -ArgumentList "Stop-IISSite -Name 'website'"
However if I add the cmdlet to set the confirmation as false it does not give me any error or the confirmation, but it doesn't stop the site.
SAPS PowerShell -Verb RunAs -ArgumentList "Stop-IISSite -Name 'website' -Force -Confirm:$false"
Any idea why?
Upvotes: 6
Views: 4407
Reputation: 21418
The $false
is being expanded when you pass the string to ArgumentList
. Either use single quotes or escape the $
used in $false
so it doesn't get expanded until the child process executes:
# Use double-quotes and escape the $ if you have a variable that *does*
# need to be expanded in the current session executes,
# such as the -Name
"Stop-IISSite -Name $name -Force -Confirm:`$false"
# Otherwise single-quotes will prevent any variable expansion from
# the current session
'Stop-IISSite -Name ''website'' -Force -Confirm:$false'
The way you are doing it now causes the command to be rendered in the child PowerShell process as follows:
Stop-IISSite -Name $name -Force -Confirm:false
Note the missing $
after -Confirm:
, but forcibly setting a [switch]
state requires a [bool]
type. $false
is a [bool]
, but when rendered in a string like this it will be evaluated to the literal string false
.
I'm surprised you aren't getting an error though, since -Confirm:false
throws a type conversion error. This could be explained if you have $ErrorActionPreference
set to SilentlyContinue
or Ignore
elsewhere, but is more likely that the error is showing in the child process window quickly before the window closes.
This article explains variable expansion in strings in more detail if you want to know more.
Upvotes: 5
Reputation: 161
You can use Stop-Website -Name 'website', this will not ask for confirmation.
Upvotes: 3