将参数从批处理文件传递到PowerShell脚本 [英] Pass parameter from a batch file to a PowerShell script

查看:103
本文介绍了将参数从批处理文件传递到PowerShell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的批处理文件中,我这样调用PowerShell脚本:

In my batch file, I call the PowerShell script like this:

powershell.exe "& "G:\Karan\PowerShell_Scripts\START_DEV.ps1"

现在,我想将字符串参数传递给START_DEV.ps1.假设参数为w=Dev.

Now, I want to pass a string parameter to START_DEV.ps1. Let's say the parameter is w=Dev.

我该怎么做?

推荐答案

假设您要从批处理文件中传递字符串Dev作为参数:

Let's say you would like to pass the string Dev as a parameter, from your batch file:

powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 Dev"

放入您的Powershell脚本头:

put inside your powershell script head:

$w = $args[0]       # $w would be set to "Dev"

如果您要使用内置变量$args,则使用此选项.否则:

This if you want to use the built-in variable $args. Otherwise:

 powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 -Environment \"Dev\""

并在您的Powershell脚本头中:

and inside your powershell script head:

param([string]$Environment)

如果您想要命名参数,则此.

This if you want a named parameter.

您可能还对返回错误级别感兴趣:

You might also be interested in returning the error level:

powershell -command "G:\Karan\PowerShell_Scripts\START_DEV.ps1 Dev; exit $LASTEXITCODE"

该错误级别将在批处理文件内显示为%errorlevel%.

The error level will be available inside the batch file as %errorlevel%.

这篇关于将参数从批处理文件传递到PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆