Guest User

Get-Java.ps1

a guest
Jan 21st, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $Rev =  "v1.0 21 Jan 2014"
  2. #$ErrorActionPreference="continue"
  3. cls
  4. Write-Host "********************************************"   -Fore Green
  5. Write-Host "Get-Java.ps1: Retrieves Java information" -Fore Green
  6. Write-Host "Written By: Tyler Applebaum" -Fore Green
  7. Write-Host "$Rev" -Fore Green
  8. Write-Host "********************************************"   -Fore Green
  9. Write-Host " "
  10. Write-Host " "
  11.  
  12. If ( ! (Get-module ActiveDirectory )) {
  13. Import-Module ActiveDirectory
  14. } #include AD module
  15.  
  16. #Company name
  17.     $Company = "Company Name Here"
  18.    
  19. # Path to the report
  20.     $reportPath = "C:\";
  21.  
  22. # Report name
  23.     $reportName = "Java_Report$(get-date -format ddMMyyyy).html";
  24.  
  25. # Path and Report name together (for emailing purposes)
  26.     $JavaReport = $reportPath + $reportName
  27.    
  28. # Remove the report if it has already been run today so it does not append to the existing report
  29. If (Test-Path $JavaReport){
  30.     Remove-Item $JavaReport
  31. }
  32.  
  33. #Gather a list of all computers in AD
  34. $list = Get-ADComputer -Filter {Name -Like "*"} | select -ExpandProperty Name
  35.  
  36. #Format the list
  37. $computers = $list | where {$_ -notlike $null } | foreach { $_.trim() }
  38.  
  39. # Create and write HTML Header of report
  40. $titleDate = get-date -uformat "%m-%d-%Y - %A"
  41. $header = "
  42.         <html>
  43.         <head>
  44.         <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
  45.         <title>Java Report</title>
  46.         <STYLE TYPE='text/css'>
  47.         <!--
  48.         td {
  49.             font-family: Tahoma;
  50.             font-size: 12px;
  51.             border-top: 1px solid #999999;
  52.             border-right: 1px solid #999999;
  53.             border-bottom: 1px solid #999999;
  54.             border-left: 1px solid #999999;
  55.             padding-top: 0px;
  56.             padding-right: 0px;
  57.             padding-bottom: 0px;
  58.             padding-left: 0px;
  59.         }
  60.         body {
  61.             margin-left: 5px;
  62.             margin-top: 5px;
  63.             margin-right: 0px;
  64.             margin-bottom: 10px;
  65.             table {
  66.             border: thin solid #000000;
  67.         }
  68.         -->
  69.         </style>
  70.         </head>
  71.         <body>
  72.         <table width='1000'>
  73.         <tr bgcolor='#CCCCCC'>
  74.         <td colspan='7' height='25' align='center'>
  75.         <font face='tahoma' color='#003399' size='4'><strong>$Company Java Version Report for $titledate</strong></font>
  76.         </td>
  77.         </tr>
  78.         </table>
  79. "
  80. Add-Content $JavaReport $header
  81.  
  82. # Create and write Table header for report
  83.  $tableHeader = "
  84. <table width='1000'><tbody>
  85.     <tr bgcolor=#CCCCCC>
  86.    <td width='95' align='center'>Computer</td>
  87.     <td width='95' align='center'>Platform</td>
  88.     <td width='225' align='center'>Java 7 x86</td>
  89.     <td width='225' align='center'>Java 7 x64</td>
  90.     <td width='225' align='center'>Java 6 x86</td>
  91.     <td width='225' align='center'>Java 6 x64</td>
  92.     </tr>
  93. "
  94. Add-Content $JavaReport $tableHeader
  95.  
  96. #Determine x86 or x64 platform
  97. Function script:Bitness {
  98.     If (Test-Path "\\$WS\C$\Program Files (x86)"){
  99.         $script:OSVer = "64-bit"
  100.     }  
  101.     Else {
  102.         $script:OSVer = "32-bit"
  103.     }
  104. }
  105.  
  106. #Detect Java 6 and 7. Detects multiple installations of different versions and platforms. Not tested with static installations. :(
  107. Function script:JavaDetect {
  108.     If ($OSVer = "64-bit"){
  109.         $java732 = "\\$WS\C$\Program Files (x86)\Java\jre7\bin\java.exe"
  110.         $java764 = "\\$WS\C$\Program Files\Java\jre7\bin\java.exe"
  111.         $java632 = "\\$WS\C$\Program Files (x86)\Java\jre6\bin\java.exe"
  112.         $java664 = "\\$WS\C$\Program Files\Java\jre6\bin\java.exe"
  113.             If (Test-Path $java732){
  114.                 $script:javaver732 = gci $java732 | Select -exp versioninfo | Select ProductName
  115.                 $script:javaver732 = $script:javaver732.ProductName
  116.             }
  117.             Else {
  118.                 $script:javaver732 = "Java 7 32-bit not found"
  119.             }  
  120.             If (Test-Path $java764){
  121.                 $script:javaver764 = gci $java764 | Select -exp versioninfo | Select ProductName
  122.                 $script:javaver764 = $script:javaver764.ProductName
  123.             }
  124.             Else {
  125.                 $script:javaver764 = "Java 7 64-bit not found"
  126.             }
  127.             If (Test-Path $java632){
  128.                 $script:javaver632 = gci $java632 | Select -exp versioninfo | Select ProductName
  129.                 $script:javaver632 = $script:javaver632.ProductName
  130.             }
  131.             Else {
  132.                 $script:javaver632 = "Java 6 32-bit not found"
  133.             }
  134.             If (Test-Path $java664){
  135.                 $script:javaver664 = gci $java664 | Select -exp versioninfo | Select ProductName
  136.                 $script:javaver664 = $script:javaver664.ProductName
  137.             }
  138.             Else {
  139.                 $script:javaver664 = "Java 6 64-bit not found"
  140.             }
  141.                    
  142.     }
  143.  
  144.     Else {
  145.         $java732 = "\\$WS\C$\Program Files\Java\jre7\bin\java.exe"
  146.         $java632 = "\\$WS\C$\Program Files\Java\jre6\bin\java.exe"
  147.             If (Test-Path $java732){
  148.                 $script:javaver732 = gci $java732 | Select -exp versioninfo | Select ProductName
  149.                 $script:javaver732 = $script:javaver732.ProductName
  150.             }
  151.             Else {
  152.                 $script:javaver732 = "Java 7 32-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.     }
  162. }
  163.  
  164. #Start doin work
  165. Foreach ($WS in $computers){
  166.     If (Test-Connection -count 1 -computer $WS -quiet) {
  167.         Bitness
  168.         JavaDetect
  169.         Write-Host $WS.ToUpper()
  170.         Write-Host $JavaVer732
  171.         Write-Host $JavaVer764
  172.         Write-Host $JavaVer632
  173.         Write-Host $JavaVer664
  174.         Write-Host `n
  175.            
  176. #Create table data rows
  177.     $dataRow = "
  178.         <tr>
  179.        <td width='95' align='center'>$WS</td>
  180.         <td width='95' align='center'>$OSVer</td>
  181.         <td width='225'>$JavaVer732</td>
  182.         <td width='225'>$JavaVer764</td>
  183.         <td width='225'>$JavaVer632</td>
  184.         <td width='225'>$JavaVer664</td>
  185.         </tr>
  186. "
  187. Add-Content $JavaReport $dataRow;
  188.  
  189.     }#endif test-connection
  190.    
  191.     Else {
  192.     Write-Host "Unable to connect to $WS. Please check the name and try again." -back Black -fore Yellow
  193.     }#endelse test-connection
  194. }#end foreach
  195.  
  196. #Add closing HTML tags
  197. Add-Content $JavaReport $tableDescription
  198. Add-Content $JavaReport "</body></html>"
  199.  
  200. If (Test-Path $JavaReport){
  201.     Write-Host "Report created successfully!"
  202.     ii $JavaReport
  203. }
Advertisement
Add Comment
Please, Sign In to add comment