I have the following script and all is working (thanks to advice from this site and Dave Wyatt). I am now attempting to add a final piece that emails the results. The issue I am having is that I can’t get the format of the email body correct.
I have tried out-string and format-table with no luck.
The goal is to write to the body of the email the list of users ($user.name) and the the machines ($comp) that were added to the user’s LogonRestrictions attribute. In a nice readable table / format.
Any ideas???
####### Begin Script #########
Write-Host “Please select input file”
$initialDir = “\NP1SECR016v\SECRpt\LogonRestrictionConfig”
Get-FileName $initialDir
Import the input file.
$items = Import-Csv $gfn_file
$ReportList = @()
$users = $items | Group-Object -Property UserID
$TotalUsers = $users.count
Write-Host $users.count " users to process:"`n`n
ForEach($user in $users)
{
Write-Host `n`n"Processing " $user.Name
$userIDSam = $user.name
$LR = ($user.Group | Select-Object -ExpandProperty LogonRestrictions) -join “,” # LR = LogonRestrctions computer list
$LR_report = $LR -split ","
Set-ADUser -Identity $user.Name -LogonWorkstations $LR
$groupArray = $user.Group | Select-Object -ExpandProperty GroupName -Unique
#Add-ADPrincipalGroupMembership -Identity $userGroup.Name -MemberOf $groupArray
Write-host "User added to $groupArray"
foreach ($comp in $LR_report)
{
Write-Host `t$comp
$ExportList = New-Object -TypeName PSObject
Add-Member -InputObject $ExportList -MemberType NoteProperty -Name UserID -Value $userIDSam
Add-Member -InputObject $ExportList -MemberType NOteProperty -Name LR -Value $comp
$ReportList += $ExportList
}
}
write-host `n`n$TotalUsers " users processed"
#$MyData = $ReportList | Group-Object -Property UserID
#$Mydata1 = $ReportList | Format-Table ‘Logon Restrictions’ -GroupBy UserID
#$MyData
#$Mydata = $ReportList | Out-String
write-host “ReportList:”
$ReportList
$mydata = $reportlist | Out-String
#=================CreateEmail
$header = @"
Table {border-width: 1px;border-style: solid; border-color: black;boder-collapse: collapse;}
th {border-width: 1px;padding: 3px;border-style: solid; border-color: black; background-color: #6495ed;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
"@
Format the email body
$EmailBody += “”
$EmailBody += “”
$EmailBody += “IT Security Operations”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “The following logon restrictions have been set.”
$EmailBody += “”
$EmailBody += “”
$EmailBody += "$LR_Report "
$EmailBody += “”
$EmailBody += “”
$EmailBody += "$ReportList "
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += " "
$EmailBody += " Users processed"
$EmailBody += " $TotalUsers"
$EmailBody += " "
$EmailBody += " "
$EmailBody += " $MyData"
$EmailBody += " "
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “Log: $LogPath”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “”
$EmailBody += “Script run time: $timeSpan”
#=================End CreateEmail
Send-MailMessage -BodyAsHtml -To “user@companyA.com” -Subject “Logon Restrictions” -SmtpServer xxx.companyA.com -From “support@companyA.com” -Body $Emailbody
####### End Script #########