powershell - 为什么通过 Invoke-Command 远程运行 Get-WmiObject 失败?

标签 powershell invoke-command get-wmiobject

我正在尝试构建一个简单的“get-ProcessInfo.ps1”模块,以与名为 Kansa 的 PowerShell(取证/IR)框架一起使用。该命令是一个简单的单行程序,它调用 Get-WMIObject win32_process 并将其通过管道传递给 Select-Object。然后 Kansa 应该通过 Export-Csv cmdlet 将数据导出到 Csv。该脚本在我的本地主机上运行没有问题,但是在通过 Kansa 中的 Invoke-Command cmdlet 远程运行(在 Windows 上)时失败。对于每个 processID,我的错误日志显示 get-ProcessInfo.ps1“找不到具有进程标识符 #### 的进程”。其他模块在远程主机上运行没有问题,所以我知道我正在以管理员身份进行身份验证。因此,我认为我遇到了权限错误,或者可能是 Wmi 的身份验证问题。我通过域管理员帐户从 Windows 框在 Windows 域中运行它。

堪萨 GitHub:https://github.com/davehull/Kansa
堪萨斯谈话:https://www.youtube.com/watch?v=cQmsT9D0kKI

我试图复制另一个 Kansa 模块中看到的 WmiObject 调用,但这仍然没有从远程主机生成数据。 - https://github.com/davehull/Kansa/blob/master/Modules/Process/Get-ProcsWMI.ps1

我试图了解 InjectedThreads.ps1 脚本中发生了什么,因为它远程使用 WmiObject 没有问题,但它有点超出我的想象。据我所知,听起来 WmiObject 是“非托管的”(未经身份验证?/没有从 PowerShell 继承 Kerberos?) - https://github.com/davehull/Kansa/blob/master/Modules/Process/Get-InjectedThreads.ps1

我尝试了 Wmi 身份验证、模拟和权限的多种变体。不幸的是,仍然没有产生远程数据。 - https://blogs.msmvps.com/richardsiddaway/2011/08/04/authentication-impersonation-and-privileges/

最后,由于 get-WmiObject 在技术上已被弃用,为了支持 Get-CIMInstance,我尝试了 Get-CIMInstance cmdlet 的多种变体。

这是我正在尝试制作的模块中的代码,get-ProcessInfo.ps1

Get-WmiObject win32_process | Select-Object creationdate,ws,ProcessName,ProcessID,ParentProcessID, @{Name = 'ParentProcessName';Expression = {(Get-Process -Id $_.ParentProcessId).Name}},Path,CommandLine,@{Name = 'ParentProcessPath';Expression = {(Get-Process -Id $_.ParentProcessId).Path}}

预期的结果应该是一个进程列表及其相关信息,它在我的本地机器上工作,并且在通过 Kansa.ps1 中的 Invoke-Command 远程运行时不返回任何数据(只是错误)

有人可以为我指明正确的方向,了解这里到底发生了什么,以及我该如何解决这个问题?

*请注意,此脚本是通过远程主机上的 WinRM(Invoke-Command)运行的,因此要求凭据是不可能的,硬编码凭据也是如此。

最佳答案

在您的计算属性中,您假设 ParentProcessId始终是有效的 id:

@{Name = 'ParentProcessPath';Expression = {(Get-Process -Id $_.ParentProcessId).Path}

Win32_Process documentation声明如下:

It is possible that the process identified by ParentProcessId is terminated, so ParentProcessId may not refer to a running process. It is also possible that ParentProcessId incorrectly refers to a process that reuses a process identifier.



这意味着 ParentProcessId可能指向一个已经终止的进程。在那种情况下 (Get-Process -Id $_.ParentProcessId).Path将触发错误(因为没有找到例如 528 的进程,因此 (Get-Process -Id $_.ParentProcessId) 将是 $null 并且您正在尝试调用 $null.Name )。

我在我的机器上测试了你的单线,没有收到你上面描述的任何错误。因此,我检查了 Kansa 如何调用给定模块,可以找到 here :
$Job = Invoke-Command -Session $PSSessions -FilePath $Module -ArgumentList $Arguments -AsJob -ThrottleLimit $ThrottleLimit

如您所见,您的模块文件作为远程作业被调用(参见 -AsJob 开关)。我问自己,通过后台调用脚本 block 时,错误处理是否会有所不同。我发现了这个有趣的 StackOverflow answer说明:

Using throw will change the job object's State property to "Failed". ...



这是有道理的,因为与默认 PowersShell 主机相比,作业不能忽略错误。所以,我将你的命令包装到一个工作中(在我的本地机器上):
$job =start-job -ScriptBlock { Get-WmiObject win32_process | Select-Object creationdate,ws,ProcessName,ProcessID,ParentProcessID, @{Name = 'ParentProcessName';Expression = {(Get-Process -Id $_.ParentProcessId).Name}},Path,CommandLine,@{Name = 'ParentProcessPath';Expression = {(Get-Process -Id $_.ParentProcessId).Path}}  } 
Wait-Job $job
$result =Receive-Job $job

调用Receive-Job时我在命令行上看到以下错误:

Cannot find a process with the process identifier 528. At line:1 char:1 + $result =Receive-Job $job + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (528:Int32) [Get-Process], ProcessCommandException + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand + PSComputerName : localhost



我还检查了$result对于 $null :
> $null -eq $result
True

从上面可以看出错误提到NoProcessFoundForGivenId ,这是不言自明的。如您所见,错误处理取决于您的代码如何被调用(作为 Job 或在 PowerShell 主机上)。

你能做什么?

您可以查看 ParentProcessId是否有效。因此,您必须将代码更改为:
Get-WmiObject win32_process | ForEach-Object {

    # Create a custom object with the given properties. If Select-Object doesn't find given property it'll create an empty `NoteProperty` with the given name
    $processInfo = $_ | Select-Object creationdate,ws,ProcessName,ProcessID,ParentProcessID, ParentProcessPath, ParentProcessName
    $p = (Get-Process -Id $_.ParentProcessId -ErrorAction SilentlyContinue) 

    if ($null -ne $p){
        # Parent process exists lets fill up the empty properties
        $processInfo.ParentProcessName = $p.Name
        $processInfo.ParentProcessPath = $p.Path
    }

    $processInfo # Return this value to the pipeline
} 

可能有更复杂的方法来执行$null检入 Select-Object 的计算属性,不幸的是,我不知道。

如果我们将上面的代码包装在一个作业中并运行它:
    $job = start-job -ScriptBlock { Get-WmiObject win32_process | ForEach-Object {
        $processInfo = $_ | Select-Object creationdate,ws,ProcessName,ProcessID,ParentProcessID, ParentProcessPath, ParentProcessName
        $p = (Get-Process -Id $_.ParentProcessId -ErrorAction SilentlyContinue) 
        if ($null -ne $p){
            $processInfo.ParentProcessName = $p.Name
            $processInfo.ParentProcessPath = $p.Path
        }

        $processInfo # Return this value to the pipeline
    } 
}

Wait-Job $job
$result = Receive-Job $job
if ($null -ne $result){
    $result
}
else {
    Write-Error "Job with id $($job.Id) failed"
}

我们将获得所有进程而不会出现任何错误。

希望有帮助。

关于powershell - 为什么通过 Invoke-Command 远程运行 Get-WmiObject 失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014402/

相关文章:

PowerShell:如何使用 WMI 在远程计算机上运行 powershell 脚本

powershell - 在 PowerCLI 中比较 2 个对象

powershell - PowerShell:Start-Job -scriptblock多行脚本 block ?

windows - 使用 Powershell 的 Invoke-Command 调用带参数的批处理文件

powershell - 代码中无法识别 Get-WmiObject

powershell - 使用Powershell获取HDD序列号

regex - 如何用逗号分割字符串忽略双引​​号中的逗号

powershell - docker rmi : Error response from daemon: invalid reference format: repository name must be lowercase

powershell - 将值传递给PowerShell。退出事件操作

powershell - Invoke-Command 中的 ArgumentList 参数不发送所有数组