2

我是这些很棒的 Power shell 世界的新手。我的脚本有问题,非常感谢您的帮助。

我有一个脚本“cmd4.ps1”需要运行另一个脚本“Transfer.ps1”,该脚本需要接收 3 个字符串参数,并且它需要作为不同于“cmd4.ps1”的其他进程运行。

cmd4.ps1:

$Script="-File """+$LocalDir+"\Remote\Transfer.ps1"" http://"+$ServerIP+"/Upload/"+$FileName+" "+$ServerIP+" "+$LocalIP

启动进程 powershell.exe -ArgumentList $Script

弹出后,$Script硬币的值类似于

-文件“c:\temp re\Remote\Transfer.ps1”http://10.1.1.1/Upload/file.txt 10.1.1.1 10.1.1.10

包含使用-File参数运行Powershell.exe脚本的语法,以及Transfer.ps1需要的三个参数("http://10.1.1.1/Upload/file.txt", 10.1.1.1, 10.1.1.10)。

当我在 PowerShell ISE 中编写这些指令时,我可以看到每个值都是正确的,并且 PowerShell.exe 以正确的值执行,一切正常!但是如果我将这些指令放在“cmd4.ps1”脚本中,它就不起作用了,我意味着参数不正确,因为我可以看到它启动 powershell 但它永远不会结束。

4

2 回答 2

1

-ArgumentList期望一个字符串参数数组而不是单个字符串。试试这个:

$ScriptArgs = @(
    '-File'
    "$LocalDir\Remote\Transfer.ps1"
    "http://$ServerIP/Upload/$FileName $ServerIP $LocalIP"
)

Start-Process powershell.exe -ArgumentList $ScriptArgs

请注意,您可以简化 args 的字符串构造,如上所示。

于 2010-11-03T19:31:46.167 回答
0

为什么不把它放在 cmd4.ps1 中呢?

& "c:\temp re\Remote\Transfer.ps1" "http://10.1.1.1/Upload/file.txt" "10.1.1.1" "10.1.1.10"
于 2010-11-08T06:43:13.753 回答