PowerShell Invoke-Command with Start-Process 输出不同的结果

问题描述

我有一个用于运行 Dell Command Update 工具的更新脚本。简而言之dcu-cli.exe。现在的事情是,当我在本地计算机上运行相同的脚本代码时,一切都运行正常,但是当我在带有 invoke-command 的脚本中运行完全相同的代码时(是的,我拥有完整的管理员权限)而不是退出代码2 表示 An unkNown application error has occurred 而不是 0 (everything OK)

这是一个非常大的脚本,所以我创建了一个新脚本来调试它。这是短代码

Invoke-Command -ComputerName "MyComputer" -ScriptBlock {    
    $ExitCode = 0             

    #Declare path and arguments
    $DcuCliPath = 'C:\Program Files (x86)\Dell\CommandUpdate\dcu-cli.exe'                              
    $DellCommand = "/applyUpdates -autoSuspendBitLocker=enable -outputLog=C:\Dell_Update.log"
                
    #Verify Dell Command | Update exists
    If (Test-Path -Path $DcuCliPath) {
        $objWMI = Get-WmiObject Win32_ComputerSystem
        Write-Host ("Dell Model [{0}]" -f $objWMI.Model.Trim())

        $serviceName = "DellClientManagementService"
        Write-Host ("Service [{0}] is currently [{1}]" -f $serviceName,(Get-Service $serviceName).Status)
        If ((Get-Service $serviceName).Status -eq 'Stopped') {    
            Start-Service $serviceName
            Write-Host "Service [$serviceName] started"    
        }

        #Update the system with the latest drivers
        Write-Host "Starting Dell Command | Update tool with arguments [$DellCommand] dcu-cli found at [$DcuCliPath]"
        $ExitCode = (start-process -FilePath ($DcuCliPath) -ArgumentList ($DellCommand) -Passthru -Wait).ExitCode
        Write-Host ("Dell Command | Update tool finished with ExitCode: [$ExitCode] current Win32 ExitCode: [$LastExitCode] Check log for more @R_524_4045@ion: C:\Dell_Update.log")
    }
}

当我删除 Invoke-Command -ComputerName "MyComputer" -ScriptBlock { 然后在 PC 上复制并运行本地脚本时,退出代码 = 0

我还注意到,当我通过“Invoke-Command”运行命令时,当我传递参数时也没有创建日志文件......所以我最好的猜测是本地和远程出现了问题路径?

那我错过了什么?我猜这很简单,但我花了几个小时才让它运行起来,但没有任何运气......

解决方法

尝试以这种方式运行它。您应该能够看到任何输出或错误消息。我通常先添加到路径中,而不是使用 &start-process

invoke-command mycomputer { 
$env:path += ';C:\Program Files (x86)\Dell\CommandUpdate'; 
dcu-cli /applyUpdates -autoSuspendBitLocker=enable -outputLog=C:\Dell_Update.log }

在 invoke-command 中使用 start-process 似乎非常具有挑战性。除非我将它保存到文件中,否则我什至看不到 findstr 的输出。如果我不等待,输出将被截断。默认情况下,启动进程在后台和另一个窗口中运行。还有一个 -nonewwindow 选项,但它对 invoke-command 没有帮助。

invoke-command localhost { # elevated
start-process 'findstr' '/i word c:\users\joe\file1' -wait -RedirectStandardOutput c:\users\joe\out } 
,

@js2010,感谢您的额外帮助。不幸的是,这也没有帮助。

所以我进行了更多的调试,结果证明这是在我的测试机器上运行的 dcu-cli 版本中的一个错误,DOH...!!

在测试机器上 3.1.1 版正在运行,而在另一台机器上 4.0 版正在运行,并且通过远程 Powershell 运行良好。所以我查找了发行说明,我在这里找到了:https://www.dell.com/support/kbdoc/000177325/dell-command-update

正如你在 3.1.3 版本中看到的,有这个修复:

A problem was solved where dcu-cli.exe was not executed in an external interactive session of PowerShell.