0

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

$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 身份验证的人的组?

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

4

2 回答 2

1

你可以试试这个:

[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
    }
  }
}
于 2013-02-08T22:00:02.330 回答
0

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

(get-qaduser $user).memberof

否则,您可以使用调用命令并提供凭据:

$groups=Invoke-Command -ComputerName $env:COMPUTERNAME -Credential $cred -ScriptBlock {
    [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups
}
于 2013-02-08T17:32:01.540 回答