Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.69 KB | None | 0 0
  1.  
  2. $ping = new-object system.net.networkinformation.ping
  3. #>>>>>>>$login = Get-Credential
  4.  
  5. # A list of computers to process.
  6. #>>>>>>>$Computers = get-Content $args[0]
  7. $ArquivoSaida = "C:\windows\temp\HVERAO_$target.log" #$args[1]
  8. #>>>>>>>ECHO $ArquivoSaida
  9. # Cabeçalho do arquivo de saída
  10. Write-Output "HOSTNAME;CONFIGURAÇÃO HV;TIME ZONE;DLBIAS;DATA ATUAL;HORA ATUAL;DIFERENÇA EM MINUTOS;INÍCIO HV;TERMINHO HV" | Out-File $ArquivoSaida -Append
  11.  
  12. # The purpose here is to have clean output to the console (or redirected to a file).
  13. $Computers | ForEach-Object {
  14.  
  15. # This is needed for the error handling in the catch block where $_ gets
  16. # set to the last error rather than the computer name.
  17. $Computer = $_
  18.  
  19. try {
  20. # VALIDA CONECTIVIDADE VIA PING
  21.  
  22. $pingreturns = $ping.send(${Computer})
  23. if ($pingreturns.Status -eq 'Success'){
  24. ##### SE PINGAR
  25. ## 1 ##
  26.  
  27.  
  28. # Since certain WMI errors are terminating, we need a try/catch statement.
  29. try {
  30.  
  31. # Here we specify the WMI query
  32. $Searcher = [WmiSearcher]'Select * From Win32_TimeZone'
  33.  
  34. # This is where we set the timeout to 1 seconds.
  35. $Searcher.Options.TimeOut = "0:0:1"
  36.  
  37. # Create necessary objects, specify a scope and namespace for the remote computer (or localhost or '.'),
  38. # then set the scope on the WMISearcher object, which is usually "\\computername\root\cimv2".
  39. $ConnectionOptions = New-Object Management.ConnectionOptions
  40. $ConnectionOptions.Username = $login.UserName
  41. $ConnectionOptions.SecurePassword = $login.Password
  42. $ManagementScope = New-Object Management.ManagementScope("\\${Computer}\root\cimv2", $ConnectionOptions)
  43.  
  44.  
  45.  
  46.  
  47. # Here we specify the WMI query TIME ZONE and Daylight
  48. $Searcher = [WmiSearcher]'Select * From Win32_TimeZone'
  49. # This is where we set the timeout to 5 seconds.
  50. $Searcher.Options.TimeOut = "0:0:1"
  51. $Searcher.Scope = $ManagementScope
  52.  
  53. # Perform the query and retrieve the desired property, and expand it to a
  54. # string with -ExpandProperty.
  55. $TZONE = $Searcher.Get() | Select-Object -ExpandProperty Caption
  56. $TZONEDAYLIGHT = $Searcher.Get() | Select-Object -ExpandProperty DaylightBias
  57.  
  58. # INICIO DO HORÁRIO DE VERÃO
  59. $intMonth = $Searcher.Get() | Select-Object -ExpandProperty DaylightMonth
  60. $intDayOfWeek = $Searcher.Get() | Select-Object -ExpandProperty DaylightDayOfWeek
  61. $intDay = $Searcher.Get() | Select-Object -ExpandProperty DaylightDay
  62.  
  63. switch ($intMonth) {
  64. 1 {$strMonth = "Janeiro"}
  65. 2 {$strMonth = "Fevereiro"}
  66. 3 {$strMonth = "Março"}
  67. 4 {$strMonth = "Abril"}
  68. 5 {$strMonth = "Maio"}
  69. 6 {$strMonth = "Junho"}
  70. 7 {$strMonth = "Julho"}
  71. 8 {$strMonth = "Agosto"}
  72. 9 {$strMonth = "Setembro"}
  73. 10 {$strMonth = "Outubro"}
  74. 11 {$strMonth = "Novembro"}
  75. 12 {$strMonth = "Dezembro"}
  76. }
  77.  
  78. switch ($intDayOfWeek) {
  79. 0 {$strDayOfWeek = "Domingo"}
  80. 1 {$strDayOfWeek = "Segunda"}
  81. 2 {$strDayOfWeek = "Terça"}
  82. 3 {$strDayOfWeek = "Quarta"}
  83. 4 {$strDayOfWeek = "Quinta"}
  84. 5 {$strDayOfWeek = "Sexta"}
  85. 6 {$strDayOfWeek = "Sabado"}
  86. }
  87.  
  88. switch ($intDay) {
  89. 1 {$strDay = "Primeiro"}
  90. 2 {$strDay = "Segundo"}
  91. 3 {$strDay = "Terceiro"}
  92. 4 {$strDay = "Quarto"}
  93. 5 {$strDay = "Ultimo"}
  94. }
  95. $INICIOHV= [STRING]::Concat( $strDay,' ',$strDayOfWeek,' de ', $strMonth)
  96.  
  97. # TERMINO DO HORÁRIO DE VERÃO
  98. $intMonth = $Searcher.Get() | Select-Object -ExpandProperty StandardMonth
  99. $intDayOfWeek = $Searcher.Get() | Select-Object -ExpandProperty StandardDayOfWeek
  100. $intDay = $Searcher.Get() | Select-Object -ExpandProperty StandardDay
  101.  
  102. switch ($intMonth) {
  103. 1 {$strMonth = "Janeiro"}
  104. 2 {$strMonth = "Fevereiro"}
  105. 3 {$strMonth = "Março"}
  106. 4 {$strMonth = "Abril"}
  107. 5 {$strMonth = "Maio"}
  108. 6 {$strMonth = "Junho"}
  109. 7 {$strMonth = "Julho"}
  110. 8 {$strMonth = "Agosto"}
  111. 9 {$strMonth = "Setembro"}
  112. 10 {$strMonth = "Outubro"}
  113. 11 {$strMonth = "Novembro"}
  114. 12 {$strMonth = "Dezembro"}
  115. }
  116.  
  117. switch ($intDayOfWeek) {
  118. 0 {$strDayOfWeek = "Domingo"}
  119. 1 {$strDayOfWeek = "Segunda"}
  120. 2 {$strDayOfWeek = "Terça"}
  121. 3 {$strDayOfWeek = "Quarta"}
  122. 4 {$strDayOfWeek = "Quinta"}
  123. 5 {$strDayOfWeek = "Sexta"}
  124. 6 {$strDayOfWeek = "Sabado"}
  125. }
  126.  
  127. switch ($intDay)
  128. {
  129. 1 {$strDay = "Primeiro"}
  130. 2 {$strDay = "Segundo"}
  131. 3 {$strDay = "Terceiro"}
  132. 4 {$strDay = "Quarto"}
  133. 5 {$strDay = "Ultimo"}
  134. }
  135. $TERMINOHV= [STRING]::Concat( $strDay,' ',$strDayOfWeek,' de ', $strMonth)
  136.  
  137. # Use this check for if there's a non-terminating error.
  138. if ($?) {
  139. $LDT = Get-WmiObject -Class Win32_LocalTime -ComputerName ${Computer} -Credential $login
  140. $LDTC= Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute -FORMAT "dd/MM/yyyy hh:mm:ss"
  141. $LDATEC= Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute -FORMAT "dd/MM/yyyy"
  142. $LTIMEC= Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute -FORMAT "hh:mm:ss"
  143.  
  144. # Checar diferença de horário
  145. $LOCALDATE=Get-Date
  146. $REMOTEDATE=Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute
  147. $TIMEDIFF= (NEW-TIMESPAN –Start $LOCALDATE –End $REMOTEDATE).Minutes
  148.  
  149.  
  150. # Here we specify the WMI query S.O.
  151. $Searcher = [WmiSearcher]'Select * From Win32_OperatingSystem'
  152. # This is where we set the timeout to 5 seconds.
  153. $Searcher.Options.TimeOut = "0:0:1"
  154. $Searcher.Scope = $ManagementScope
  155.  
  156. # Perform the query and retrieve the desired property, and expand it to a
  157. # string with -ExpandProperty.
  158. $ServidorSO = $Searcher.Get() | Select-Object -ExpandProperty Caption
  159.  
  160.  
  161. if ($TZONEDAYLIGHT -eq -60) {
  162.  
  163. Write-Output "${Computer};OK;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;$INICIOHV;$TERMINOHV;$ServidorSO" | Out-File $ArquivoSaida -Append
  164. Write-Output "${Computer};OK;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;$INICIOHV;$TERMINOHV;$ServidorSO"
  165. } else {
  166. Write-Output "${Computer};NG;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;;;$ServidorSO" | Out-File $ArquivoSaida -Append
  167. Write-Output "${Computer};NG;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;;;$ServidorSO"
  168.  
  169. }
  170.  
  171.  
  172. }
  173.  
  174.  
  175.  
  176. # If a non-terminating error occurs, we get here, but I think all errors are terminating with $searcher.Get() ...
  177. else {
  178.  
  179. Write-Output "${Computer};NG;Error: $($Error[0])" | Out-File $ArquivoSaida -Append
  180. Write-Output "${Computer};NG;Error: $($Error[0])"
  181.  
  182. }
  183.  
  184. }
  185.  
  186. # If WMI has a terminating error, we get here.
  187. catch {
  188.  
  189. Write-Output "${Computer};NG;Error (terminating): $($Error[0])" | Out-File $ArquivoSaida -Append
  190. Write-Output "${Computer};NG;Error (terminating): $($Error[0])"
  191.  
  192. }
  193. }else {
  194. ##### SENÃO PINGAR
  195.  
  196. Write-Output "${Computer};NG;Error: Resolve nome no DNS mas não pinga" | Out-File $ArquivoSaida -Append
  197. Write-Output "${Computer};NG;Error: Resolve nome no DNS mas não pinga"
  198.  
  199. }
  200. }
  201. catch {
  202.  
  203. Write-Output "${Computer};NG;Error (terminating): $($Error[0])" | Out-File $ArquivoSaida -Append
  204. Write-Output "${Computer};NG;Error (terminating): $($Error[0])"
  205.  
  206. }
  207. }
  208.  
  209.  
  210.  
  211. <# breakdown
  212. -query Select * From Win32_TimeZone
  213. -query Select * From Win32_OperatingSystem
  214. -Class Win32_LocalTime
  215.  
  216. HOSTNAME: ${Computer} #informado no carregamento do script
  217. CONFIGURAÇÃO HV: OK #status do hverao no server
  218. TIME ZONE: $TZONE #win32_timezone.caption
  219. DLBIAS: $TZONEDAYLIGHT #win32_timezone.DaylightBias
  220. DATA ATUAL: $LDATEC #Win32_LocalTime @(year month day hour minute) -format "dd/MM/yyyy hh:mm:ss"
  221. HORA ATUAL: $LTIMEC #Win32_LocalTime @(year month day hour minute) -format "dd/MM/yyyy hh:mm:ss"
  222. DIFERENÇA EM MINUTOS: $TIMEDIFF #(NEW-TIMESPAN –Start (get-date) –End $REMOTEDATE).Minutes @(($REMOTEDATE=Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute))
  223. INÍCIO HV: $INICIOHV #
  224. TERMINHO HV: $TERMINOHV
  225. SO: $ServidorSO
  226.  
  227.  
  228.  
  229. #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement