Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write this in Powershell script?

Tags:

powershell

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

like image 735
Houman Avatar asked Nov 27 '11 17:11

Houman


People also ask

How do you use special characters in PowerShell?

Unicode character (`u{x}) This special character was added in PowerShell 6.0. The Unicode escape sequence ( `u{x} ) allows you to specify any Unicode character by the hexadecimal representation of its code point.

What is mean by $_ in PowerShell?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.

What does $() mean in PowerShell?

Subexpression operator $( ) For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression. PowerShell Copy.


1 Answers

Short answer, including a question: Why the hell does that need to be a PowerShell script?

You can simply create a batch file containing

%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"

and run that directly instead of trying to figure out execution policies, etc.

Furthermore, appcmd should probably be in your PATH, 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.

  1. You want to run a PowerShell script without having the appropriate execution policy set. This can be done with

    powershell -ExecutionPolicy Unrestricted -File myscript.ps1
    
  2. You need to adjust environment variable usage within PowerShell scripts, as % is not used to expand environment variables there. So you actually need

    & $Env:WinDir\system32\inetsrv\appcmd.exe add apppool /name:VCPool /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated
    & $Env:WinDir\system32\inetsrv\appcmd.exe set app WebRole_IN_0_VC/ /applicationPool:VCPool
    

    Note 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:

    PS Home:\> args add apppool /name:VCPool /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated
    argv[1] = add
    argv[2] = apppool
    argv[3] = /name:VCPool
    argv[4] = /managedRuntimeVersion:v4.0
    argv[5] = /managedPipelineMode:Integrated
    
    PS Home:\> args set app WebRole_IN_0_VC/ /applicationPool:VCPool
    argv[1] = set
    argv[2] = app
    argv[3] = WebRole_IN_0_VC/
    argv[4] = /applicationPool:VCPool
    

    However, if appcmd actually 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:

    & $Env:WinDir\system32\inetsrv\appcmd.exe set app WebRole_IN_0_VC/ '/applicationPool:"VCPool"'
    
like image 60
Joey Avatar answered Oct 05 '22 00:10

Joey