Powershell - Convertto-html / loop problem

I have the following script and it all works just fine if I try to output it to CSV or a txt file but as soon as I try to output to HTML I do not get any data in the file.  The syntax I use for the HTML works if the script is not using a Loop so I'm sure I have the syntax wrong for using a loop but I just can't figure out what to change.  Any help would be appreciated.
_____________________________________________________________________________
$Alias_Input = "c:\LastLogonTime\Alias_Output.txt"
$Alias_Output = "c:\LastLogonTime\Mailsrv2.htm"

$a = "<style>"
$a = $a + "BODY{background-color:#808080;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#737CA1;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#C0C0C0;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "</style>"

foreach ($User in Get-Content $Alias_Input){

get-MailboxStatistics -server mailsrv2 |

where {$_.lastloggedonuseraccount -eq "domain\$User" -and $_.lastlogontime -lt $(get-date).adddays(-1) -and $_.lastloggedonuseraccount -ne 'domain\user1' -and $_.lastloggedonuseraccount -ne 'domain\user2'} |

Select-Object @{name="Mailbox";expression={$_.displayname}},@{name="Last Logon Time";expression={$_.lastlogontime}}, @{name="Last Logged on User Account";expression={$_.lastloggedonuseraccount}}

ConvertTo-HTML -head $a –body "<H2>Last Logon Account</H2><title>Last Logon Account</title>" |
Out-file "c:\LastLogonTime\Mailsrv2.htm"
}
exsuprtAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe KlimisCommented:
I think this should work but i have been unable to test
$Alias_Input = "c:\LastLogonTime\Alias_Output.txt"
$Alias_Output = "c:\LastLogonTime\Mailsrv2.htm"

$a = "<style>"
$a = $a + "BODY{background-color:#808080;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#737CA1;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#C0C0C0;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "</style>"

foreach ($User in Get-Content $Alias_Input){

get-MailboxStatistics -server mailsrv2 | 

where {$_.lastloggedonuseraccount -eq "domain\$User" -and $_.lastlogontime -lt $(get-date).adddays(-1) -and $_.lastloggedonuseraccount -ne 'domain\user1' -and $_.lastloggedonuseraccount -ne 'domain\user2'} | 

Select-Object @{name="Mailbox";expression={$_.displayname}},@{name="Last Logon Time";expression={$_.lastlogontime}}, @{name="Last Logged on User Account";expression={$_.lastloggedonuseraccount}} | ConvertTo-HTML -head $a –body "<H2>Last Logon Account</H2><title>Last Logon Account</title>" |
Out-file "c:\LastLogonTime\Mailsrv2.htm"
}

Open in new window

0
exsuprtAuthor Commented:
I originally had the " | " at the end of the select-object line as well (forgot to add in my question, sorry) and no it does not work.  I get all the formatting on the file but none of the data.  If at the end of the select-object line I do something like ...>> c:\lastlogontime\mailsrv2.txt i get everything I need but doing the HTML something is wrong.
0
Joe KlimisCommented:
you need to  build an array in the loop and  convert at the end , other wise the file over writes with each loop. here is an example that works, should be easy to convert it back for you script
if you get stuck let me know

$a = "<style>" 
$a = $a + "BODY{background-color:#808080;margin-left:auto; margin-right:auto;text-align:center}" 
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;margin-left:auto; margin-right:auto;text-align:center}" 
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#737CA1;margin-left:auto; margin-right:auto;text-align:center}" 
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#C0C0C0;margin-left:auto; margin-right:auto;text-align:center}" 
$a = $a + "</style>" 
$mydata =  get-wmiobject win32_process |select processname,handle
$results = @()

 foreach ($User in $mydata)
	{ 
	$results += ,$user
	}
	
$results| ConvertTo-HTML -head $a –body "<H2>Last Logon Account</H2><title>Last Logon Account</title>" | Out-file "Mailsrv2.htm"

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Chris DentPowerShell DeveloperCommented:
I would have gone down this line I think.

The use you have of Get-MailboxStatistics is a bit wasteful since you're getting everything then filtering with Where-Object (?).

You should be able to get away with -NotMatch to check for the two users, | is Or in the NotMatch context (Regular Expression).

HTH

Chris
$Alias_Input = "c:\LastLogonTime\Alias_Output.txt"
$Alias_Output = "c:\LastLogonTime\Mailsrv2.htm"

$a = "<style>"
$a = $a + "BODY{background-color:#808080;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#737CA1;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#C0C0C0;margin-left:auto; margin-right:auto;text-align:center}"
$a = $a + "</style>"

Get-Content $Alias_Input | %{
  Get-MailboxStatistics $_ -Server mailsrv2 |
    ?{ $_.LastLogonTime -lt $(Get-Date).AddDays(-1) -And `
      $_.LastLoggedOnUserAccount -NotMatch "user1|user2" } |
    Select-Object @{n="Mailbox";e={ $_.displayname }},
      @{n="Last Logon Time";e={ $_.lastlogontime }}, 
      @{n="Last Logged on User Account";e={$_.lastloggedonuseraccount}}
} | ConvertTo-HTML -head $a –body "<H2>Last Logon Account</H2><title>Last Logon Account</title>" | `
  Out-file "c:\LastLogonTime\Mailsrv2.htm"

Open in new window

0
exsuprtAuthor Commented:
Joe, I have no idea how to integrate that into my script to pull the information I need.

Chris, your solution doesn't seem to be working.  It pulls everything instead of segregating back on the input file and the criteria listed in the select-object.

Maybe this isn't as easy as I thought it would be or I'm still enough of a newbe to powershell that I'm having difficulties editing what you have listed to my script to run.  I still think there should be an easy way to output the results Ive seen when I do a text file but use html instead.
0
Chris DentPowerShell DeveloperCommented:

Start with something easier and build it up, testing all the way. This way, you stand a much better chance of seeing where it's breaking. You're dealing with a small number of changes each time and can become familiar with the syntax.

First one, test the Select, and ConvertTo-Html for just you:

Get-MailboxStatistics "You" | `
  Select DisplayName, LastLogonTime, LastLoggedOnUserAccount | `
  ConvertTo-Html | `
  Out-File "SomeFile.html"

If that works, wrap add the input file into the mix:

Get-Content $Alias_Input | %{ Get-MailboxStatistics $_ } | `
  Select DisplayName, LastLogonTime, LastLoggedOnUserAccount | `
  ConvertTo-Html | `
  Out-File "SomeFile.html"

If you get too many users, consider the effect of blank lines in your input file (because that will mean the above returns everyone). If it's still good, you can add in the condition you had:

Get-Content $Alias_Input | %{
  $Statistics = Get-MailboxStatistics $_
  If ($Statistics.LastLogonTime -lt $(Get-Date).AddDays(-1) -And `
      $Statistics.LastLoggedOnUserAccount -NotMatch "user1|user2")
  {
    # Leave this entry in the output pipeline (otherwise ignore it)
    $Statistics
  }
} | Select DisplayName, LastLogonTime, LastLoggedOnUserAccount | `
  ConvertTo-Html | `
  Out-File "SomeFile.html"

There's not much left to do now, rename a few properties (as you did above), and add the Header to the HTML file.

HTH

Chris
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:
One more little bit...

If you can upgrade to PowerShell 2 you should. Scripts written for PowerShell 1 will work in 2, but the reverse isn't so easy. Many of the examples you find will written for PowerShell 2 these days, this is certainly true of my own blog.

PowerShell 2 will run with the Exchange CmdLets, if you're running on the Exchange Server itself ensure 2007 SP2 is installed. If you run the Exchange tools from your own computer the patch level it's not so important but having the most recent available is always best if you can.

You will find the installer here if you use anything other than Windows 2008 R2 or Windows 7:

http://support.microsoft.com/kb/968929

Chris
0
exsuprtAuthor Commented:
I pretty much got this to work with the exception of the 'NoMatch "user1|user2"' which did not work for me so I went back the original syntax I had to accomplish the same thing.  Either way thank you for your assistance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.