jeudi 30 juillet 2015

can't get If else statement work in Powershell AD script

The problem is, this script works perfect. The problem is.. it also works on a "Administrator" and "Guest" account, and that's something I don't want. So I created a if else statement but it's not working, maybe someone is seeing really fast what I am doing wrong?

What does the script? When someone is locked in the AD, it creates an HTML mail with info (hostname, domain, username).

This is the script:

$DC = "DC1"
$Report= "c:\powershell\html.html"
$Name1 = "Administrator", "Guest"
$log2 = "C:\powershell\temp.log"

$HTML=@"
<title>Account locked out Report</title>
<style>
BODY{background-color :#FFFFF}
TABLE{Border-width:thin;border-style: solid;border-color:Black;border-collapse: collapse;}
TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color: ThreeDShadow}
TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color: Transparent}
H2{color: #457dcf;font-family: Arial, Helvetica, sans-serif;font-size: medium; margin-left: 40px;
</style>
"@

$Account_Name = @{n='Account name';e={$_.ReplacementStrings[-1]}}
$Account_domain = @{n='Account Domain';e={$_.ReplacementStrings[-2]}}
$Caller_Computer_Name = @{n='Caller Computer Name';e={$_.ReplacementStrings[-1]}}


$event= Get-EventLog -LogName Security -ComputerName $DC -InstanceId 4740 -Newest 1 |
   Select TimeGenerated,ReplacementStrings,"Account name","Account Domain","Caller Computer Name" |
   % {
     New-Object PSObject -Property @{
      "Account name" = $_.ReplacementStrings[-7]
      "Account Domain" = $_.ReplacementStrings[5]
      "Caller Computer Name" = $_.ReplacementStrings[1]
      Date = $_.TimeGenerated
    }
   }

 $event | ConvertTo-Html -Property "Account name","Account Domain","Caller Computer Name",Date -head $HTML -body  "<H2> User is locked in the Active Directory</H2>"|
     Out-File $Report -Append

Get-EventLog -LogName Security -ComputerName DC1 -InstanceId 4740 -Newest 1 | Format-List >> $log2


If ((Get-Content $log2 ) -contains $Name1 )

{
#Don't do shit
}

Else
{
$MailBody= Get-Content $Report
$MailSubject= "User Account locked out"
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "smtp.eurotransplant.org"
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "mailadress@mailadress.com"
$MailMessage.To.add("mailadress@mailadress.com")
$MailMessage.Subject = $MailSubject
$MailMessage.IsBodyHtml = 1
$MailMessage.Body = $MailBody
$SmtpClient.Send($MailMessage)
}


Remove-Item c:\powershell\html.html
Remove-Item C:\Powershell\temp.log

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire