xs4eyes

Find users computer from ad group

Oct 4th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.        $FILESERVER = "File server name"
  2.         $group = Get-ADGroupMember -Identity "AD Group Name"
  3.         $groupname = $group.name
  4.         $findusercomputer = $null
  5.         # Connect Remotely to Server, Run Session, get a list of everybody logged in there
  6.  
  7.  
  8.         $S=NEW-PSSESSION –computername $FILESERVER
  9.         $Results=(INVOKE-COMMAND –Session $s –scriptblock { (NET SESSION) })
  10.         REMOVE-PSSESSION $S
  11.  
  12.  
  13.         foreach($name in $groupname){
  14.             $Username = Get-ADUser -filter * -Properties samaccountname, cn | Where-Object cn -like "$name" | select samaccountname
  15.             $username = $username.samaccountname
  16.             $R = $results | Select-String $Username
  17.             Foreach ( $Part in $R ) {
  18.                 $ComputerIP=$Part.Line.substring(2,21).trim()
  19.                 $User = $Part.Line.substring(21,44).trim()
  20.                 # Use nslookup to identify the computer, grab the line with the “Name:” field in it
  21.                 $Computername=([System.Net.dns]::GetHostbyAddress("$ComputerIP"))
  22.                 $computername =  $ComputerName.HostName
  23.                 If ($Computername -ne $NULL) {  
  24.                     #Else { $Computername=$Computername.substring(9).trim()}
  25.                     # “$User is on $Computername with IP $ComputerIP”
  26.                     [array]$findusercomputer += $computername
  27.                 }
  28.             }
  29.         }
  30.         $findusercomputer
Advertisement
Add Comment
Please, Sign In to add comment