Powershell参数

时间:2014-10-23 22:07:45

标签: url powershell escaping character

我是powershell的极端新手。我有一个脚本,我想传递一个URL作为参数。 url运行PHP进程,创建并下载PDF文件,然后脚本打印pdf,然后删除pdf。我无法获得URL parm。

以下是我的剧本

$w=$args[0] 

Start $w

$Directory = "C:\Users\pslessor\downloads\"

Get-ChildItem -path $Directory -recurse -include *.pdf | ForEach-Object {Start-Process -FilePath $_.fullname -Verb Print -PassThru | %{sleep 5;$_} | kill }

Remove-Item C:\Users\pslessor\downloads\* -include *.PDF

此脚本由批处理文件PrintPl.bat

执行
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%PrintPl.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' %1";

我正在测试testprint.bat

C:\Wget\PrintPl "https://partners.wayfair.com/print_shipping_docs.php?Print=1&printPackingSlips=1&PackingSlipPOs=CS287851107"

网址是一个字符串,这个编辑器强制换行.php?

我得到的错误是 字符串开头:

At Line:1 Char:25
+ & 'C:\Wget\Printpl.ps1'   <<<< 'https://partners.wayfair.com/print_shipping_docs.php?print;
is missing the terminator: '.
At line:1 Char:85 
+ & 'C:\Wget\PrintPl.psl' 'https://partners.wayfair.com/print_shipping_docs.php
?print; <<<<

    + CategoryInfo      :ParserError: <https://partner...docs.php?print;:
String> [], ParentContainsErrorRecordException
  + FullyqualifiedErrorid : TerminatorExpectedAtEndOfString

'printPackingSlips' is not recognaized as internal or external command,
Operable program or batch file.
'PackingSlipPOs' is not recognized as an internal or external command,
operable program or batch File

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为当你传递参数时,命令解释器会扩展你的变量并看到:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\Wget\PrintPl.ps1' https://partners.wayfair.com/print_shipping_docs.php?    Print=1&printPackingSlips=1&PackingSlipPOs=CS287851107"

因此它尝试执行的命令是C:\ Wget \ PrintPl.ps1,并且它假定下一步是参数。由于它传递的内容有一个空格,并没有用引号或双引号括起来,因此它假定它是多个参数。它将其视为:

执行此脚本:C:\ Wget \ PrintPl.ps1
使用以下参数:

  • $参数数量[0] =?
  • $参数数量[1] =打印= 1&安培; printPackingSlips = 1&安培; PackingSlipPOs = CS287851107

要阻止这种情况发生,您还需要将URL括在引号中,因此您的命令应如下所示:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%' '%1'"

编辑:好的,这对您的案例没有用处。所以我们要稍微改变一下。而不是-Command我建议你使用-File。因此,批处理文件中的Powershell执行行将如下所示:

PowerShell -NoProfile -ExecutionPolicy Bypass -File "%PowerShellScriptPath%" %*

您应该能够像以前一样运行批处理文件。我非常有信心能为你效劳。