6

में मैं स्थानीय व्यवस्थापकों के समूह के सदस्यों का निर्धारण करने के लिए निम्न कोड का उपयोग करें:प्रिंट स्थानीय समूह सदस्य PowerShell 5.0

$obj_group = [ADSI]"WinNT://localhost/Administrators,group" 
[email protected]($obj_group.Invoke("Members"))|foreach{$_.GetType().InvokeMember("Name","GetProperty",$null,$_,$null)} 
Write-Output "Current local Administrators: $members" 

इस कोड PowerShell 2.0 में काम करता है - 4.0। हालांकि, पावरशेल 5.0 के साथ मेरी विंडोज 10 मशीन पर, यह टूट जाता है।

Error while invoking GetType. Could not find member. 
At line:2 char:54 
+ ... "))|foreach{$_.GetType().InvokeMember("Name","GetProperty",$null,$_,$ ... 
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OperationStopped: (:) [], MissingMemberException 
    + FullyQualifiedErrorId : System.MissingMemberException 

डोमेन खातों कि प्रशासक के एक सदस्य हैं के लिए, कोई त्रुटि उत्पन्न होता है: प्रत्येक स्थानीय खाता स्थानीय प्रशासक समूह का एक सदस्य है कि के लिए, यह निम्न त्रुटि फेंकता है।

जो चीज मुझे पहेली करती है वह GetType() ऑब्जेक्ट का एक सदस्य है (मैंने हाथ से आदेश का पता लगाया है), इसलिए मुझे यकीन नहीं है कि यह क्यों त्रुटियों से बाहर है।

मैंने पावरशेल 5.0 के लिए चेंजलॉग देखा और कुछ भी नहीं देखा जो स्पष्ट रूप से इस व्यवहार को समझाएगा।

ऐसा क्यों हो रहा है? यदि PowerShell 5.0 में किसी स्थानीय समूह के सदस्यों को मुद्रित करने का कोई बेहतर तरीका है?

उत्तर

10

इस मुद्दे अपने आप में पड़ गए और एक समाधान (विंडोज़ 10 और 8.1 में परीक्षण किया गया)

$obj_group = [ADSI]"WinNT://localhost/Administrators,group" 
$members= @($obj_group.psbase.Invoke("Members")) | foreach{([ADSI]$_).InvokeGet("Name")} 
Write-Output "Current local Administrators: $members" 
+0

यह तय करता है। एक्सपी (पावरशेल 2.0), विंडोज 7 (पावरशेल 4.0), और विंडोज 10 (पावरशेल 5.0) में परीक्षण किया गया। –

1

आप here को बग पोस्ट करना चाहते हैं क्योंकि किसी के पास वर्कअराउंड उपलब्ध हो सकता है।

+0

यह पहले से ही पोस्ट किया गया: https://connect.microsoft.com/PowerShell/feedback/details/1437366/powershell-5-bug- इन-गेटटाइप-फॉर-कॉम-ऑब्जेक्ट्स- iadsuser –

2

जैमी के जवाब अपने विशिष्ट मुद्दे के लिए एकदम सही था पता लगा, लेकिन मैं सदस्यों से कई गुण प्राप्त करने के एक की जरूरत है । मैंने पाया कि InvokeMember पर कॉल करने से पहले GetType के विरुद्ध Invoke पर कॉल करके अन्यथा अपना कोड बदलने के बिना आप इस समस्या को हल कर सकते हैं। नोट GetType नहीं रह गया है नीचे दिए गए कोड में () बाद यह है कि:

$obj_group = [ADSI]"WinNT://localhost/Administrators,group" 
[email protected]($obj_group.Invoke("Members"))|foreach{$_.GetType.Invoke().InvokeMember("Name","GetProperty",$null,$_,$null)} 
Write-Output "Current local Administrators: $members" 

यह मेरा उपयोग के मामले जो समूह के सदस्यों के बारे में अधिक जानकारी प्रदान करता है था। Resolve-DNS कमांड के उपयोग के कारण PowerShell 4.0 की आवश्यकता होती है:

function Get-LocalGroupMembers { 
<# 
.Synopsis 
    Get the group membership of a local group on the local or a remote computer 
.EXAMPLE 
    Defaults to collecting the members of the local Administrators group 

    PS C:\> Get-LocalGroupMembers | ft -AutoSize 

    ComputerName ParentGroup Nesting Name   Domain  Class 
    ------------ ----------- ------- ----   ------  ----- 
    EricsComputer     0 Administrator EricsComp User 
    EricsComputer     0 eric   EricsComp User 
    EricsComputer     0 Domain Admins DomainName Group 
.EXAMPLE 
    Query a remote computer (that is known not to respond to a ping) and a targeted group 

    PS C:\> Get-LocalGroupMembers -computerName EricsComputer -localgroupName Users -pingToEstablishUpDown $false 

    ComputerName ParentGroup Nesting Name   Domain  Class 
    ------------ ----------- ------- ----   ------  ----- 
    EricsComputer     0 SomeOtherGuy EricsComp User 

.NOTES 
    The ParentGroup and Nesting attributes in the output are present to allow 
    the output of this function to be combined with the output of 
    Get-ADNestedGroupMembers. They serve no purpose otherwise. 
#> 
    Param(
     $computerName = $env:computername, 
     $localgroupName = "Administrators", 
     $pingToEstablishUpDown = $true 
    ) 
    $requestedComputerName = $computerName 
    if ($computername = Resolve-DnsName $computername) { 
     $computername = ($computername | where querytype -eq A).Name 
     if ($computername -ne $requestedComputerName) { 
      Write-Warning "Using name $computerName for $requestedComputerName" 
     } 
    } else { 
     Write-Warning "Unable to resolve $requestedComputerName in DNS" 
     return "" | select @{label="ComputerName";Expression={$requestedComputerName}}, 
             @{label="ParentGroup";Expression={""}}, 
             @{label="Nesting";Expression={""}}, 
             @{Label="Name";Expression={"ComputerName did not resolve in DNS"}}, 
             @{Label="Domain";Expression={"ComputerName did not resolve in DNS"}}, 
             @{Label="Class";Expression={"ComputerName did not resolve in DNS"}} 
    } 
    if ($pingToEstablishUpDown) { 
     if (-not (Test-Connection -count 1 $computerName)) { 
      Write-Warning "Unable to ping $computerName, aborting ADSI connection attempt" 
      return "" | select @{label="ComputerName";Expression={$requestedComputerName}}, 
             @{label="ParentGroup";Expression={""}}, 
             @{label="Nesting";Expression={""}}, 
             @{Label="Name";Expression={"Not available to query"}}, 
             @{Label="Domain";Expression={"Not available to query"}}, 
             @{Label="Class";Expression={"Not available to query"}} 
     } 
    } 
    try { 
     if([ADSI]::Exists("WinNT://$computerName/$localGroupName,group")) {  
      $group = [ADSI]("WinNT://$computerName/$localGroupName,group") 
      $members = @() 
      $Group.Members() | foreach { 
       $AdsPath = $_.GetType.Invoke().InvokeMember("Adspath", 'GetProperty', $null, $_, $null) 
       # Domain members will have an ADSPath like WinNT://DomainName/UserName. 
       # Local accounts will have a value like WinNT://DomainName/ComputerName/UserName. 
       $a = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries) 
       $name = $a[-1] 
       $domain = $a[-2] 
       $class = $_.GetType.Invoke().InvokeMember("Class", 'GetProperty', $null, $_, $null) 

       $members += "" | select @{label="ComputerName";Expression={$computerName}}, 
             @{label="ParentGroup";Expression={""}}, 
             @{label="Nesting";Expression={0}}, 
             @{Label="Name";Expression={$name}}, 
             @{Label="Domain";Expression={$domain}}, 
             @{Label="Class";Expression={$class}} 
      }  
     } 
     else { 
      Write-Warning "Local group '$localGroupName' doesn't exist on computer '$computerName'" 
     } 
    } 
    catch { 
     Write-Warning "Unable to connect to computer $computerName with ADSI" 
     return $false } 
    return ,$members 
} 
1

अच्छा! इसकी आवश्यकता है!

.net रास्ता भी एक बाईपास था (http://blogs.technet.com/b/heyscriptingguy/archive/2013/10/27/the-admin-s-first-steps-local-group-membership.aspx)

Add-Type -AssemblyName System.DirectoryServices.AccountManagement 
$ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine 
$computer = $env:COMPUTERNAME 
$context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $computer 
$idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName 
$group = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, 'Administrators') 
$group.Members | select @{N='Server'; E={$computer}}, @{N='Domain'; E={$_.Context.Name}}, samaccountName 
संबंधित मुद्दे