首页 > 解决方案 > 带有 Start-Process 的 PowerShell Invoke-Command 输出不同的结果

问题描述

我有一个用于运行 Dell Command 更新工具的更新脚本。总之dcu-cli.exe。现在的事情是,当我在本地计算机上运行相同的脚本代码时,一切都运行正常,但是当我在脚本中运行完全相同的代码时invoke-command(是的,我拥有完整的管理员权限),而不是退出代码的2含义An unknown application error has occurred0 (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 information: C:\Dell_Update.log")
    }
}

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

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

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

标签: windowspowershellinvoke-command

解决方案


尝试以这种方式运行它。您应该能够看到任何输出或错误消息。我通常先添加到路径而不是使用&or 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 localhost { # elevated
start-process 'findstr' '/i word c:\users\joe\file1' -wait -RedirectStandardOutput c:\users\joe\out } 

推荐阅读