Invoke-expression and Stop-computer
-
25 июня 2012 г. 19:44
I am working on a shutdown script for the company. I want to build this script so that it is dynamic enough to run in test mode (using -whatif command) or live mode. When I try to run the command it works fine until i put the -confim:$False in the line I then get the folloing error.
Stop-Computer : Cannot convert 'System.String' to the type 'System.Management.Automation.SwitchParameter' required by parameter 'Confirm'.
How can I get around this Error.
Command that Works:
Invoke-Expression "Stop-Computer -ComputerName localhost $WhatIf -Force"
Command that Does not Work:
Invoke-Expression "Stop-Computer -ComputerName localhost $WhatIf -Force -confirm:$False"
I know that the default value for the confirm on stop-computer is $false. Other commands in the script it is true. I need to pass in False to them.
Все ответы
-
25 июня 2012 г. 19:59
-Confirm is a switch parameter, meaning you only have to specify it, with no arguments:
Stop-Computer -Confirm
...will cause a confirmation dialog to appear.
Grant Ward, a.k.a. Bigteddy
-
25 июня 2012 г. 20:05
But I do not want it to prompt. I want this to be an automated process that runs without intervention.
How can I get that to happen?
-
26 июня 2012 г. 6:14Just don't include the -Confirm parameter.
Grant Ward, a.k.a. Bigteddy
-
26 июня 2012 г. 8:38Модератор
First of all, not sure why you would use Invoke-Expression for that... Use splatting - that's preferred way to pass parameters to command dynamically. :)
And if you insist on using it - do it correctly. In your example boolean $False will be turned in to String 'False' (you used double quotes so variables will expand) - to avoid it, escape $:
iex "Stop-Computer -Confirm:`$False"
- Помечено в качестве ответа Bartek BielawskiModerator 26 июня 2012 г. 8:38

