Script to include error checking

Hi All,

Hopefully someone can help me with this.  I'm quite new to powershell and dont really know much about it.  A bit of background on what i want to achieve.  

At the end of each server build (vm or physical), I would like to run a script which does a full inventory of our windows servers and checks for anything which is not configured / installed.  So for example once the server is build, I would like to check what windows features are installed / GPO's are applied / windows activation status etc and then i want the script to create a HTML report and highlight anything it finds which could be missing or not configured.  I understand this is quite vague but for the time being i would just like a generic check and then can fine tune it later.  I have managed to get the following script from another site and added a few modifications myself but need to now get it doing what i want:

Set-ExecutionPolicy RemoteSigned -ErrorAction SilentlyContinue 

$UserName = (Get-Item  env:\username).Value
$ComputerName = (Get-Item env:\Computername).Value 
#$filepath = (Get-ChildItem env:\userprofile).value
$share = "\\files\general$\Operations\Build Reports"

#ReportDate 
$ReportDate = Get-Date | Select -Property DateTime |ConvertTo-Html -Fragment 

#General Information 
$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem |  
Select -Property Model , Manufacturer , Description , PrimaryOwnerName , SystemType |ConvertTo-Html -Fragment 

#Operating System Information 
$OS = Get-WmiObject -Class Win32_OperatingSystem | Select -Property Caption , CSDVersion , OSArchitecture , OSLanguage | ConvertTo-Html -Fragment 

#Windows Activation Status
$Activation = Get-CimInstance -ClassName SoftwareLicensingProduct | where PartialProductKey | Select Name, @{Label="Grace period (days)"; Expression={ $_.graceperiodremaining / 1440}}, @{Label= "License Status"; Expression={switch (foreach {$_.LicenseStatus}) { 0 {"Unlicensed"} 1 {"Licensed"} 2 {"Out-Of-Box Grace Period"} 3 {"Out-Of-Tolerance Grace Period"} 4 {"Non-Genuine Grace Period"} 5 {"Notification Period"} 6 {"Extended Grace"} } } } | ConvertTo-Html -Fragment 

#Logical Disk Information 
$Disk = Get-WmiObject -Class Win32_LogicalDisk -Filter DriveType=3 | 
Select SystemName , DeviceID , @{Name=size(GB);Expression={{0:N1} -f($_.size/1gb)}}, @{Name=freespace(GB);Expression={{0:N1} -f($_.freespace/1gb)}} | 
ConvertTo-Html -Fragment 

#Time Zone Information 
$TimeZone = Get-WmiObject -Class Win32_TimeZone | Select Caption , StandardName | ConvertTo-Html -Fragment 

#CPU Information 
$SystemProcessor = Get-WmiObject -Class Win32_Processor  |  Select SystemName , Name , MaxClockSpeed , Manufacturer , status |ConvertTo-Html -Fragment

#Memory Information 
$PhysicalMemory = Get-WmiObject -Class Win32_PhysicalMemory | Select -Property Tag , SerialNumber , PartNumber , Manufacturer , DeviceLocator , @{Name="Capacity(GB)";Expression={"{0:N1}" -f ($_.Capacity/1GB)}} | ConvertTo-Html -Fragment 

#Software Inventory 
$Software = Get-WmiObject -Class Win32_Product | Select Name , Vendor , Version , Caption | ConvertTo-Html -Fragment  

#Windows Features Inventory
$Features = Get-WmiObject -query "select * from win32_optionalfeature where installstate=1" | select Name, Caption | ConvertTo-Html -Fragment

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Report Generated By</B></H4></font>$username
<font color = blue><H4><B>General Information</B></H4></font>$ComputerSystem 
<font color = blue><H4><B>Operating System Information</B></H4></font>$OS 
<font color = blue><H4><B>Activation Status</B></H4></font>$Activation
<font color = blue><H4><B>Time Zone Information</B></H4></font>$TimeZone 
<font color = blue><H4><B>Disk Information</B></H4></font>$Disk 
<font color = blue><H4><B>Processor Information</B></H4></font>$SystemProcessor 
<font color = blue><H4><B>Memory Information</B></H4></font>$PhysicalMemory 
<font color = blue><H4><B>Windows Features Iventory</B></H4></font>$Features
<font color = blue><H4><B>Software Inventory</B></H4></font>$Software" -CssUri  "$share\style.CSS" -Title "Server Inventory" | Out-File "$share\$ComputerName.html" 

Write-Host "Script Execution Completed" -ForegroundColor Yellow 
Invoke-Item -Path "$share\$ComputerName.html"

So what I would like is in the HTML report, anything that is not done right is highlighted in Red with a quick sentence at the end stating the issue.  So for example where this script checks the Windows Activation status, I would like that any status which is reported that is not "Licensed" is highlighted red in the HTML report and a sentence at the end of the report which reads out something like "Windows is not activated.  Please activate".  All our base build have a certain set of windows features installed, so the  script could reference a set of features installed on a current system (possibly output the windows features from a correctly built system which is output to a file and then compared on the newly built server) and check what is missing and again highlight in red (for example "Failover clustering is not Installed" etc.  

I would appreciate any help that could be provided

Thanks



  • Edited by jutler123 Tuesday, August 19, 2014 3:21 PM
August 19th, 2014 6:16pm

There are so many things that, in your environment, you might want to check to verify a server build that it is highly unlikely that anyone else does, or would want to do, the same tests.

You will probably have better luck asking separate questions about how to do some of the specific checks you want to run, and, possibly, how to use colour-coding to highlight in HTML.

I'll start you off by pointing out that what you make look complex can be done in a much simpler manner. For example, how you get the username from the username environment variable:

PS C:\Users\Al> $UserName = (Get-Item  env:\username).Value
PS C:\Users\Al> $username | gm | select typename -first 1

TypeName
--------
System.String


PS C:\Users\Al>

It is good that you are concerned with the type of value assigned, but there is a simpler way that works for all environment variables (all of which are strings):

PS C:\Users\Al> $UserName = $env:username
PS C:\Users\Al> $username | gm | select typename -first 1

TypeName
--------
System.String


PS C:\Users\Al>

This also returns a string, but, in the process, the code is simpler and a bit more intuitive, at least in my opinion.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2014 12:52am

There's a method for color-coding and formatting html using xml that looks perfect for your scenario since you're already using convertto-html:

http://tech-comments.blogspot.com/2012/07/powershell-dynamically-color-posh.html

I've used this method to analyze HTML reports generated by someone, too, it's very handy.

August 20th, 2014 8:57am

Use Desired State Configuration in powershell 4(http://blogs.technet.com/b/privatecloud/archive/2013/08/30/introducing-powershell-desired-state-configuration-dsc.aspx)

In powershell 3 you have to design and write this mechanism yourself.

This code can help you to highlight some cells in your report:

http://community.spiceworks.com/scripts/show/2450-change-cell-color-in-html-table-with-powershell-set-cellcolor

Good luck)


Free Windows Admin Tool Kit Click here and download it now
August 20th, 2014 12:40pm

Use Desired State Configuration in powershell 4(http://blogs.technet.com/b/privatecloud/archive/2013/08/30/introducing-powershell-desired-state-configuration-dsc.aspx)

In powershell 3 you have to design and write this mechanism yourself.

This code can help you to highlight some cells in your report:

http://community.spiceworks.com/scripts/show/2450-change-cell-color-in-html-table-with-powershell-set-cellcolor

Good luck)


August 20th, 2014 7:37pm

Hi All,

Hopefully someone can help me with this.  I'm quite new to powershell and dont really know much about it.  A bit of background on what i want to achieve.  

At the end of each server build (vm or physical), I would like to run a script which does a full inventory of our windows servers and checks for anything which is not configured / installed.  So for example once the server is build, I would like to check what windows features are installed / GPO's are applied / windows activation status etc and then i want the script to create a HTML report and highlight anything it finds which could be missing or not configured.  I understand this is quite vague but for the time being i would just like a generic check and then can fine tune it later.  I have managed to get the following script from another site and added a few modifications myself but need to now get it doing what i want:

Set-ExecutionPolicy RemoteSigned -ErrorAction SilentlyContinue 

$UserName = (Get-Item  env:\username).Value
$ComputerName = (Get-Item env:\Computername).Value 
#$filepath = (Get-ChildItem env:\userprofile).value
$share = "\\files\general$\Operations\Build Reports"

#ReportDate 
$ReportDate = Get-Date | Select -Property DateTime |ConvertTo-Html -Fragment 

#General Information 
$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem |  
Select -Property Model , Manufacturer , Description , PrimaryOwnerName , SystemType |ConvertTo-Html -Fragment 

#Operating System Information 
$OS = Get-WmiObject -Class Win32_OperatingSystem | Select -Property Caption , CSDVersion , OSArchitecture , OSLanguage | ConvertTo-Html -Fragment 

#Windows Activation Status
$Activation = Get-CimInstance -ClassName SoftwareLicensingProduct | where PartialProductKey | Select Name, @{Label="Grace period (days)"; Expression={ $_.graceperiodremaining / 1440}}, @{Label= "License Status"; Expression={switch (foreach {$_.LicenseStatus}) { 0 {"Unlicensed"} 1 {"Licensed"} 2 {"Out-Of-Box Grace Period"} 3 {"Out-Of-Tolerance Grace Period"} 4 {"Non-Genuine Grace Period"} 5 {"Notification Period"} 6 {"Extended Grace"} } } } | ConvertTo-Html -Fragment 

#Logical Disk Information 
$Disk = Get-WmiObject -Class Win32_LogicalDisk -Filter DriveType=3 | 
Select SystemName , DeviceID , @{Name=size(GB);Expression={{0:N1} -f($_.size/1gb)}}, @{Name=freespace(GB);Expression={{0:N1} -f($_.freespace/1gb)}} | 
ConvertTo-Html -Fragment 

#Time Zone Information 
$TimeZone = Get-WmiObject -Class Win32_TimeZone | Select Caption , StandardName | ConvertTo-Html -Fragment 

#CPU Information 
$SystemProcessor = Get-WmiObject -Class Win32_Processor  |  Select SystemName , Name , MaxClockSpeed , Manufacturer , status |ConvertTo-Html -Fragment

#Memory Information 
$PhysicalMemory = Get-WmiObject -Class Win32_PhysicalMemory | Select -Property Tag , SerialNumber , PartNumber , Manufacturer , DeviceLocator , @{Name="Capacity(GB)";Expression={"{0:N1}" -f ($_.Capacity/1GB)}} | ConvertTo-Html -Fragment 

#Software Inventory 
$Software = Get-WmiObject -Class Win32_Product | Select Name , Vendor , Version , Caption | ConvertTo-Html -Fragment  

#Windows Features Inventory
$Features = Get-WmiObject -query "select * from win32_optionalfeature where installstate=1" | select Name, Caption | ConvertTo-Html -Fragment

ConvertTo-Html -Body "<font color = blue><H4><B>Report Executed On</B></H4></font>$ReportDate 
<font color = blue><H4><B>Report Generated By</B></H4></font>$username
<font color = blue><H4><B>General Information</B></H4></font>$ComputerSystem 
<font color = blue><H4><B>Operating System Information</B></H4></font>$OS 
<font color = blue><H4><B>Activation Status</B></H4></font>$Activation
<font color = blue><H4><B>Time Zone Information</B></H4></font>$TimeZone 
<font color = blue><H4><B>Disk Information</B></H4></font>$Disk 
<font color = blue><H4><B>Processor Information</B></H4></font>$SystemProcessor 
<font color = blue><H4><B>Memory Information</B></H4></font>$PhysicalMemory 
<font color = blue><H4><B>Windows Features Iventory</B></H4></font>$Features
<font color = blue><H4><B>Software Inventory</B></H4></font>$Software" -CssUri  "$share\style.CSS" -Title "Server Inventory" | Out-File "$share\$ComputerName.html" 

Write-Host "Script Execution Completed" -ForegroundColor Yellow 
Invoke-Item -Path "$share\$ComputerName.html"

So what I would like is in the HTML report, anything that is not done right is highlighted in Red with a quick sentence at the end stating the issue.  So for example where this script checks the Windows Activation status, I would like that any status which is reported that is not "Licensed" is highlighted red in the HTML report and a sentence at the end of the report which reads out something like "Windows is not activated.  Please activate".  All our base build have a certain set of windows features installed, so the  script could reference a set of features installed on a current system (possibly output the windows features from a correctly built system which is output to a file and then compared on the newly built server) and check what is missing and again highlight in red (for example "Failover clustering is not Installed" etc.  

I would appreciate any help that could be provided

Thanks



It's ridiculousy simple.

For instance, rite after you calculated $Activation, write this code, to include the colors:

$Activation = $Activation `
    -replace '(?s)<td>Licensed</td>' , '<td bgcolor=green>Licensed</td>' `
    -replace '(?s)<td>Unlicensed</td>' , '<td bgcolor=red>Unlicensed</td>'

You'll get green background if it's Licensed, or red background if it's Unlicensed.

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2014 2:02am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics