Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.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
Windows 用于运行带有参数的exe文件的Powershell脚本_Windows_Powershell_Scripting_Exe - Fatal编程技术网

Windows 用于运行带有参数的exe文件的Powershell脚本

Windows 用于运行带有参数的exe文件的Powershell脚本,windows,powershell,scripting,exe,Windows,Powershell,Scripting,Exe,我需要脚本来运行带有参数的exe文件。 这就是我写的,如果有更好的方法的话 $Command=“\\Networkpath\Restart.exe” $Parms=“/t:21600/m:360/r/f” $Prms=$Parms.Split(“”) &“$Command”$Prms运行外部可执行文件时,您有两个选项 此方法实质上是将数组作为参数连接到可执行文件。这使您的参数列表更清晰,可以重新编写为: $params = @( '/t:21600' '/m:360'

我需要脚本来运行带有参数的exe文件。 这就是我写的,如果有更好的方法的话

$Command=“\\Networkpath\Restart.exe”
$Parms=“/t:21600/m:360/r/f”
$Prms=$Parms.Split(“”)

&“$Command”$Prms
运行外部可执行文件时,您有两个选项


此方法实质上是将数组作为参数连接到可执行文件。这使您的参数列表更清晰,可以重新编写为:

$params = @(
    '/t:21600'
    '/m:360'
    '/r'
    '/f'
)
这通常是我最喜欢的解决问题的方法


立即使用参数调用可执行文件 如果在参数、路径等中没有空格,则不一定需要有变量,甚至不需要

\\netpath\restart.exe /t:21600 /m:360 /r /f

这是我的第二次尝试,因为它让我能够更好地控制最终的过程。有时,可执行文件会产生子进程,并且您的呼叫操作员不会等到进程结束后再继续执行脚本。这个方法可以让你控制它

$startParams = @{
    'FilePath'     = '\\netpath\restart.exe'
    'ArgumentList' = '/t:21600', '/m:360', '/r', '/f'
    'Wait'         = $true
    'PassThru'     = $true
}
$proc = Start-Process @startParams
$proc.ExitCode

我所知道的最后一个方法是直接使用
进程
.NET类。如果我需要对流程进行更多控制,例如收集其输出,我会使用此方法:

try
{
    $proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
        'FileName'               = "\\netshare\restart.exe"
        'Arguments'              = '/t:21600 /m:360 /r /f'
        'CreateNoWindow'         = $true
        'UseShellExecute'        = $false
        'RedirectStandardOutput' = $true
    })
    $output = $proc.StandardOutput
    $output.ReadToEnd()
}
finally
{
    if ($null -ne $proc)
    {
        $proc.Dispose()
    }
    if ($null -ne $output)
    {
        $output.Dispose()
    }
}

我会使用
Start Process
,但您的示例也可以。您不需要
围绕
$Command
@Bill\u Stewart
”$($Command.ToString())”
:P@TheIncorrigible1 :-)
try
{
    $proc = [System.Diagnostics.Process]::Start([System.Diagnostics.ProcessStartInfo]@{
        'FileName'               = "\\netshare\restart.exe"
        'Arguments'              = '/t:21600 /m:360 /r /f'
        'CreateNoWindow'         = $true
        'UseShellExecute'        = $false
        'RedirectStandardOutput' = $true
    })
    $output = $proc.StandardOutput
    $output.ReadToEnd()
}
finally
{
    if ($null -ne $proc)
    {
        $proc.Dispose()
    }
    if ($null -ne $output)
    {
        $output.Dispose()
    }
}