Last active
July 7, 2016 08:23
-
-
Save terrytrent/0d03f99547e449404371d94b7d157f32 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
$DayDate=(get-date (get-date).AddDays(-1) -Format dddd) | |
$FileDate=(get-date (get-date).AddDays(-1) -format "MM-dd-yyyy") | |
$SubjectDate=(get-date (Get-Date).AddDays(-1) -format 'dddd MMMM dd, yyyy') | |
$DHCPLogLocation='C:\windows\system32\dhcp' | |
$Day=$DayDate[0..2] -join '' | |
$LogName="DhcpSrvLog-$Day" | |
$LogFile="$LogName.log" | |
$LogFullPath="$DHCPLogLocation\$LogFile" | |
$LogAssignsFile="DhcpSrvLog-$FileDate-Assigns.csv" | |
$LogRenewsFile="DhcpSrvLog-$FileDate-Renews.csv" | |
$TempLocation="C:\temp" | |
if(!(Test-Path $TempLocation)) | |
{ | |
New-Item -Path $TempLocation -ItemType Directory | Out-Null | |
} | |
$LogFileContent=Get-Content $LogFullPath | |
$IPLeases=$LogFileContent | select -Skip 34 | where {$($_).substring(0,2) -match '^10$|^11$'} | |
$AllLeases=@() | |
foreach($Lease in $IPLeases) | |
{ | |
$LeaseSplit=$Lease -split ',' | |
$LeaseArray=New-Object -TypeName psobject | |
$LeaseArray | Add-Member –MemberType NoteProperty –Name 'DateTime' -Value $(get-date "$($LeaseSplit[1]) $($LeaseSplit[2])") | |
$LeaseArray | Add-Member –MemberType NoteProperty –Name 'Description' -Value $LeaseSplit[3] | |
$LeaseArray | Add-Member –MemberType NoteProperty –Name 'IP Address' -Value $LeaseSplit[4] | |
$LeaseArray | Add-Member –MemberType NoteProperty –Name 'Host Name' -Value $LeaseSplit[5] | |
$LeaseArray | Add-Member –MemberType NoteProperty –Name 'MAC Address' -Value $LeaseSplit[6] | |
$AllLeases+=$LeaseArray | |
} | |
$Renews=$AllLeases | where {$_.Description -eq "Renew"} | select DateTime,'IP Address','Host Name','MAC Address' | export-csv -NoTypeInformation "$TempLocation\$LogAssignsFile" | |
$Assigns=$AllLeases | where {$_.Description -eq "Assign"} | select DateTime,'IP Address','Host Name','MAC Address' | export-csv -NoTypeInformation "$TempLocation\$LogRenewsFile" | |
$Username="DomainUsername@domain.local" # Replace with actual email username | |
$Password="PlainTextPassword" | ConvertTo-SecureString -Force -AsPlainText # See http://www.adminarsenal.com/admin-arsenal-blog/secure-password-with-powershell-encrypting-credentials-part-1/ for more ways to pull password, please do not actually store in the powershell script | |
$Creds=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $Password | |
$Attachments=@("$TempLocation\$LogAssignsFile","$TempLocation\$LogRenewsFile") | |
$Subject="DHCP Logs for $SubjectDate" | |
$Body="<h1>DHCP Logs Attached:</h1><ul><li>$LogAssignsFile</li><li>$LogRenewsFile</li></ul>" | |
$To="<recipient email address>" # Replace wtih actual TO Address | |
$From="<sender email address>" # Replace with actual FROM Address | |
$SMTPServer="<SMTP Server>" # Replace with actual SMTP Server | |
Send-MailMessage -Attachments $Attachments -Body $Body -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer -BodyAsHtml -Credential $Creds | |
foreach($File in $Attachments) | |
{ | |
Remove-Item $File | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment