I am very new to Powershell script.
I am trying to run a powershell script from a cmd
powershell.exe Set-ExecutionPolicy Unrestricted
powershell.exe .\startup.ps1
I need two lines within powershell script.
%WINDIR%\system32\inetsrv\appcmd.exe add apppool /name:"VCPool" /managedRuntimeVersion:"v4.0" /managedPipelineMode:"Integrated"
%WINDIR%\system32\inetsrv\appcmd.exe set app "WebRole_IN_0_VC/" /applicationPool:"VCPool"
Can I simply write it like this within the ps1 file?
& %WINDIR%\system32\inetsrv\appcmd.exe add apppool /name:"VCPool" /managedRuntimeVersion:"v4.0" /managedPipelineMode:"Integrated"
Many Thanks
Short answer, including a question: Why the hell does that need to be a PowerShell script?
You can simply create a batch file containing
and run that directly instead of trying to figure out execution policies, etc.
Furthermore,
appcmdshould probably be in yourPATH, so you can run it directly without needing to specify the full path to the program.Longer answer, actually using PowerShell: There are two problems here.
You want to run a PowerShell script without having the appropriate execution policy set. This can be done with
You need to adjust environment variable usage within PowerShell scripts, as
%is not used to expand environment variables there. So you actually needNote also that you need an ampersand (
&) before each line as a variable name in the beginning of a line switches into expression mode while you want to run a command, therefore needing command mode.Furthermore quoted arguments can be a bit of a pain in PowerShell. PowerShell tries quoting arguments when necessary and it’s not always obvious when things go wrong what actually comes out on the other end. In this case the easiest way is to not quote the arguments in any way whcih ensures that they come out correctly:
However, if
appcmdactually needs the quotes around the argument after a colon, then you need to quote the whole argument with single quotes and add the double quotes back in: