2013-09-27 219 views
4

我试图从运行对话框(将用作计划任务)运行PowerShell脚本,并且遇到麻烦传递参数。将参数传递给powershell脚本

该脚本将包含两个名为title和msg的参数。 该脚本位于:D:\Tasks Scripts\Powershell\script.ps1

这就是我想要做的事:

powershell.exe -noexit 'D:\Tasks Scripts\Powershell\script.ps1' -title 'Hello world' -msg 'This is a test message' 

但它无法在阅读的参数。

在powershell上运行.\script.ps1 -title 'Hello world' -msg 'This is a test message'工作正常。

+1

使用-command选中此SO回答-http://stackoverflow.com/questions/12691781/scheduled-task-for-powershell-script-with -string阵列面值ameter – Mitul

+0

如果您能告诉我们“它失败”的含义,这将有所帮助。如果有错误消息,请引用错误消息,或者告诉我们发生了什么以及您期望发生什么。 –

回答

3

使用-file的路径,你的脚本之前:

powershell.exe -noexit -file 'D:\Tasks Scripts\Powershell\script.ps1' etc... 
+0

这工作,谢谢! – desto

2

我通常运行cmd.exe的从PowerShell脚本,因为这是便携式 (作品外的即装即用别人的电脑,就像开发商人或客户): 无需担心Set-ExecutionPolicy或关联.ps1扩展名。

我创建.cmd扩展名(代替的.ps1)的文件,复制粘贴&短, 恒定代码来调用powershell.exe并传递文件的其余部分 到第一线(S)它。
传递参数非常棘手。我有常数代码 的多种变体,因为一般情况是痛苦的。

  1. 时不传递参数,.cmd文件看起来是这样的:

    @powershell -c ".(iex('{#'+(gc '%~f0' -raw)+'}'))" & goto :eof 
    # ...arbitrary PS code here... 
    write-host hello, world! 
    

    它使用powershell.exe的-Command说法。 Powershell将.cmd 文件作为文本读取,并将其放入ScriptBlock中,并将第一行注释掉, 并使用'。'进行评估。命令。 此外command line arguments 可以根据需要被加入到Powershell的调用(例如-ExecutionPolicy无限制, -Sta等)

    传递不包含空格或是“单引号”参数时
  2. (其是非 - 标准中的cmd.exe),一衬垫是这样的:可以使用

    @powershell -c ".(iex('{#'+(gc($argv0='%~f0') -raw)+'}'))" %* & goto :eof 
    
    write-host this is $argv0 arguments: "[$($args -join '] [')]" 
    

    param()声明为好,$args不是必须的。
    $argv0用于补偿丢失的$MyInvocation.PS*信息。
    例子:

    G:\>lala.cmd 
    this is G:\lala.cmd arguments: [] 
    
    G:\>lala.cmd "1 2" "3 4" 
    this is G:\lala.cmd arguments: [1] [2] [3] [4] 
    
    G:\>lala.cmd '1 2' '3 4' 
    this is G:\lala.cmd arguments: [1 2] [3 4] 
    
  3. 传递是“双引号”,但不包含 的&和参数时

    “字,我用两班轮,以取代所有的“”

    @echo off& set A= %*& set [email protected] -c "$argv0='%~f0';.(iex('{' 
    %B%+(gc $argv0|select -skip 2|out-string)+'}'))" %A:"='%&goto :eof 
    
    write-host this is $argv0 arguments: "[$($args -join '] [')]" 
    

    (注意空间在A= %*分配的 参数少的情况下非常重要的。)
    结果:

    G:\>lala.cmd 
    this is G:\lala.cmd arguments: [] 
    
    G:\>lala.cmd "1 2" "3 4" 
    this is G:\lala.cmd arguments: [1 2] [3 4] 
    
    G:\>lala.cmd '1 2' '3 4' 
    this is G:\lala.cmd arguments: [1 2] [3 4] 
    
  4. 最常见的情况是通过环境变量传递参数 因此Powershell的param()声明不起作用。在这种情况下, 论据预计是“双引号”,可能含有“或& (除.cmd文件本身的路径):

    ;@echo off & setlocal & set A=1& set ARGV0=%~f0 
    ;:loop 
    ;set /A A+=1& set ARG%A%=%1& shift& if defined ARG%A% goto :loop 
    ;powershell -c ".(iex('{',(gc '%ARGV0%'|?{$_ -notlike ';*'}),'}'|out-string))" 
    ;endlocal & goto :eof 
    for ($i,$arg=1,@(); test-path -li "env:ARG$i"; $i+=1) { $arg += iex("(`${env:ARG$i}).Trim('`"')") } 
    write-host this is $env:argv0 arguments: "[$($arg -join '] [')]" 
    write-host arg[5] is ($arg[5]|%{if($_){$_}else{'$null'}}) 
    

    (请注意,在第一线A=1&不能包含空格)
    结果:

    G:\>lala.cmd "a b" "c d" "e&f" 'g' "h^j" 
    this is G:\lala.cmd arguments: [a b] [c d] [e&f] ['g'] [h^j] 
    arg[5] is $null