Link to home
Start Free TrialLog in
Avatar of Ampletrix
AmpletrixFlag for Australia

asked on

Powershell Newline

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.
Avatar of becraig
becraig
Flag of United States of America image

Try p><code>$DomainAdmin`n`r</code></p>
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of Ampletrix

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
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.
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!