SOLVED

Help with code

%3CLINGO-SUB%20id%3D%22lingo-sub-2666059%22%20slang%3D%22en-US%22%3EHelp%20with%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2666059%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20all%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20the%20following%20script%20that%20looks%20for%20all%20disabled%20users%20that%20have%20a%20value%20in%20the%20%22manager%22%20attribute.%20It%20works%2C%20however%20if%20no%20results%20are%20the%20script%20sends%20an%20email.%20I%20would%20like%20the%20script%20to%20not%20send%20an%20email%26nbsp%3B%20if%20no%20results%20are%20found.%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EQuery%20all%20users%20all%20disabled%20users%20and%20remove%20value%20from%20manager%20attribute%3CBR%20%2F%3E%23%26gt%3B%3CBR%20%2F%3ERemove-Item%20-Path%20C%3A%5CTemp%5CDisabledUsers%5C*.csv%20-Force%3CBR%20%2F%3E%24smtpFrom%20%3D%20%22AllADusers%40vixxo.com%22%3CBR%20%2F%3E%24smtpServer%20%3D%20%22BOS-EXC2016%22%3CBR%20%2F%3E%24today%20%3D%20Get-Date%20-Format%20yyyyMMdd%3CBR%20%2F%3E%24smtpto%20%3D%20%22skip.hofmann%40vixxo.com%2CKlint.price%40vixxo.com%2Ceric.burt%40vixxo.com%22%3CBR%20%2F%3E%24message%20%3D%20New-Object%20System.Net.Mail.MailMessage%20%24smtpfrom%2C%20%24smtpto%3CBR%20%2F%3E%24ToDay%20%3DGet-date%20-Format%20yyyMMdd%3CBR%20%2F%3EImport-Module%20activedirectory%3CBR%20%2F%3E%24Disabled%20%3D%20Get-ADUser%20-Filter%20*%20-Properties%20samaccountname%2Cmanager%2Cenabled%20%7C%20where%20%7B%24_.manager%20-notlike%20%24null%20-and%20%24_.enabled%20-eq%20%24False%7D%20%7Cselect%20samaccountname%2Cenabled%2Cmanager%2Cdistinguishedname%3CBR%20%2F%3E%24Disabled%20%7C%20Export-Csv%20-path%20C%3A%5CTemp%5CDisabledUsers%5C%24today.csv%20-NoTypeInformation%3C%2FP%3E%3CP%3E%24message.Subject%20%3D%20(%22Disabled%20users%20that%20currently%20have%20manager.%20Manager%20wil%20be%20removed%20%22%2B%24today)%3CBR%20%2F%3E%24disabledUser%20%3D%20Get-Item%20C%3A%5CTemp%5CDisabledUsers%5C%24today.csv%3CBR%20%2F%3E%24att3%20%3D%20new-object%20Net.Mail.Attachment(%24disabledUser)%3CBR%20%2F%3E%24message.Attachments.Add(%24att3)%3CBR%20%2F%3E%24smtp%20%3D%20New-Object%20Net.Mail.SmtpClient(%24smtpServer)%3CBR%20%2F%3E%24smtpFrom%3CBR%20%2F%3E%24smtp.Send(%24message)%3CBR%20%2F%3E%24message.Attachments.Remove(%24att3)%3C%2FP%3E%3CP%3E%24Disabled%20%3D%20Import-Csv%20-Path%20C%3A%5CTemp%5CDisabledUsers%5C%24today.csv%3CBR%20%2F%3E%24Disabled%20%7CForEach-Object%20%7BSet-ADUser%20-Identity%20%24_.samaccountname%20-Manager%20%24null%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2666059%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2666248%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2666248%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1096811%22%20target%3D%22_blank%22%3E%40Skipster311-1%3C%2FA%3ETry%20the%20modified%20version%20below.%20Two%20modifications%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1)%20Put%20a%20check%20around%20the%20code%20to%20send%20the%20email%20and%20make%20the%20AD%20changes%20to%20determine%20if%20there%20were%20any%20disabled%20users%20present%20in%20the%20%24Disabled%20variable%3C%2FP%3E%3CP%3E2)%20There's%20no%20need%20to%20read%20the%20disabled%20users%20again%20from%20the%20CSV%20file%2C%20you%20already%20have%20them%20in%20the%20%24Disabled%20variable%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ERemove-Item%20-Path%20C%3A%5CTemp%5CDisabledUsers%5C*.csv%20-Force%0A%24smtpFrom%20%3D%20%22AllADusers%40vixxo.com%22%0A%24smtpServer%20%3D%20%22BOS-EXC2016%22%0A%24today%20%3D%20Get-Date%20-Format%20yyyyMMdd%0A%24smtpto%20%3D%20%22skip.hofmann%40vixxo.com%2CKlint.price%40vixxo.com%2Ceric.burt%40vixxo.com%22%0A%24message%20%3D%20New-Object%20System.Net.Mail.MailMessage%20%24smtpfrom%2C%20%24smtpto%0A%24ToDay%20%3DGet-date%20-Format%20yyyMMdd%0AImport-Module%20activedirectory%0A%24Disabled%20%3D%20Get-ADUser%20-Filter%20*%20-Properties%20samaccountname%2Cmanager%2Cenabled%20%7C%20where%20%7B%24_.manager%20-notlike%20%24null%20-and%20%24_.enabled%20-eq%20%24False%7D%20%7Cselect%20samaccountname%2Cenabled%2Cmanager%2Cdistinguishedname%0A%24Disabled%20%7C%20Export-Csv%20-path%20C%3A%5CTemp%5CDisabledUsers%5C%24today.csv%20-NoTypeInformation%0A%0Aif%20(%24Disabled)%20%7B%0A%20%20%20%20%24message.Subject%20%3D%20(%22Disabled%20users%20that%20currently%20have%20manager.%20Manager%20wil%20be%20removed%20%22%2B%24today)%0A%20%20%20%20%24disabledUser%20%3D%20Get-Item%20C%3A%5CTemp%5CDisabledUsers%5C%24today.csv%0A%20%20%20%20%24att3%20%3D%20new-object%20Net.Mail.Attachment(%24disabledUser)%0A%20%20%20%20%24message.Attachments.Add(%24att3)%0A%20%20%20%20%24smtp%20%3D%20New-Object%20Net.Mail.SmtpClient(%24smtpServer)%0A%20%20%20%20%24smtpFrom%0A%20%20%20%20%24smtp.Send(%24message)%0A%20%20%20%20%24message.Attachments.Remove(%24att3)%0A%0A%20%20%20%20%24Disabled%20%7CForEach-Object%20%7BSet-ADUser%20-Identity%20%24_.samaccountname%20-Manager%20%24null%7D%0A%7D%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2666471%22%20slang%3D%22en-US%22%3ERe%3A%20Help%20with%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2666471%22%20slang%3D%22en-US%22%3EOkay%2C%20thank%20you%2C%20i%20will%20give%20this%20a%20try%2C%20and%20yes%20i%20should%20have%20removed%20the%20email%20addresses%20before%20posting.%20Thank%20you%20again.%3C%2FLINGO-BODY%3E
Frequent Contributor

Hello all

 

I have the following script that looks for all disabled users that have a value in the "manager" attribute. It works, however if no results are the script sends an email. I would like the script to not send an email  if no results are found. 

Thank you in advance.

 

Query all users all disabled users and remove value from manager attribute
#>
Remove-Item -Path C:\Temp\DisabledUsers\*.csv -Force
$smtpFrom = "AllADusers@example.com"
$smtpServer = "XXXX"
$today = Get-Date -Format yyyyMMdd
$smtpto = ""
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$ToDay =Get-date -Format yyyMMdd
Import-Module activedirectory
$Disabled = Get-ADUser -Filter * -Properties samaccountname,manager,enabled | where {$_.manager -notlike $null -and $_.enabled -eq $False} |select samaccountname,enabled,manager,distinguishedname
$Disabled | Export-Csv -path C:\Temp\DisabledUsers\$today.csv -NoTypeInformation

$message.Subject = ("Disabled users that currently have manager. Manager wil be removed "+$today)
$disabledUser = Get-Item C:\Temp\DisabledUsers\$today.csv
$att3 = new-object Net.Mail.Attachment($disabledUser)
$message.Attachments.Add($att3)
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtpFrom
$smtp.Send($message)
$message.Attachments.Remove($att3)

$Disabled = Import-Csv -Path C:\Temp\DisabledUsers\$today.csv
$Disabled |ForEach-Object {Set-ADUser -Identity $_.samaccountname -Manager $null}

3 Replies
best response confirmed by Skipster311-1 (Frequent Contributor)
Solution

@Skipster311-1Try the modified version below. Also, might be wise to remove your email addresses :) Two modifications:

 

1) Put a check around the code to send the email and make the AD changes to determine if there were any disabled users present in the $Disabled variable

2) There's no need to read the disabled users again from the CSV file, you already have them in the $Disabled variable

 

Remove-Item -Path C:\Temp\DisabledUsers\*.csv -Force
$smtpFrom = "AllADusers@vixxo.com"
$smtpServer = "BOS-EXC2016"
$today = Get-Date -Format yyyyMMdd
$smtpto = "XXX,XXX,XXX"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$ToDay =Get-date -Format yyyMMdd
Import-Module activedirectory
$Disabled = Get-ADUser -Filter * -Properties samaccountname,manager,enabled | where {$_.manager -notlike $null -and $_.enabled -eq $False} |select samaccountname,enabled,manager,distinguishedname
$Disabled | Export-Csv -path C:\Temp\DisabledUsers\$today.csv -NoTypeInformation

if ($Disabled) {
    $message.Subject = ("Disabled users that currently have manager. Manager wil be removed "+$today)
    $disabledUser = Get-Item C:\Temp\DisabledUsers\$today.csv
    $att3 = new-object Net.Mail.Attachment($disabledUser)
    $message.Attachments.Add($att3)
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $smtpFrom
    $smtp.Send($message)
    $message.Attachments.Remove($att3)

    $Disabled |ForEach-Object {Set-ADUser -Identity $_.samaccountname -Manager $null}
}

 

 

 

Okay, thank you, i will give this a try, and yes i should have removed the email addresses before posting. Thank you again.
Worked great. Thank you again