Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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 运行带有启动进程的.bat文件时_Powershell_Batch File_Cmd - Fatal编程技术网

Powershell 运行带有启动进程的.bat文件时

Powershell 运行带有启动进程的.bat文件时,powershell,batch-file,cmd,Powershell,Batch File,Cmd,当我直接运行这个.bat文件时,它会执行它应该执行的操作。当我使用start process时,会打开一个黑色窗口,然后立即关闭 Start-Process \\server01\f$\filelocation\myfile.bat -Verbose -NoNewWindow 知道它为什么不运行吗 下面是批处理脚本中的内容 MediSpan.Install.exe /Autostart:DEV_Medispan_Data 扩展我的评论: # Simple batch file Get-Con

当我直接运行这个.bat文件时,它会执行它应该执行的操作。当我使用start process时,会打开一个黑色窗口,然后立即关闭

Start-Process \\server01\f$\filelocation\myfile.bat -Verbose -NoNewWindow
知道它为什么不运行吗

下面是批处理脚本中的内容

MediSpan.Install.exe /Autostart:DEV_Medispan_Data
扩展我的评论:

# Simple batch file
Get-Content -Path 'D:\Temp\abc.bat'
echo off
cls
echo Hello

# Run the .bat file
D:\Temp\abc.bat
# Results - no window is seen unless you are really paying attention since it will flash very fast
<#
Hello
#>

# Using the call operator vs Start-Process
& 'D:\Temp\abc.bat'
# Results - same window response as above
<#
Hello
#>


Start-Process -FilePath ' D:\Temp\abc.bat' -Wait
# Results  - same window response as above
<#
no results are displayed
#>

Start-Process -FilePath ' D:\Temp\abc.bat' -PassThru
# Results  - same window response as above - But this shows it ran
<#
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    19       3     1536       2524       0.00  10160  11 cmd
#>

$A = Start-Process -FilePath ' D:\Temp\abc.bat'-PassThru;$A.ExitCode
$A
# Results  - same window response as above - But this shows it ran
<#
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
            0        0          0       0.02  19128
#> 
通过TechNet链接从PowerShell调用和3rdP可执行文件

($p = Start-Process -FilePath 'F:\SWDATINSCD_AN_19000101\MediSpan.Install.exe' -ArgumentList '/Autostart:DEV_Medispan_Data' -wait -NoNewWindow -PassThru)
$p.HasExited
$p.ExitCode
  • 接线员&
  • 原因:用于将字符串视为单个命令。对交易有用 有空格

    在PowerShell V2.0中,如果正在运行7z.exe(7-Zip.exe)或另一个以数字开头的>命令,则必须使用命令调用运算符&

    PowerShell V3.0解析器现在做得更智能了,在这种情况下,您不再需要&了

    详细信息:运行命令、脚本或脚本块。调用运算符(也称为“调用运算符”)允许您运行存储在变量中并由字符串表示的命令。因为调用运算符不解析命令,所以无法解释命令参数

    示例:

    当一个外部命令有很多参数或者参数或路径中有空格时,事情会变得棘手

    使用空格时,必须嵌套引号,结果并不总是清晰的

    在这种情况下,最好将所有内容分开,如下所示:

    $CMD  = 'SuperApp.exe'
    $arg1 = 'filename1'
    $arg2 = '-someswitch'
    $arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
    $arg4 = '-yetanotherswitch'
     
    & $CMD $arg1 $arg2 $arg3 $arg4
    
    或者像这样:

    $AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
    & 'SuperApp.exe' $AllArgs
    
    对于启动过程,按照相同的TechNet链接

    ($p = Start-Process -FilePath 'F:\SWDATINSCD_AN_19000101\MediSpan.Install.exe' -ArgumentList '/Autostart:DEV_Medispan_Data' -wait -NoNewWindow -PassThru)
    $p.HasExited
    $p.ExitCode
    

    myfile.bat
    中可能有错误,但您尚未向我们展示,因此我们和您一样陷入困境。此外,由于您已经很好地混淆了路径和文件名,我们只能推测-如果其中任何一个包含分隔符,则“引用完整文件名”,因此:
    “\\server01\f$\filelocation\myfile.bat”
    可能会有所帮助。从PowerShell运行外部命令/可执行文件时,它们必须正确格式化才能执行。直接运行批处理文件时没有错误。当它直接运行时,它完全按照它应该做的做。如果我删除-nonewindow,它会提示我运行批处理脚本,我点击ok,然后它就关闭了。我以前运行过其他类似的脚本,从未遇到过类似的问题,所以我认为这是我忘记做的事情。使用呼叫接线员是一个更好的选择。请参阅我的更新以了解您的用例。然而,若你们设置了启动过程,你们可以看到我的更新。请参阅我的第二次更新。
    ($p = Start-Process -FilePath 'F:\SWDATINSCD_AN_19000101\MediSpan.Install.exe' -ArgumentList '/Autostart:DEV_Medispan_Data' -wait -NoNewWindow -PassThru)
    $p.HasExited
    $p.ExitCode