Solved

Powershell Newline

Posted on 2014-03-30
9
447 Views
Last Modified: 2014-04-04
Hello All,

I have a Powershell script that grabs all the members of some groups in Active Directory and then emails it to a few people.

It is working fine, except the output displays the users next to each other. Here is the section I'm talking about:

Example: Domain Admins Group;

------------------------------------------------------------------------------------------------------
Import-Module ActiveDirectory

$DomainAdmin = Get-ADGroupMember -Identity "Domain Admins" | Sort-Object name | ForEach-Object {$_.Name} | Out-String
$DAcount = $DomainAdmin.count

$outbody = @"
<h2>Domain Admin Membership</h2>
<p><code>$DomainAdmin</p></code>
<p><b>Domain Admin Member count: $DAcount</p></b>
<hr style="border: 1pax dashed black;" />
-------------------------------------------------------------------------------

What I'd like to do is to get the names in lines of their own. I was looking at the "newline" command but I can't get it to work.
Any help would be awesome.

Thanks.
Ampletrix.
0
Comment
Question by:Ampletrix
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 39965580
Try p><code>$DomainAdmin`n`r</code></p>
0
 
LVL 39

Accepted Solution

by:
footech earned 300 total points
ID: 39965729
A couple points here.  The biggest reason it doesn't look the way you want it to is because you're not treating it like HTML.  Create any file in notepad with the same formatting tags that you show above, and compare it to what you get if you output $outbody to a file and you should see exactly what I mean.  If you were just outputting to a text file things would look fine, but since you're creating an HTML file (or email), you have to account for that.

I could write this in different ways, but just to keep the structure of your script...  Two modifications are necessary.
1) Don't use Out-String
$DomainAdmin = Get-ADGroupMember -Identity "Domain Admins" | Sort-Object name | ForEach-Object {$_.Name}

Open in new window

2) From the above you have an array in $DomainAdmin.  Now you'll want to join each element using HTML formatting to put everything on a new line.  I'm not a whiz at HTML coding, so I don't even know what the <code></code> tags are supposed to be, but I left them in there assuming that you do.
$outbody = @"
<h2>Domain Admin Membership</h2>
<p><code>$($DomainAdmin -join "<br>")</p></code>
<p><b>Domain Admin Member count: $DAcount</p></b>
<hr style="border: 1pax dashed black;" />

Open in new window

0
 
LVL 16

Expert Comment

by:Learnctx
ID: 39965738
Why not just use PowerShell to generate the HTML? It won't have the count, but I would argue if that you need a counter of how many domain admins exist in your company you need to strongly consider looking into delegation of rights. This goes off the topic of your question I suppose, but AD security is so often poorly managed. Also look at including Schema Admins and Enterprise Admins in your reporting.

Send-MailMessage -To "youremail@yourdomain.com" -From "noreply@yourdomain.com" -Subject "Members of domain admins" -SmtpServer smtpserveraddress -BodyAsHtml -Body  $(Get-ADGroupMember -Identity "Domain Admins" | sort name| select samaccountname,distinguishedname | ConvertTo-Html | out-string)

Open in new window

Or

$Body = Get-ADGroupMember -Identity "Domain Admins" | sort name| select samaccountname,distinguishedname | ConvertTo-Html | out-string
Send-MailMessage -To "youremail@yourdomain.com" -From "noreply@yourdomain.com" -Subject "Members of domain admins" -SmtpServer smtpserveraddress -BodyAsHtml -Body $Body

Open in new window

0
 

Author Comment

by:Ampletrix
ID: 39965929
Thanks everyone for replying.
Unfortunately I got side tracked from work today and will certainly test this tomorrow morning.

Becraig and footech: Thanks for the comments. I tried testing becraigs's suggestion but didn't quite get to the results. It looks like, between the two suggestions, that this is what I'm looking for.

Learnctx: Thank you too for your comment. We have a fairly large and overly complicated network with an abundance of security controls. The script contains close to 20 AD groups, and the emailing part works fine. The rest of the output requires the counter so that my peers can easily review any changes. It's the CDO in me (OCD in alphabetical order) that added that to the domain admins group, the Schema Admins  as well as the Enterprise Admins group. I only used the Domain admins group as an example, (-: but thanks for pointing that out anyway.

The Send-MailMessage command I have sends the output fine, with all the members of all the various groups and their respective counts, so I think having a large number of groups might make your solution a little harder.
I do however value your concerns around the number of Domain Admins and overall domain security. It is absolutely very important!

I will test this tomorrow morning and post my results. Thanks everyone for the suggestions.
Cheers.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 28

Assisted Solution

by:becraig
becraig earned 200 total points
ID: 39966527
My comment answered your question about how to create a newline, however footech is on point  (As he always is) now I look at your script.


There are a lot of options to do what you want.

$output  = @()
$DomainAdmin = Get-ADGroupMember -Identity "Domain Admins" | Sort-Object name | ForEach-Object {$user = $_.Name; $output += "$user `n"}
$DAcount = $DomainAdmin.count

$outbody = @"
<h2>Domain Admin Membership</h2>
<p><code>$output</p></code>
<p><b>Domain Admin Member count: $DAcount</p></b>
<hr style="border: 1pax dashed black;" />

Open in new window

0
 

Author Closing Comment

by:Ampletrix
ID: 39970775
Thanks everyone for the comments and help. It is working great. I ended up using footech's sollution, but I did award points to becraig too because the answer would have been correct too. I hope that this is fair to everyone. Thank you all very much for the help.
0
 
LVL 39

Expert Comment

by:footech
ID: 39971826
I don't care about the points, but thought it was important to clarify that becraig's solution does not work.  It is doing the same thing that you have in your original question.  Namely, the point here is that a new line in a string does not equate to a new line in HTML.
0
 

Author Comment

by:Ampletrix
ID: 39973919
Hello Footech,
You're right about that.
You know the Eureka moment, or the "light bulb" moment you get when something you've been working falls in place, or you understand the concept or problem? I'm sure you do.
Anyway - That's what I got from you. You had it spot on with the issue where I wasn't treating it as HTML. I am grateful for that. You taught me something. You added value and gave me some experience. I'd rather shake your hand or perhaps have a beer with if I could.

You know the false sense of achievement you got when you copied your classmate's homework because you didn't do it yourself? That is more or less what I would have had by using Becraig's solution. I hope you understand what I'm trying to say.

This is my problem with Expert Sexchange (EE); my Achilles heel! Do you win something or get awarded with something if you get more points? I regularly get the assignment of points wrong. In this case I used your solution because it would be easier to add groups to the script, and it worked great! Not only that, but you added lots!!! more value to me as a geek. Both becraig and Learnctx added value to my little dilemma with this script, but you added more value to me than what points in EE can account for.
How do I measure that in EE? How do I say thanks for adding value to the other participants without awarding them points?
I'd be happier to buy you a beer and say thank you!

In my opinion - this is why EE's points system suck piles! This is also why a lot of my fellow students at the university refuse to use EE (I study part time).

I am about to ask another Powershell question with which you may be able to help me out. If I can say I apologize for not awarding you with all the points, and at the same time say thank you for teaching me something, I'd be happy with that. Alternatively, I hope that I can give you an advantage to help me again, with my next minor dilemma I'm about to raise?

Thanks again.
Ampletrix.
0
 
LVL 39

Expert Comment

by:footech
ID: 39977649
I'm glad I was able to help you.  I'd much rather explain something than just provide a bit of code.  The only thing we earn with points are rankings (and optionally T-shirts if we want them).  I also tend to look at points as a means to help gauge my skills or progress in a subject.  Recognition can be a factor (how much depends on the person), but mostly I participate here to help others and to learn (there's often no better way to learn or vet your own knowledge of a subject than to teach it to others).

You're the best judge of which posts you found helpful or not.  I would say that any posts that helped you gain an understanding of the situation or guided you to a solution are eligible to be awarded points.  A direct answer to your question may not necessarily be the best solution, and it's not a must that the accepted solution be awarded more points than an assist.  Here are some of the mistakes (if you want to call them that, sometimes it's just a difference in opinion) that I see in grading:
 - awarding points to all posts (whether helpful or not).  This just shows a lack of effort on the part of the grader/asker.
 - awarding points to solutions that have either not been tested or that don't work.  The danger here is for the next person that views the thread and may be misled.
 - not awarding points to any other posts when a direct answer was not provided and the asker found their own solution (but with the guidance from other posts).  Sometimes it's not possible to provide a direct answer, but pointing the asker in the right direction to find their own solution is worthy of consideration.
 - deleting a question when the asker found their own solution instead of posting the answer.  I have seen some threads with really great troubleshooting and background information deleted.  This is a judgement call, but it would be my preference that the asker post their own solution and accept it as the answer.

It's not really possible to award points based on effort (though sometimes it's obvious, in many situations how could you possibly tell?).  I have seen others on here post better code in 15 minutes that what I came up with in an hour.  The way I look at the grading/awarding of points is as a means of building a knowledge base of answers and useful troubleshooting information.  It's interesting that you mention that others you know refuse to use EE because of the points system (I assume you mean just the awarding of points).  I don't believe I've heard that sentiment before.  It's not an unexpected possibility for there to be a little confusion the first few times when grading solutions to a question, but I wouldn't expect that to last.

In relation to this thread:
It's only because becraig's solution didn't work in the context of HTML that I wanted to point it out (I don't want to pick on becraig here as I have seen some very good solutions from him).
Learnctx's suggestion of using ConvertTo-HTML is a good one, especially if you need to output more than a single property.  It can save you a lot of work manually writing the HTML code yourself.

There's absolutely no reason to apologize for not awarding me all the points.

Cheers!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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 …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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