Powershell: Monitor and E-mail AD Group Membership

I got a request a few months ago for a script to monitor a couple of AD groups. The requirements were an excel sheet which contained the Users Name, ID# and Group Name so the sheet could be filtered and sorted.

Note: This script requires the ActiveRoles Management Shell for Active Directory.

Connect-QADService

$Results = @()

$Date = (Get-Date -DisplayHint Date)
$save_date = $Date.ToString("MM-dd-yyyy")

$Groups = "First_ADGroup","Second_ADGroup"
$Groups = $Groups | Get-QADGroup

foreach($group in $Groups){

foreach($user in $group)
{
$Results += Get-QADGroupMember $user -Indirect -SizeLimit 0 |
Add-Member -Name "Users" -value $user -MemberType NoteProperty -PassThru |
Add-Member -Name "Group" -value $group -MemberType NoteProperty -PassThru |
Select DisplayName,SamAccountName,Group
}
}

$file_output = ('D:\Path_To_Save_File\File-' + $save_date + '.csv')
$Results | Export-CSV -Path $file_output -NoTypeInformation

Start-Sleep -s 20

$filename = $file_output
$smtpServer = “SMTP Server or SMTP Relay Server”

$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = “yourname@whatever.com”
$msg.To.Add(”User you want to send this to”)
$msg.cc.Add("Add a cc address or comment this line out")
$msg.Subject = “Weekly AD Group Membership Tracking"
$msg.Body = “E-mail body message”
$msg.Attachments.Add($att)

$smtp.Send($msg)

Powershell: Change Network Label

I ran into the situation where I needed to change the network label on hundreds of VM’s after acquiring a Nexus switch. There was no way I could have accomplished this manually during a small outage window. You run this script directly from the console; just type the new network label and hit enter and watch the magic. It’s a really short script and pretty self-explanatory.

$NetworkName = Read-Host "Enter the network name"
Get-VM -Location "I used vCenter Folder" | get-networkadapter | set-networkadapter -networkname $NetworkName -Confirm:$false