And this is my output EmailBody variable
:
TEST GROUP --- @{InputObject=User01; SideIndicator==>}TEST GROUP --- @{InputObject=User02; SideIndicator==>}
Here is the kind of output I am looking for EmailBody variable
as top and bottom :
TEST GROUP --- User01 added to mail group.
TEST GROUP --- User02 added to mail group.
Is it possible for me to format my output? I only want to display custom keyword instead of @{InputObject=User01; SideIndicator==>}
Any help would be greatly appreciated!
Here is the script that I wrote:
$EmailBody = ""
$Groups = Get-Content -Path "C:\temp\MyGroups.txt"
$Groups | ForEach-Object {
$Group = $_
$infoMember = Get-AdgroupMember "$Group" -Recursive
$Members = foreach ($member in $infoMember) { get-aduser $member.samaccountname -properties displayname | select -ExpandProperty displayname }
if (!(Test-Path "C:\temp\$Group.txt"))
{
New-Item -path "C:\temp\$Group.txt" -type "file"
Write-Host "Created new file"
$Members | Out-File -FilePath "c:\temp\$Group.txt" -Force
}
else{
$OldMembers = Get-Content -Path "C:\temp\$Group.txt"
$Change = Compare-Object -ReferenceObject $OldMembers -DifferenceObject $Members
ForEach($Changes in $Change) {
if ($Changes.sideindicator -eq "=>") {
if ($Changes){
$EmailBody += "$Group`n---`n$Changes"
}
write-host "user added to mail group" -ForegroundColor Cyan
$Members | Out-File -FilePath "c:\temp\$Group.txt" -Force
}
else{
write-host "nothing" -ForegroundColor Yellow
}
}
}
}