Advertisement
Guest User

Get-Java.ps1

a guest
Jan 23rd, 2014
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. Usage: Get-Java.ps1 -f <AD filter> -e <If -e is present, email will be sent with the report as attachment>
  3. #>
  4. [CmdletBinding()]
  5.     param (
  6.         [Parameter(Mandatory=$true, HelpMessage="Specify the Get-ADComputer filter to apply (Use * for wildcard")]
  7.         [Alias("f")]
  8.         [string] $Filter,
  9.        
  10.         [Parameter(Mandatory=$false)]
  11.         [Alias("e")]
  12.         [switch] $email
  13.     )
  14. $Rev =  "v1.0 21 Jan 2014"
  15. #$ErrorActionPreference="continue"
  16. cls
  17. Write-Host "********************************************"   -Fore Green
  18. Write-Host "Get-Java.ps1: Retrieves Java information" -Fore Green
  19. Write-Host "Written By: Tyler Applebaum" -Fore Green
  20. Write-Host "$Rev" -Fore Green
  21. Write-Host "********************************************"   -Fore Green
  22. Write-Host " "
  23. Write-Host " "
  24.  
  25.  
  26. If ( ! (Get-module ActiveDirectory )) {
  27. Import-Module ActiveDirectory
  28. } #include AD module
  29.  
  30. #Company name
  31. $Company = "Company Name"
  32.  
  33. #Email Properties
  34. $date = get-date
  35. $recipients = "IT Dept. <itdept@company.com>", "User Two <Usertwo@company.com>"
  36. $from = "IT Dept. <itdept@company.com>"
  37. $emailserver = "exchange.company.com"
  38. $body = "Java Version Report $date"
  39. $subject = "Java Version Report"
  40.    
  41. #Path to the report
  42. $reportPath = "C:\";
  43.  
  44. #Report name
  45. $reportName = "Java_Report$(get-date -format ddMMyyyy).html";
  46.  
  47. #Colors
  48. $whiteColor = "#FFFFFF"
  49. $redColor = "#FF4500"
  50. $yellowColor = "#CCCC00"   
  51.  
  52. # Path and Report name together (for emailing purposes)
  53. $JavaReport = $reportPath + $reportName
  54.    
  55. # Remove the report if it has already been run today so it does not append to the existing report
  56. If (Test-Path $JavaReport){
  57.     Remove-Item $JavaReport
  58. }
  59.  
  60. #Gather a list of all computers in AD
  61. $list = Get-ADComputer -Filter {Name -Like $Filter} | select -ExpandProperty Name
  62.  
  63. #Format the list
  64. $computers = $list | where {$_ -notlike $null } | foreach { $_.trim() }
  65.  
  66. # Create and write HTML Header of report
  67. $titleDate = get-date -uformat "%m-%d-%Y - %A"
  68. $header = "
  69.         <html>
  70.         <head>
  71.         <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
  72.         <title>Java Report</title>
  73.         <STYLE TYPE='text/css'>
  74.         <!--
  75.         td {
  76.             font-family: Tahoma;
  77.             font-size: 12px;
  78.             border-top: 1px solid #999999;
  79.             border-right: 1px solid #999999;
  80.             border-bottom: 1px solid #999999;
  81.             border-left: 1px solid #999999;
  82.             padding-top: 0px;
  83.             padding-right: 0px;
  84.             padding-bottom: 0px;
  85.             padding-left: 0px;
  86.         }
  87.         body {
  88.             margin-left: 5px;
  89.             margin-top: 5px;
  90.             margin-right: 0px;
  91.             margin-bottom: 10px;
  92.             table {
  93.             border: thin solid #000000;
  94.         }
  95.         -->
  96.         </style>
  97.         </head>
  98.         <body>
  99.         <table width='1000'>
  100.         <tr bgcolor='#CCCCCC'>
  101.         <td colspan='7' height='25' align='center'>
  102.         <font face='tahoma' color='#003399' size='4'><strong>$Company Java Version Report for $titledate</strong></font>
  103.         </td>
  104.         </tr>
  105.         </table>
  106. "
  107. Add-Content $JavaReport $header
  108.  
  109. # Create and write Table header for report
  110.  $tableHeader = "
  111. <table width='1000'><tbody>
  112.     <tr bgcolor=#CCCCCC>
  113.    <td width='125' align='center'>Computer</td>
  114.     <td width='65' align='center'>Platform</td>
  115.     <td width='225' align='center'>Java 7 x86</td>
  116.     <td width='225' align='center'>Java 7 x64</td>
  117.     <td width='225' align='center'>Java 6 x86</td>
  118.     <td width='225' align='center'>Java 6 x64</td>
  119.     </tr>
  120. "
  121. Add-Content $JavaReport $tableHeader
  122.  
  123. #Determine x86 or x64 platform
  124. Function script:Bitness {
  125.     If (Test-Path "\\$WS\C$\Program Files (x86)"){
  126.         $script:OSVer = "64-bit"
  127.     }  
  128.     Else {
  129.         $script:OSVer = "32-bit"
  130.     }
  131. }
  132.  
  133. #Detect Java 6 and 7. Detects multiple installations of different versions and platforms. Not tested with static installations. :(
  134. Function script:JavaDetect {
  135.     If ($OSVer = "64-bit"){
  136.         $java732 = "\\$WS\C$\Program Files (x86)\Java\jre7\bin\java.exe"
  137.         $java764 = "\\$WS\C$\Program Files\Java\jre7\bin\java.exe"
  138.         $java632 = "\\$WS\C$\Program Files (x86)\Java\jre6\bin\java.exe"
  139.         $java664 = "\\$WS\C$\Program Files\Java\jre6\bin\java.exe"
  140.             If (Test-Path $java732){
  141.                 $script:javaver732 = gci $java732 | Select -exp versioninfo | Select ProductName
  142.                 $script:javaver732 = $script:javaver732.ProductName
  143.             }
  144.             Else {
  145.                 $script:javaver732 = "Java 7 32-bit not found"
  146.             }  
  147.             If (Test-Path $java764){
  148.                 $script:javaver764 = gci $java764 | Select -exp versioninfo | Select ProductName
  149.                 $script:javaver764 = $script:javaver764.ProductName
  150.             }
  151.             Else {
  152.                 $script:javaver764 = "Java 7 64-bit not found"
  153.             }
  154.             If (Test-Path $java632){
  155.                 $script:javaver632 = gci $java632 | Select -exp versioninfo | Select ProductName
  156.                 $script:javaver632 = $script:javaver632.ProductName
  157.             }
  158.             Else {
  159.                 $script:javaver632 = "Java 6 32-bit not found"
  160.             }
  161.             If (Test-Path $java664){
  162.                 $script:javaver664 = gci $java664 | Select -exp versioninfo | Select ProductName
  163.                 $script:javaver664 = $script:javaver664.ProductName
  164.             }
  165.             Else {
  166.                 $script:javaver664 = "Java 6 64-bit not found"
  167.             }
  168.                    
  169.     }
  170.  
  171.     Else {
  172.         $java732 = "\\$WS\C$\Program Files\Java\jre7\bin\java.exe"
  173.         $java632 = "\\$WS\C$\Program Files\Java\jre6\bin\java.exe"
  174.             If (Test-Path $java732){
  175.                 $script:javaver732 = gci $java732 | Select -exp versioninfo | Select ProductName
  176.                 $script:javaver732 = $script:javaver732.ProductName
  177.             }
  178.             Else {
  179.                 $script:javaver732 = "Java 7 32-bit not found"
  180.             }
  181.             If (Test-Path $java632){
  182.                 $script:javaver632 = gci $java632 | Select -exp versioninfo | Select ProductName
  183.                 $script:javaver632 = $script:javaver632.ProductName
  184.             }
  185.             Else {
  186.                 $script:javaver632 = "Java 6 32-bit not found"
  187.             }
  188.     }
  189. }
  190.  
  191. #Start doin work
  192. Foreach ($WS in $computers){
  193.     If (Test-Connection -count 1 -computer $WS -quiet) {
  194.         Bitness
  195.         JavaDetect
  196.         Write-Host $WS.ToUpper()
  197.         Write-Host $JavaVer732
  198.         Write-Host $JavaVer764
  199.         Write-Host $JavaVer632
  200.         Write-Host $JavaVer664
  201.         Write-Host `n
  202.        
  203.         $Color = $WhiteColor
  204.         If ($JavaVer732 -notlike "*not found*"){
  205.         $color1 = $yellowColor
  206.         }
  207.         If ($JavaVer764 -notlike "*not found*"){
  208.         $color2 = $yellowColor
  209.         }
  210.         If ($JavaVer632 -notlike "*not found*"){
  211.         $color3 = $redColor
  212.         }
  213.         If ($JavaVer664 -notlike "*not found*"){
  214.         $color4 = $redColor
  215.         }
  216.  
  217. #Create table data rows
  218.     $dataRow = "
  219.         <tr>
  220.        <td width='125' align='center'>$WS</td>
  221.         <td width='65' align='center'>$OSVer</td>
  222.         <td width='225' bgcolor=`'$color1`'>$JavaVer732</td>
  223.         <td width='225' bgcolor=`'$color2`'>$JavaVer764</td>
  224.         <td width='225' bgcolor=`'$color3`'>$JavaVer632</td>
  225.         <td width='225' bgcolor=`'$color4`'>$JavaVer664</td>
  226.         </tr>
  227.     "
  228. Add-Content $JavaReport $dataRow;
  229.  
  230. #reset colors
  231.     sv color1 $whiteColor
  232.     sv color2 $whiteColor
  233.     sv color3 $whiteColor
  234.     sv color4 $whiteColor
  235.  
  236.     }#endif test-connection
  237.    
  238.     Else {
  239.         Write-Host "Unable to connect to $WS. Please check the name and try again." -back Black -fore Yellow
  240.     }#endelse test-connection
  241. }#end foreach
  242.  
  243. #Add closing HTML tags
  244. Add-Content $JavaReport $tableDescription
  245. Add-Content $JavaReport "</body></html>"
  246.  
  247. If (Test-Path $JavaReport){
  248.     Write-Host "Report created successfully!"
  249.     ii $JavaReport
  250. }
  251.  
  252. $script:MailMessage = @{
  253. To = $recipients
  254. From = $from
  255. Subject = $Subject
  256. Body = $body
  257. Attachments = $JavaReport
  258. Smtpserver = $emailserver
  259. }
  260.  
  261. If ($email) {
  262. Send-MailMessage @MailMessage
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement