I have a Batch file which will call a Powershell Script :
我有一个批处理文件,它将调用Powershell脚本:
BATCH FILE : @ECHO OFF powershell ..\PowerShellScript.ps1
批处理文件:@ECHO OFF powershell .. \ PowerShellScript.ps1
The powershell script in turn has a function which expects a parameter :
powershell脚本又有一个需要参数的函数:
POWERSHELL SCRIPT:
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
Lets say i have a value : VALUE1 which needs to be passed from the batch File while calling the PowerShellScript.ps1, how do I pass it to the function PSFunction so that my output is VALUE1?
假设我有一个值:VALUE1需要在调用PowerShellScript.ps1时从批处理文件传递,如何将其传递给函数PSFunction以使我的输出为VALUE1?
4 个解决方案
#1
modify your script to look like the following
将脚本修改为如下所示
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
PSFunction $args[0]
and from the batch file, it would look like
从批处理文件中,它看起来像
powershell ..\PowerShellScript.ps1 VALUE1
#2
Use the -Command switch to tell powershell.exe to interpret a string as if it were typed at a PowerShell prompt. In your case, the string could dot-source PowerShellScript.ps1 (to import it into the new powershell.exe environment) and then call PSFunction with VALUE1 as a parameter:
使用-Command开关告诉powershell.exe解释字符串,就好像它是在PowerShell提示符下键入的一样。在您的情况下,字符串可以点源PowerShellScript.ps1(将其导入新的powershell.exe环境),然后使用VALUE1作为参数调用PSFunction:
set VALUE1=Hello World
powershell.exe -command ". ..\PowerShellScript.ps1; PSFunction '%VALUE1%'"
#3
Defining a function in a Powershell script does not execute the function. If you want that, then your script might need to look like that:
在Powershell脚本中定义函数不会执行该函数。如果你想要,那么你的脚本可能需要看起来像这样:
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
PSFunction "some string"
From within the script you still have a dynamic $args
variable that gets any parameters you passed to the script. So
在脚本中,您仍然有一个动态$ args变量,它获取您传递给脚本的任何参数。所以
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
PSFunction $args[0]
will pass the first parameter you gave on the commandline on to the function.
将您在命令行上给出的第一个参数传递给该函数。
#4
It seems to me that you simple should choose what to use - batch files or PowerShell:) PowerShell is more powerful, but batch files are easier to create (especially with Dr.Batcher) and can be run anywhere.
在我看来,你应该选择使用什么 - 批处理文件或PowerShell :) PowerShell功能更强大,但批处理文件更容易创建(特别是使用Dr.Batcher)并且可以在任何地方运行。
#1
modify your script to look like the following
将脚本修改为如下所示
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
PSFunction $args[0]
and from the batch file, it would look like
从批处理文件中,它看起来像
powershell ..\PowerShellScript.ps1 VALUE1
#2
Use the -Command switch to tell powershell.exe to interpret a string as if it were typed at a PowerShell prompt. In your case, the string could dot-source PowerShellScript.ps1 (to import it into the new powershell.exe environment) and then call PSFunction with VALUE1 as a parameter:
使用-Command开关告诉powershell.exe解释字符串,就好像它是在PowerShell提示符下键入的一样。在您的情况下,字符串可以点源PowerShellScript.ps1(将其导入新的powershell.exe环境),然后使用VALUE1作为参数调用PSFunction:
set VALUE1=Hello World
powershell.exe -command ". ..\PowerShellScript.ps1; PSFunction '%VALUE1%'"
#3
Defining a function in a Powershell script does not execute the function. If you want that, then your script might need to look like that:
在Powershell脚本中定义函数不会执行该函数。如果你想要,那么你的脚本可能需要看起来像这样:
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
PSFunction "some string"
From within the script you still have a dynamic $args
variable that gets any parameters you passed to the script. So
在脚本中,您仍然有一个动态$ args变量,它获取您传递给脚本的任何参数。所以
function PSFunction([string]$Parameter1)
{
Write-Host $Parameter1
}
PSFunction $args[0]
will pass the first parameter you gave on the commandline on to the function.
将您在命令行上给出的第一个参数传递给该函数。
#4
It seems to me that you simple should choose what to use - batch files or PowerShell:) PowerShell is more powerful, but batch files are easier to create (especially with Dr.Batcher) and can be run anywhere.
在我看来,你应该选择使用什么 - 批处理文件或PowerShell :) PowerShell功能更强大,但批处理文件更容易创建(特别是使用Dr.Batcher)并且可以在任何地方运行。