Solved

Help with some formating in a here-string in Powershell

Posted on 2015-01-14
3
91 Views
Last Modified: 2015-01-14
Hello Powershell Experts,

I want to do a proof of concept using Powershell to generate a group membership audit report.  We have a file that has a list of authorized users for the test group.  I then want to compare the list of authorized user to the users actually in the group in Active Directory.  This is easy enough.  I’ve written the following to achieve this goal:

$TargetGroup = "NickTestGroup2"

$AuthorizedGroupMembers = Get-Content -Path C:\PowershellResources\Group2.txt

$GroupMembersInAD = Get-ADGroupMember -Identity $TargetGroup | ForEach-Object {$_.name}

$UnauthorizedGroupMembers = Compare-Object $GroupMembersInAD $AuthorizedGroupMembers -IncludeEqual | Where {$_.sideindicator -eq "<="} | ForEach-Object {$_.Inputobject}

$EmailBody = @"
The following is a list of unauthorized users in the '$TargetGroup':
$UnauthorizedGroupMembers
"@

This almost gives me what I want.  The value of the $EmailBody variable as it is now is:

The following is a list of unauthorized users in the ‘Test Group’:
User1 User2 User3

Ideally, I would like the output to look like:

The following is a list of unauthorized users in the ‘Test Group’:
User1
User2
User3


My question to all of you is:
How can I get my output to is how can I get the content of the $EmailBody variable to list the users on different lines as detailed above?
0
Comment
Question by:ndalmolin_13
3 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40549288
Try this:
$EmailBody = @"
The following is a list of unauthorized users in the '$TargetGroup':
$($UnauthorizedGroupMembers -join "`r`n")
"@

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40549293
You change the default string separator $OFS (which is space by default), or join the strings yourself using a different separator.
$EmailBody = @"
The following is a list of unauthorized users in the '$TargetGroup':
$($UnauthorizedGroupMembers -join "`r`n")
"@ 

Open in new window

0
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 40549902
That worked great.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now