Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在powershell中使用获得的凭据查找经过身份验证的用户组?_Powershell_Authentication_Active Directory_Credentials_Active Directory Group - Fatal编程技术网

如何在powershell中使用获得的凭据查找经过身份验证的用户组?

如何在powershell中使用获得的凭据查找经过身份验证的用户组?,powershell,authentication,active-directory,credentials,active-directory-group,Powershell,Authentication,Active Directory,Credentials,Active Directory Group,我写了一个脚本,在执行时要求提供凭证;事情是这样的 $cred = Get-Credential #Read credentials $username = $cred.username $password = $cred.GetNetworkCredential().password $CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName $domain = New-Object System.DirectoryServices.

我写了一个脚本,在执行时要求提供凭证;事情是这样的

$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,    $UserName, $Password)

if ($domain.name -eq $null)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Windows.Forms.MessageBox]::Show("Authentication failed - please verify your username and password.")
    exit
}
else
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [System.Windows.Forms.MessageBox]::Show("Authentication Success")

    $Groups = [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
->后来我意识到了我的错误,最后一行收集了登录windows机器的用户组;如何更改此设置并获取已通过身份验证的人员的组,而不是使用windows身份验证的人员的组

请务必让我知道任何问题或澄清。

如果您能够安装,它将非常简单:

(get-qaduser $user).memberof
否则,您可以使用invoke命令并提供凭据:

$groups=Invoke-Command -ComputerName $env:COMPUTERNAME -Credential $cred -ScriptBlock {
    [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
}
您可以尝试以下方法:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
$CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,    $UserName, $Password)

if ($domain.name -eq $null)
{
  [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  [System.Windows.Forms.MessageBox]::Show("Authentication failed - please verify your username and password.")
  exit
}
else
{
  Add-Type -AssemblyName System.DirectoryServices.AccountManagement
  [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

  $cred = Get-Credential #Read credentials
  $username = $cred.username
  $password = $cred.GetNetworkCredential().password
  $CurrentDomain = "LDAP://" + ([ADSI] "" ).distinguishedName
  $domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,    $UserName, $Password)

  if ($domain.name -eq $null)
  {
    [System.Windows.Forms.MessageBox]::Show("Authentication failed - please verify your username and password.")
    exit
  }
  else
  {
    [System.Windows.Forms.MessageBox]::Show("Authentication Success")

    $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
    $user = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($ct, $username)

    $groups = $user.GetGroups()
    foreach($i in $groups){
      $i.SamAccountName
    }
  }
}

这会在很多工作站上发生,无法在所有工作站上安装quest角色。这会导致WINRM出错,执行此“[System.Security.Principal.WindowsIdentity]::GetCurrent().Groups”单独工作,但整个脚本失败;既然我只想为经过身份验证的用户创建组,为什么我们需要使用“-computername”?invoke命令的参数集强制我们使用-computername,如果我们想使用-credential。。。我不明白你的回答,失败的是什么?能否提供完整的错误消息和更新问题,以查看更新的脚本以及如何在工作站上运行它?此行“添加类型-AssemblyName System.DirectoryServices.AccountManagement”是否在所有powershell安装中都可用?因为我们是在一个动态环境中引入这个脚本的,这是一个内置模块吗?它需要任何插件吗?它是一个.net命名空间,framework 3.5。。。IIRC,也试试
[reflection.assembly]::LoadWithPartialName(“System.DirectoryServices.AccountManagement”)
在我们的实现中,我们将低至.net 2.0(这是powershell 2.0的最低要求),目前我测试了我们的块,它工作得很好lt,但我唯一关心的是使它具有普遍性,而不依赖于任何库。让我知道你的想法,我选择你的回答作为答案,谢谢你。目前,下面的代码对我很有效$filter=“(&(objectCategory=$category)(SamAccountname=$user))“$objDomain=New Object System.DirectoryServices.DirectoryEntry$objSearcher=New Object System.DirectoryServices.DirectorySearcher$objSearcher.SearchRoot=$objDomain$objSearcher.PageSize=1000$objSearcher.filter=$filter$objSearcher.SearchScope=“子树”$objSearcher.FindOne()$Groups=$Obj.Propertied“memberof”@Darktux是的,我刚刚写信给你,让你使用[ADSI]并在这里了解它:,Accountmanagement名称空间并没有短一点,但也许有些计算机只有.net 2.0。。