Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在带有参数的UNC路径上运行批处理文件,并将输出捕获到不带社区扩展名的变量_Powershell_Batch File_Parameter Passing - Fatal编程技术网

Powershell 在带有参数的UNC路径上运行批处理文件,并将输出捕获到不带社区扩展名的变量

Powershell 在带有参数的UNC路径上运行批处理文件,并将输出捕获到不带社区扩展名的变量,powershell,batch-file,parameter-passing,Powershell,Batch File,Parameter Passing,我想运行如下命令: pushd \\myServer\share\scripts myBatchFile.bat param1 param2 "parameter 3" popd 只能通过powershell启动 注意:批处理文件的名称保存在一个变量中,每个参数也是如此 function Escape-StringForCmd($a) { if(($a -like '*"*') -or ($a -like '* *')) { ('"{0}"' -f ($a -r

我想运行如下命令:

pushd \\myServer\share\scripts
myBatchFile.bat param1 param2 "parameter 3"
popd
只能通过powershell启动

注意:批处理文件的名称保存在一个变量中,每个参数也是如此

function Escape-StringForCmd($a)
{
    if(($a -like '*"*') -or ($a -like '* *'))
    {
        ('"{0}"' -f ($a -replace '"','""'))
    }
    else
    {
        $a
    }
}

$batch = "myBatchFile.bat"
$p1 = "param1"
$p2 = "param2"
$p3 = "parameter 3"

$batch = Escape-StringForCmd($batch)
$p1 = Escape-StringForCmd($p1)
$p2 = Escape-StringForCmd($p2)
$p3 = Escape-StringForCmd($p3)

pushd \\myServer\share\scripts

cmd.exe /c $batch $p1 $p2 $p3
#above fails; no error returned; I think because cmd doesn't like the UNC path, so reverts to the system directory

Start-Process "cmd.exe" -ArgumentList "/c",$batch,$p1,$p2,$p3 -NoNewWindow -Wait -WorkingDirectory "\\myServer\share\scripts"
#above also fails; not sure why as looks healthy when running outside of ps1 file

popd
我还对捕获输出感兴趣——尽管目前批处理文件还没有运行,但我将首先关注这一点

我还没有尝试ProcessStartInfo解决方案(请参见下面的链接),因为它似乎
启动进程
,或者简单地说
cmd.exe/c
应该可以工作(当然,当我在ps1文件之外运行测试时,这已经工作了),但我很快就会尝试该方法


ProcessStartInfo解决方案:

有什么原因不能为此使用调用表达式吗

$MyCmd = "$batch $p1 $p2 $p3 *>&1"

$ReturnOutput = Invoke-Expression $MyCmd
*>&1
将所有来自StdErr和StdOut的输出放入输出流


使用@JNK的答案和下面的破解方法,我找到了一种让它工作的方法

$tempBatchName = ".\~myTempBatchFile.bat" #put this in a variable so we can easily amend if required

"
pushd \\myServer\share\scripts
$batch $p1 $p2 $p3
popd
" | out-file $tempBatchName -encoding ascii

$MyCmd = ("{0} *>&1" -f $tempBatchName)

$ReturnOutput = Invoke-Expression $MyCmd
$ReturnOutput | out-file ("{0}.log" -f $tempBatchName)
remove-item $tempBatchName

太棒了-我以前没见过
调用表达式
-谢谢。注意:为了完成这项工作,我还必须明确表示我想从本地目录执行批处理-即“\$batch”实际上-刚刚意识到我在本地批处理上进行测试-这仍然存在UNC路径问题-尽管它运行批处理脚本,但脚本不会在当前UNC路径上执行(即,来自powershell早期的pushd表达式)…很遗憾,我无法更改批处理文件的内容…还有其他想法吗?您不能将UNC路径放在批处理文件之前吗?是的,但遗憾的是,这不会影响工作目录(批处理文件引用同一目录中的其他文件)…有趣的是,Invoke Expression能够运行批处理;只有当批处理开始运行时,它才会切换到c:\windows目录。我还尝试了Invoke Expression的多行表达式,在调用批处理的任一侧执行pushd和popd;没有运气。修复了它-我只创建了一个带有3行的临时批处理文件,然后好的,哈克,但它能用。