PowerShell - Benutzer, Gruppen, Clipboard

Benutzer

Gruppen von Benutzern auslesen und ins Clipboard schreiben

$userlist = "user1","user2"
$dl = "──────────────────────────────────────────────────"
$ddl = "══════════════════════════════════════════════════"
$this_report = ""
$this_report += $ddl + "`r`n"
foreach ($this_user in $userlist)
   {
   $this_obj_user = (Get-ADObject -Filter {samaccountname -eq $this_user} -Properties * -ErrorAction SilentlyContinue)
   if ($this_obj_user)
       {
       $this_sn = $this_obj_user.sn
       $this_gn = $this_obj_user.givenName
       $this_desc = $this_obj_user.Description
       $this_dept = $this_obj_user.department
       $this_report += "│ User: $this_user │ $this_sn, $this_gn │ $this_desc, $this_dept │`r`n"
       $this_report += $dl + "`r`n"
       $this_groups = ($this_obj_user | select -ExpandProperty memberof | % {Get-ADObject $_ -properties name | select -ExpandProperty name} | sort)
       foreach ($this_group in $this_groups)
           {
           $this_report += $this_group + "`r`n"
           }
       $this_report += $ddl + "`r`n"
       }
   else
       {
       $this_report += "│ User: " + $this_user + " │`r`n"
       $this_report += $dl + "`r`n" + "─── NOT FOUND! ───" + "`r`n"
       $this_report += $ddl + "`r`n"
       }
   }
$this_report | Out-Host
$this_report | Set-Clipboard
"Sent to Clipboard." | Out-Host

 

Gruppen

Member von Gruppen auslesen und ins Clipboard schreiben

$grouplist = "group1","group2"
$dl = "──────────────────────────────────────────────────"
$ddl = "══════════════════════════════════════════════════"
$this_report = ""
$this_report += $ddl + "`r`n"
foreach ($this_group in $grouplist)
   {
   $this_obj_group = (Get-ADObject -Filter {samaccountname -eq $this_group} -Properties * -ErrorAction SilentlyContinue)
   if ($this_obj_group)
       {
       $this_desc = $this_obj_group.Description
       $this_report += "│ Group: $this_group │ $this_desc │`r`n"
       $this_report += $dl + "`r`n"
       $this_users = ($this_obj_group | select -ExpandProperty member | % {Get-ADObject $_ -properties samaccountname | select -ExpandProperty samaccountname} | sort)
       foreach ($this_user in $this_users)
           {
           $this_report += $this_user + "`r`n"
           }
       $this_report += $ddl + "`r`n"
       }
   else
       {
       $this_report += "│ Group: " + $this_group + " │`r`n"
       $this_report += $dl + "`r`n" + "─── NOT FOUND! ───" + "`r`n"
       $this_report += $ddl + "`r`n"
       }
   }
$this_report | Out-Host
$this_report | Set-Clipboard
"Sent to Clipboard." | Out-Host