簡體   English   中英

調用“GetOwner”的異常

[英]Exception calling "GetOwner"

團隊! 最近,我的遠程 pssession 腳本在獲取進程所有者時失敗。

        #Some code up and down the fragmet
        #Use WMI or CIM cmdlets.
        $UseCIM = !( Test-GetWMIObjectPresent ) #local function to choose CIM or WMI methods 
        if ( $UseCIM ) {
            $ProcessArray = Get-CimInstance -ClassName 'Win32_Process' -Property  $ProcessProperties                       
        }
        Else {
            $ProcessArray = Get-WmiObject  -Class 'Win32_Process' -Property  $ProcessProperties 
        }

        $ProcessArray | Add-Member -NotePropertyName 'User' -NotePropertyValue ''
        $ProcessProperties += 'User'
        $ProcessArray | Add-Member -NotePropertyName 'ComputerName' -NotePropertyValue $env:COMPUTERNAME
        $ProcessProperties += 'ComputerName'

        foreach ( $Item in $ProcessArray ){
            try {
                if (  $UseCIM ) {
                    # We have error here!!!
                    $Owner = $Item | Invoke-CimMethod -MethodName 'GetOwner' -ErrorAction 'SilentlyContinue'
                }
                Else {
                    # and here!
                    $Owner = $Item | Invoke-WmiMethod -Name 'GetOwner' -ErrorAction 'SilentlyContinue'
                }
                if ( $Owner ){
                    if ( $Owner.domain ){
                        $Item.User = "$( $Owner.domain )\$( $Owner.User )"
                    }
                    Else {
                        $Item.User = "$( $Owner.User )"
                    }
                }
            }
            Catch{
                Add-ToLog -Message "$_" -Display -Status "error"
            }
        }

遠程用戶具有管理權限。

我們有錯誤:

調用“GetOwner”的異常:“由於對象的當前狀態,操作無效。”

如果我們在本地會話下運行它,則不會發生錯誤。 有什么建議么?

要獲取所有者,傳遞給GetOwner的對象必須具有名為Handle的屬性。

文檔

即使您沒有使用 Property 參數列出它們,返回的對象也包含關鍵屬性。 該類的其他屬性存在,但未填充。

在評論中感謝 Theo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM