?
Solved

Help with some formating in a here-string in Powershell

Posted on 2015-01-14
3
Medium Priority
?
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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 70

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

800 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