Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass multiple string parameters to a PowerShell script?

I am trying to do some string concatenation/formatting, but it's putting all the parameters into the first placeholder.

Code

function CreateAppPoolScript([string]$AppPoolName, [string]$AppPoolUser, [string]$AppPoolPass) {     # Command to create an IIS application pool     $AppPoolScript = "cscript adsutil.vbs CREATE ""w3svc/AppPools/$AppPoolName"" IIsApplicationPool`n"     $AppPoolScript += "cscript adsutil.vbs SET ""w3svc/AppPools/$AppPoolName/WamUserName"" ""$AppPoolUser""`n"     $AppPoolScript += "cscript adsutil.vbs SET ""w3svc/AppPools/$AppPoolName/WamUserPass"" ""$AppPoolPass""`n"     $AppPoolScript += "cscript adsutil.vbs SET ""w3svc/AppPools/$AppPoolName/AppPoolIdentityType"" 3"      return $AppPoolScript } $s = CreateAppPoolScript("name", "user", "pass") write-host $s 

Output

cscript adsutil.vbs CREATE "w3svc/AppPools/name user pass" IIsApplicationPool cscript adsutil.vbs SET "w3svc/AppPools/name user pass/WamUserName" "" cscript adsutil.vbs SET "w3svc/AppPools/name user pass/WamUserPass" "" cscript adsutil.vbs SET "w3svc/AppPools/name user pass/AppPoolIdentityType" 3 
like image 342
Brian Lyttle Avatar asked Aug 22 '08 16:08

Brian Lyttle


People also ask

How do you pass multiple parameters in PowerShell?

Refer below PowerShell script for the above problem statement to pass multiple parameters to function in PowerShell. Write-Host $TempFile "file already exists!" Write-Host -f Green $TempFile "file created successfully!" Write-Host -f Green $FolderName "folder created successfully!"

How do you pass parameters to a function in PowerShell?

You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don't need to pass the argument because the variable is itself a Public and can be accessible inside the function.

How do you pass multiple parameters to a function?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.


2 Answers

Lose the parentheses and commas.

Calling your function as:

$s = CreateAppPoolScript "name" "user" "pass" 

gives:

cscript adsutil.vbs CREATE "w3svc/AppPools/name" IIsApplicationPool cscript adsutil.vbs SET "w3svc/AppPools/name/WamUserName" "user" cscript adsutil.vbs SET "w3svc/AppPools/name/WamUserPass" "pass" cscript adsutil.vbs SET "w3svc/AppPools/name/AppPoolIdentityType" 3 
like image 169
Paul Roub Avatar answered Sep 20 '22 21:09

Paul Roub


By the way, using a PowerShell here-string might make your function a little easier to read as well, since you won't need to double up all the "-marks:

function CreateAppPoolScript([string]$AppPoolName, [string]$AppPoolUser, [string]$AppPoolPass) {   # Command to create an IIS application pool   return @" cscript adsutil.vbs CREATE "w3svc/AppPools/$AppPoolName" IIsApplicationPool cscript adsutil.vbs SET "w3svc/AppPools/$AppPoolName/WamUserName" "$AppPoolUser" cscript adsutil.vbs SET "w3svc/AppPools/$AppPoolName/WamUserPass" "$AppPoolPass" cscript adsutil.vbs SET "w3svc/AppPools/$AppPoolName/AppPoolIdentityType" 3 "@ } 
like image 34
Emperor XLII Avatar answered Sep 20 '22 21:09

Emperor XLII