Advertisement
kohijones

Disable/Move inactive computer objects w/email

Feb 8th, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-PSSnapin -Name Quest.ActiveRoles.ADManagement -ea 0
  2. $scriptDate = get-date -f ("MM-dd-yyy hh:mm:ss tt")
  3. #region Function to test if number is even
  4. Function check-even ($num) {[bool]!($num%2)}
  5. #endregion
  6.  
  7. #region Function Get-ScriptDir
  8. function Get-ScriptDirectory {
  9.     if($hostinvocation -ne $null)
  10.     {
  11.         Split-Path $hostinvocation.MyCommand.path
  12.     }
  13.     else
  14.     {
  15.         Split-Path $script:MyInvocation.MyCommand.Path
  16.     }
  17. }
  18. #endregion
  19.  
  20. #region Vars and Props
  21. $QADprops =
  22.     @{N='Computer Name';E={$_.computername}}, `
  23.     @{N='Last Logon TimeStamp';E={$_.lastlogontimestamp}}, `
  24.     @{N='OS Name';E={$_.osname}}, `
  25.     @{N='Parent Container DN';E={$_.parentcontainerdn}};
  26.  
  27. $QADParams =
  28. @{
  29.     sizelimit = "0"
  30.     pagesize = "2000"
  31.     dontusedefaultincludedproperties = $true
  32.     includedproperties = @('ComputerName', 'LastLogonTimeStamp', 'OSName', 'ParentContainerDN')
  33.     searchroot = @('some.domain.com/USA/WST/Windows7','some.domain.com/USA/WST/XP')
  34. }
  35.  
  36. $results = @()
  37. $i = 0
  38. $Currentdate = get-date
  39. $Date = get-date -f ("MM-dd-yyyy")
  40. $Days = 90
  41. $currentDir = Get-ScriptDirectory
  42. $CSV = "$($currentDir)\inactive_computers-$($Date).csv"
  43. $attachment = $false
  44. $numComps = 30
  45. $source = "USA/WST/"
  46. $dest = "USA/WST/Disabled"
  47. #end region
  48.  
  49. #region Email Information
  50. $smtp    = "smtp.some.domain.com"
  51. $from    = "poshalerts@somedoamin.com"  
  52. $to      = "kohijones@somedomain.com",
  53. $subject = "Powershell Script Alert: Move/Disable Inactive Computer Objects"
  54. #endregion
  55.  
  56. #region Call Get-QadComputer
  57. $Comps=Get-QADComputer @QADParams |
  58.            where { $_.LastLogonTimeStamp -ne $Null -and ($Currentdate-$_.LastLogonTimeStamp).Days -gt $Days -and $_.parentcontainer -notlike "*Exclude*" } |
  59.            select $QADprops -ErrorAction 0
  60. #endregion
  61.  
  62. #region HTML Header and CSS
  63. $HeaderHTML =
  64. @"
  65. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
  66. <html><head><title>Powershell Script Alert: Move/Disable Inactive Computer Objects</title>
  67. <style type="text/css">
  68. <!--
  69. body, p, th, td, h1, a:link {font-family:Verdana,Arial;font-size:8.5pt;color:#244F54;text-decoration:none;}
  70. h1 {font-size:12pt;letter-spacing:1pt;word-spacing:1pt;font-weight:bold;}
  71. table, td, th { border: 1px solid #244F54;border-collapse:collapse;padding:4px;}
  72. td.activity {background:#A4BCC2;font-weight:bold;}
  73. td.alert {background:#A4BCC2;font-weight:bold;}
  74. td.result, a:link {color:#244F54;}
  75. tr.odd td {background:#E8F3F8;color:#244F54;}
  76. tr.even td {background:#DBE6EC;color:#244F54;}
  77. p {font-weight:normal;color:#244F54;letter-spacing:1pt;word-spacing:1pt;font-size:9px;}
  78. -->
  79. </style>
  80. </head>
  81. <body>
  82. "@
  83. #endregion
  84.  
  85. #region Main Loop
  86. foreach ($comp in $comps) {
  87.     ForEach-Object { $i++; $_ }
  88.     # Construct alternate td colors
  89.     if (check-even $i) {
  90.         $results += "<tr class='even'><td>$($comp.'Computer Name'.Replace('$', ''))</td><td>Computer</td><td>$($dest)</td></tr>"
  91.     }
  92.     else {
  93.         $results += "<tr class='odd'><td>$($comp.'Computer Name'.Replace('$', ''))</td><td>Computer</td><td>$($dest)</td></tr>"
  94.     }
  95.     # Disable AD Objects
  96.     Disable-QADComputer -Identity $comp.'Computer Name' -ErrorAction 0 | out-null
  97.     write-host "Disabling: $($comp.'Computer Name'.Replace('$', ''))"
  98.     Update AD Object description with original ou/dn
  99.     Set-QADComputer $comp.'Computer Name' -ObjectAttributes @{description=$comp.'Parent Container DN'}
  100.     write-host "Updating description: $($comp.'Parent Container DN')"
  101.   # Move AD Objects
  102.   Move-QADObject -Identity $comp."Computer Name" -To "some.domain.com/$($dest)" -ErrorAction 0 | out-null
  103.   write-host "Moving: $($comp.'Computer Name'.Replace('$', ''))"
  104.  
  105. }
  106. #endregion
  107.  
  108. #region HTML Body
  109. $BodyHTML =
  110. @"
  111. <h1>Powershell Script Alert</h1>
  112. <table>
  113. <tr><td class='alert'>Execution Time:</td><td class='result'>$($scriptDate)</td></tr>
  114. <tr><td class='alert'>Script Server:</td><td class='result'>$($env:computername)</td></tr>
  115. <tr><td class='alert'>Job Type:</td><td class='result'>AD Object</td></tr>
  116. <tr><td class='alert'>Job Action:</td><td class='result'>Move Inactive Computers</td></tr>
  117. <tr><td class='alert'>Inactive:</td><td class='result'>$($days) Days</td></tr>
  118. <tr><td class='alert'>Source:</td><td class='result'>$($source)</td></tr>
  119. <tr><td class='alert'>Destination:</td><td class='result'>$($dest)</td></tr>
  120. <tr><td class='alert'>Job Status:</td><td class='result'>Successful</td></tr>
  121. <tr><td class='alert'>Contact:</td><td class='result'><a href="mailto:kohijones@somedomain.com?subject=Move Inactive Computers">kohi jones</a> - 555-555-555</td></tr>
  122. </table>
  123. <h1>Activity Log</h1>
  124. <p>
  125. ($($comps.Count)) Computer(s) moved from $($source) OU.<br>
  126. </p>
  127. <table>
  128. "@
  129. #endregion
  130.  
  131. #region Loop $results @()
  132. if ($comps.Count -gt $numComps) {
  133.     $attachment = $true
  134.     $comps | Export-csv $CSV -notype    
  135.     $BodyHTML += "<tr><td><h1>Please see attached csv file.</h1></td></tr>"
  136.    }
  137. else {
  138.     $BodyHTML += "<tr><td class='activity'>Name</td><td class='activity'>Type</td><td class='activity'>Destiniation OU</td></tr>"
  139. foreach ($result in $results) {
  140.     $bodyHTML += $result
  141.    }
  142. }
  143. #endregion
  144.  
  145. #region HTML Footer
  146. $FooterHTML =
  147. @"
  148. </table>
  149. <p>
  150. Report Generated: $(get-date -f ("MM-dd-yyy hh:mm:ss tt"))
  151. </div>
  152. </body>
  153. </html>
  154. "@
  155. #endregion
  156.  
  157. #region Construct HTML
  158.     $MessageHTML = $HeaderHTML + $bodyHTML + $FooterHTML
  159. #endregion
  160.  
  161. #region Email Report
  162. $emailParams =
  163. @{
  164.     to = $to  
  165.     from = $from
  166.     subject = $subject
  167.     smtpserver = $smtp
  168.     body = ($MessageHTML | Out-String)
  169.     bodyashtml = $true
  170. }
  171.  
  172. if ($comps -ne $null){
  173.     if ($attachment) {
  174.         Send-MailMessage @emailParams -Attachments $CSV }
  175.     else {
  176.         Send-MailMessage @emailParams
  177.    }
  178. }
  179. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement