Created
March 6, 2015 02:42
-
-
Save brianly/b50cc365ec9d589d9dfa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Copyright 2014 Microsoft | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
Yammer auditing tool that looks for active Yammer accounts that | |
are either disabled or missing from AD DS. | |
Takes User.csv file from Yammer Data Export as the input file. | |
Compares all Active Yammer accounts in the input file to user | |
lookup in AD. User is searched by both email and proxyAddresses. | |
If a user is not Enabled (either Disabled on Not Found) in ALL of | |
the specified domain services, a row is written to the output csv | |
file. A user found to be Enabled in at least one domain service is | |
considered safe to remain in Yammer. | |
The users listed in the output file may be Suspended via the Yammer | |
bulk update tool. Suspension is preferred over Deletion because | |
suspended accounts can be re-activated if necessary. Deleted accounts | |
can not be restored. | |
NOTE: The error file that is output can be used as the input file | |
when re-running this script. This makes it easy to rerun the process | |
if there were connection errors, etc. | |
Outputs - | |
Results File (accounts you should suspend in Yammer) = "Results_[dateStamp].csv" | |
$log_path = "log_[dateStamp].txt" | |
$error_path = "error_[dateStamp].csv" | |
Params - | |
Root path to input file - example: C:\utils\yammeraudit | |
Users.csv file name - example: Users.csv or error_[dateStamp].csv | |
#> | |
$path = $args[0] | |
$inputFile = $args[1] | |
$input_path = "$path\$inputFile" | |
$count = 0 | |
$errorCount = 0 | |
#List of domain services to search (depending on environment, port may or may not be needed) | |
$ADServices = "hq.sourcefoundry.co.uk:3268"#, "emea.corp.contoso.com:3268" , "noam.corp.contoso.com:3268" | |
$dateStamp = (Get-Date -format s) -replace ":", "-" | |
$output_path = "$path\Results_$dateStamp.csv" | |
$log_path = "$path\log_$dateStamp.txt" | |
$error_path = "$path\error_$dateStamp.csv" | |
echo '==========' | |
echo $input_path | |
echo $output_path | |
echo $log_path | |
echo $error_path | |
echo '==========' | |
Add-content $log_path -value "[$(Get-Date -format g)] Starting Yammer User Audit..." | |
Add-content $log_path -value "[$(Get-Date -format g)] Input File: $input_path" | |
Add-content $log_path -value "[$(Get-Date -format g)] Calculating total active Yammer accounts..." | |
#Run through entire csv file initially to get total count (used later for showing progress percent complete) | |
Import-Csv -Path $input_path | Where-Object {$_.state -eq "active"} | ForEach-Object ` | |
{ | |
$count++ | |
write-progress -activity "Counting Active Yammer Accounts in CSV File" -status "$count" | |
} | |
$totalActive = $count | |
Add-content $log_path -value "[$(Get-Date -format g)] Total active Yammer accounts = $totalActive" | |
Add-content $log_path -value "[$(Get-Date -format g)] Comparing each active Yammer account to AD... " | |
$90logged = $FALSE | |
$80logged = $FALSE | |
$70logged = $FALSE | |
$60logged = $FALSE | |
$50logged = $FALSE | |
$40logged = $FALSE | |
$40logged = $FALSE | |
$30logged = $FALSE | |
$20logged = $FALSE | |
$10logged = $FALSE | |
$count = 0 | |
Import-Csv -Path $input_path | Where-Object {$_.state -eq "active"} | ForEach-Object ` | |
{ | |
#Use this code to enter specific accounts to skip (for example, VIP's and other key accounts that should NEVER get suspended) | |
if (([string]::Compare($_.email, "ceo@contoso.com", $True) -eq 0) ` | |
-or ([string]::Compare($_.email, "glen@contoso.com", $True) -eq 0)) | |
{ | |
return | |
} | |
$percentComplete = [int](($count/$totalActive)*100) | |
#logic for showing periodic percent complete update in the log file | |
switch -Regex ($percentComplete) | |
{ | |
[9][0-9] | |
{ | |
if (!$90logged) | |
{ | |
$90logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[8][0-9] | |
{ | |
if (!$80logged) | |
{ | |
$80logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[7][0-9] | |
{ | |
if (!$70logged) | |
{ | |
$70logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[6][0-9] | |
{ | |
if (!$60logged) | |
{ | |
$60logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[5][0-9] | |
{ | |
if (!$50logged) | |
{ | |
$50logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[4][0-9] | |
{ | |
if (!$40logged) | |
{ | |
$40logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[3][0-9] | |
{ | |
if (!$30logged) | |
{ | |
$30logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[2][0-9] | |
{ | |
if (!$20logged) | |
{ | |
$20logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
[1][0-9] | |
{ | |
if (!$10logged) | |
{ | |
$10logged = $TRUE | |
$logPercent = $TRUE | |
} | |
} | |
} | |
if ($logPercent) | |
{ | |
Add-content $log_path -value "[$(Get-Date -format g)] $percentComplete percent complete" | |
$logPercent = $FALSE | |
} | |
write-progress -activity "Comparing each active Yammer account to AD" -status "Progress->" -percentcomplete $percentComplete -CurrentOperation "$percentComplete percent complete" | |
$acctEnabled = $FALSE | |
$errorOccurred = $FALSE | |
#initialize hash table | |
$properties = @{ | |
'YammerId'= $_.id; | |
'YammerEmail' = $_.email; | |
'YammerState' = $_.state; | |
'YammerApiUrl' = $_.api_url; | |
} | |
$email = $_.email | |
$proxyEmail = "smtp:" + $email | |
#check all domain services specified. Only flag an account for Yammer Suspension if NOT enabled in ALL of the services. | |
Foreach ($service in $ADServices) | |
{ | |
Try | |
{ | |
$user = Get-ADUser -Filter {(mail -eq $email) -or (proxyAddresses -eq $proxyEmail)} -Properties mail, proxyAddresses -Server $service | |
if (!$user) | |
{ | |
$properties.Add($service, "not found") | |
} | |
else | |
{ | |
if ($user -and ($user.Enabled)) | |
{ | |
#User is enabled in at least one domain service, keep the user in Yammer | |
$acctEnabled = $TRUE | |
break | |
} | |
else | |
{ | |
$properties.Add($service, "disabled") | |
} | |
} | |
} | |
Catch | |
{ | |
#Log the exception alongside the account in the error file | |
[system.exception] | |
$errorOccurred = $TRUE | |
$errorCount++ | |
$errorProperties = @{ | |
'id'= $properties.Get_Item("YammerId"); | |
'email' = $properties.Get_Item("YammerEmail"); | |
'state' = $properties.Get_Item("YammerState"); | |
'api_url' = $properties.Get_Item("YammerApiUrl"); | |
'Domain' = $service; | |
'Error' = $($Error | Out-String); | |
'Timestamp' = $(Get-Date -format g) | |
} | |
New-Object -TypeName PSObject -Property $errorProperties | Export-Csv $error_path -Append | |
Write-Host "ERROR" | |
Write-Host ($errorProperties | Format-Table | Out-String) | |
$Error.Clear() | |
break | |
} | |
}#Foreach | |
#Only add user to the suspension list if the user is NOT enabled in ALL domain services AND there was not an error | |
if (!$acctEnabled -and !$errorOccurred) | |
{ | |
New-Object -TypeName PSObject -Property $properties | Export-Csv $output_path -Append | |
} | |
$count++ | |
} #Import-Csv ... ForEach-Object | |
Add-content $log_path -value "[$(Get-Date -format g)] 100 percent complete" | |
if ($errorCount -gt 0) | |
{ | |
Add-content $log_path -value "[$(Get-Date -format g)] Audit process has completed WITH ERRORS. The list of accounts to suspend in Yammer are in the file - $output_path. Errors are listed in $error_path. If there was an error auditing an account, it will not be listed in the suspend list." | |
Write-Host "Audit process has completed WITH ERRORS. The list of accounts to suspend in Yammer are in the file - $output_path. Errors are listed in $error_path. If there was an error auditing an account, it will not be listed in the suspend list." | |
} | |
else | |
{ | |
Add-content $log_path -value "[$(Get-Date -format g)] Audit process has completed. The list of accounts to suspend in Yammer are in the file - $output_path" | |
Write-Host "Audit process has completed. The list of accounts to suspend in Yammer are in the file - $output_path" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment