Аргументы командной строки в PowerShell

Содержание
Введение
Пример
Проверить передан ли аргумент
Похожие статьи

Введение

В PowerShell скрипт, можно передавать аргументы из командной строки. В отличие от Bash где можно просто обращаться к аргументам по порядку ($1, $2 …) в PowerShell нужно заранее создать список параметров.

param ( [тип]$имя )

param ( [string]$name, [int]$name2 )

Передаются эти аргументы указаниме имени параметра через дефис

.\demo.ps1 -name value

Пример

Следующий скрипт может принимать три именованых аргумента. Два строковых: setup, test. Один целочисленный: clean.

param ( [string]$setup, [string]$test, [int]$clean ) if ($setup -eq "ui") { python -m pip install -r setup/ui/requirements.txt } elseif ($setup -eq "full") { python -m pip install -r setup/app/requirements.txt python -m pip install -r setup/ui/requirements.txt } if ($test -eq "collect") { python -m robot app/Tests/CollectDataFromUI.robot } elseif ($test -eq "output") { python -m pytest -vvv tools/ui/verify_output.py } if ($clean -eq 1) { Remove-Item -Path "tools\data\*.csv" -Recurse -Force }

./param_demo.ps1 -setup ui ./param_demo.ps1 -setup full ./param_demo.ps1 -test collect ./param_demo.ps1 -test output ./param_demo.ps1 -clean 1

param ( [string]$test, [string]$setup, [int]$clean, [string]$path ) function setup_test_env { If ($env:VIRTUAL_ENV -eq "${PWD}\venv") { Write-Host "venv from this dir is already activated" -f Yellow Write-Host $env:VIRTUAL_ENV Write-Host "${PWD}\venv" } ElseIf ($null -ne $env:VIRTUAL_ENV) { Write-Host "WARN: venv from another dir ${env:VIRTUAL_ENV} is already activated" -f Red Write-Host $env:VIRTUAL_ENV Write-Host "${PWD}\venv" } Else { python -m venv venv .\venv\Scripts\activate python -m pip install --upgrade pip python -m pip install -r .\requirements.txt } } if ($setup -eq "testenv") { setup_test_env } elseif ($setup -eq "ui") { python -m pip install -r ui/requirements.txt } elseif ($setup -eq "full") { setup_test_env python -m pip install -r ui/requirements.txt } elseif ($test -eq "collect") { python -m robot app/Tests/CollectDataFromUI.robot } elseif ($test -eq "output") { python -m pytest -vvv ui/verify_output.py } elseif ($test -eq "static") { python ui/utils.py } elseif ($test -eq "full") { python -m robot app/Tests/CollectDataFromUI.robot python ui/utils.py python -m pytest -vvv ui/verify_output.py } else { setup_test_env } if ($clean -eq 1) { Remove-Item -Path "app\tools\data\*.csv" -Recurse -Force } if ($path -eq "tesseract") { $Env:Path += ';C:/Program Files/Tesseract-OCR' }

Проверить передан ли аргумент

param ( [string]$Url ) if (-not $Url) { Write-Output "Parameter 'Url' was not set." } else { Write-Output "Parameter 'Url' is: $Url" }

.\demo.ps1 -Url testsetup.ru

Parameter 'Url' is: testsetup.ru

Если не передавать Url

… test_parameter …

.\demo.ps1

Parameter 'Url' was not set.

Article author: Andrei Olegovich

Похожие статьи
Windows
PowerShell
Alias
Calling REST API
Users
Network
Install
Files
Functions
Loops
ExecutionPolicy
if
GetType()
param

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: