• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1308
  • Last Modified:

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"
}
0
exsuprt
Asked:
exsuprt
  • 3
  • 3
  • 2
1 Solution
 
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
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now