Name F
Name F

Reputation: 21

Set powershell variable in ps1 script by a paramater

How to set Powershell variable into ps1 script before the execution of the script

example:

application execution path: powershell.exe -ExecutionPolicy Bypass -File "C:/script.ps1"

paramater: $WhichSite= "StackOverflow"

ps1 contains: curl -UseBasicParsing "http://www.$WhichSite.com"

I tried the example above to set the $WhichSite = "stackoverflow"

But it did not work

result expected of executing powershell.exe -ExecutionPolicy Bypass -File "C:/script.ps1": curl -UseBasicParsing "http://www.stackoverflow.com"

Upvotes: 0

Views: 481

Answers (1)

tomarbs
tomarbs

Reputation: 11

If I understand your question correctly, the best thing would be to pass a parameter to the script and then call the curl function from within. You might consider something like this inside of script.ps1:

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [System.String]
        $WhichSite
    )
    
    Invoke-WebRequest -UseBasicParsing "Http://$WhichSite.com"

Then, to run the script from a PowerShell window, you can do:

./script.ps1 -WhichSite "stackoverflow"

You can set the execution policy for PowerShell for the current session by running:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

So that you don't have to add it in front of your script execution everytime time.

Upvotes: 1

Related Questions