5

I have created a log-on script based on Active-Directory Module, that queries the user group membership in order to map his drives etc.

I have compiled it with PowerGui, and created an EXE file. the problem is, the module doesn't exist on the users computers.

Is there a way to do this without the module, or add the module to the compilation?

JPBlanc
  • 70,406
  • 17
  • 130
  • 175
Benny
  • 695
  • 2
  • 6
  • 19

2 Answers2

2

For group memberships, you could also get it without connecting to AD, and parse the output of the WHOAMI utility

$groups = WHOAMI /GROUPS /FO CSV | ConvertFrom-Csv | Select-Object -ExpandProperty 'Group Name'

if($groups -contains 'group1')
{
   do something
}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
1

One way is using Active-Directory Service Interface (ADSI).

you can find in another SO post (Can I match a user to a group accross different domains?) one way to find all the groups a user belongs to, using ADSI, in the post it's a C# code, but it's easy to translate.

Here is a small example of a simple search to begin.

Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://WM2008R2ENT:389/dc=dom,dc=fr","jpb@dom.fr","Pwd")

# Look for a user
$user2Find = "user1"
$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$rc = $Rech.filter = "((sAMAccountName=$user2Find))"
$rc = $Rech.SearchScope = "subtree"
$rc = $Rech.PropertiesToLoad.Add("mail");

$theUser = $Rech.FindOne()
if ($theUser -ne $null)
{
  Write-Host $theUser.Properties["mail"]
} 

Another way is to use System.DirectoryServices.AccountManagement Namespace.

This way is also using ADSI, but it's encapsulated, and you need the Framework .NET 3.5. You will also find in the same post but in the Edited (2011-10-18 13:25) part, a C# code using this way.

You can also use WMI :

$user2Find = "user1"
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$user2find'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
$user.DS_mail

You can use this solution localy on your server or from a computer inside the domain, but it's a bit more complicated to authenticate to WMI from outside the domain.

Community
  • 1
  • 1
JPBlanc
  • 70,406
  • 17
  • 130
  • 175