简体   繁体   English

将参数从批处理文件传递给powershell脚本

[英]Passing parameters to powershell script from batch file

I am calling a powershell script from a batch file, both in different locations. 我在批处理文件中调用powershell脚本,它们位于不同的位置。

I would like to pass the folder location for the powershell script file as well as the parameter, a user entered string in the batch file, from that batch file. 我想传递powershell脚本文件的文件夹位置以及该批处理文件中用户在批处理文件中输入的字符串参数。

powershell script: powershell脚本:

$url = "https://www.ons.gov.uk/generator?format=csv&uri=/economy/inflationandpriceindices/timeseries/chaw/mm23"
$output="sample.csv"
$start_time = Get-Date
$arg1=$args[0]
Invoke-WebRequest -Uri $url -OutFile $arg1\$output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

My Batch file : 我的批处理文件:

powershell.exe -ExecutionPolicy Bypass -file C:\temp\download-rpi.ps1 "\\drives\savehere"

You could have done all of this in a single language instead of using both Powershell and batch, but regardless, here is what you want 您可以使用单一语言完成所有这些操作,而不是同时使用Powershell和批处理,但无论如何,这都是您想要的

@echo off
if "%1"=="" (
    set /p "pspath=Enter the path to Powershell: "
    ) else set "pspath=%1"
if "%2"=="" (
    set /p "sharepath=Enter The share parameter: "
    ) else set "sharepath=%2"
    powershell.exe -ExecutionPolicy Bypass -file "%pspath% "%sharepath%"

How it works: 这个怎么运作:

you can either double click the file which will then prompt for the powershell path and sharepath 您可以双击该文件,然后提示您输入powershell路径和共享路径

OR 要么

run from cmdline and enter the variables after the batch command, which will use %1 %2 to set the variables. 从cmdline运行并在批处理命令后输入变量,这将使用%1 %2来设置变量。 Examples: 例子:

  1. Double Clicking batch: 双击批次:
Enter the path to Powershell: C:\Some Path\
Enter The share parameter: \\some\share

Result 结果

powershell.exe -ExecutionPolicy Bypass -file "C:\Some Path\" "\\some\share"
  1. Running from cmd.exe prompt 从cmd.exe提示符运行
C:\> BatchFileName.cmd "C:\Some Path\" "\\some\share"

Result 结果

powershell.exe -ExecutionPolicy Bypass -file "C:\Some Path\" "\\some\share"

In PowerShell, this is done using parameters : 在PowerShell中,这是使用参数完成的:

param(
    [string]$Path
)

$url        = "https://www.ons.gov.uk/generator?format=csv&uri=/economy/inflationandpriceindices/timeseries/chaw/mm23"
$output     = "sample.csv"
$start_time = Get-Date

Invoke-WebRequest -Uri $url -OutFile $Path\$output

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

An alternative is to use the automatic variable $MYINVOCATION to get similar behaviour to an $args array but I would not recommend that as you have no way of knowing what unbound parameters will be provided. 另一种方法是使用自动变量$ MYINVOCATION来获得与$args数组类似的行为,但我不建议这样做,因为你无法知道将提供什么未绑定的参数。

$url        = "https://www.ons.gov.uk/generator?format=csv&uri=/economy/inflationandpriceindices/timeseries/chaw/mm23"
$output     = "sample.csv"
$start_time = Get-Date
$Path       = $MYINVOCATION.UnboundArguments

Invoke-WebRequest -Uri $url -OutFile $Path\$output

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM