Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ping = new-object system.net.networkinformation.ping
- #>>>>>>>$login = Get-Credential
- # A list of computers to process.
- #>>>>>>>$Computers = get-Content $args[0]
- $ArquivoSaida = "C:\windows\temp\HVERAO_$target.log" #$args[1]
- #>>>>>>>ECHO $ArquivoSaida
- # Cabeçalho do arquivo de saída
- 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
- # The purpose here is to have clean output to the console (or redirected to a file).
- $Computers | ForEach-Object {
- # This is needed for the error handling in the catch block where $_ gets
- # set to the last error rather than the computer name.
- $Computer = $_
- try {
- # VALIDA CONECTIVIDADE VIA PING
- $pingreturns = $ping.send(${Computer})
- if ($pingreturns.Status -eq 'Success'){
- ##### SE PINGAR
- ## 1 ##
- # Since certain WMI errors are terminating, we need a try/catch statement.
- try {
- # Here we specify the WMI query
- $Searcher = [WmiSearcher]'Select * From Win32_TimeZone'
- # This is where we set the timeout to 1 seconds.
- $Searcher.Options.TimeOut = "0:0:1"
- # Create necessary objects, specify a scope and namespace for the remote computer (or localhost or '.'),
- # then set the scope on the WMISearcher object, which is usually "\\computername\root\cimv2".
- $ConnectionOptions = New-Object Management.ConnectionOptions
- $ConnectionOptions.Username = $login.UserName
- $ConnectionOptions.SecurePassword = $login.Password
- $ManagementScope = New-Object Management.ManagementScope("\\${Computer}\root\cimv2", $ConnectionOptions)
- # Here we specify the WMI query TIME ZONE and Daylight
- $Searcher = [WmiSearcher]'Select * From Win32_TimeZone'
- # This is where we set the timeout to 5 seconds.
- $Searcher.Options.TimeOut = "0:0:1"
- $Searcher.Scope = $ManagementScope
- # Perform the query and retrieve the desired property, and expand it to a
- # string with -ExpandProperty.
- $TZONE = $Searcher.Get() | Select-Object -ExpandProperty Caption
- $TZONEDAYLIGHT = $Searcher.Get() | Select-Object -ExpandProperty DaylightBias
- # INICIO DO HORÁRIO DE VERÃO
- $intMonth = $Searcher.Get() | Select-Object -ExpandProperty DaylightMonth
- $intDayOfWeek = $Searcher.Get() | Select-Object -ExpandProperty DaylightDayOfWeek
- $intDay = $Searcher.Get() | Select-Object -ExpandProperty DaylightDay
- switch ($intMonth) {
- 1 {$strMonth = "Janeiro"}
- 2 {$strMonth = "Fevereiro"}
- 3 {$strMonth = "Março"}
- 4 {$strMonth = "Abril"}
- 5 {$strMonth = "Maio"}
- 6 {$strMonth = "Junho"}
- 7 {$strMonth = "Julho"}
- 8 {$strMonth = "Agosto"}
- 9 {$strMonth = "Setembro"}
- 10 {$strMonth = "Outubro"}
- 11 {$strMonth = "Novembro"}
- 12 {$strMonth = "Dezembro"}
- }
- switch ($intDayOfWeek) {
- 0 {$strDayOfWeek = "Domingo"}
- 1 {$strDayOfWeek = "Segunda"}
- 2 {$strDayOfWeek = "Terça"}
- 3 {$strDayOfWeek = "Quarta"}
- 4 {$strDayOfWeek = "Quinta"}
- 5 {$strDayOfWeek = "Sexta"}
- 6 {$strDayOfWeek = "Sabado"}
- }
- switch ($intDay) {
- 1 {$strDay = "Primeiro"}
- 2 {$strDay = "Segundo"}
- 3 {$strDay = "Terceiro"}
- 4 {$strDay = "Quarto"}
- 5 {$strDay = "Ultimo"}
- }
- $INICIOHV= [STRING]::Concat( $strDay,' ',$strDayOfWeek,' de ', $strMonth)
- # TERMINO DO HORÁRIO DE VERÃO
- $intMonth = $Searcher.Get() | Select-Object -ExpandProperty StandardMonth
- $intDayOfWeek = $Searcher.Get() | Select-Object -ExpandProperty StandardDayOfWeek
- $intDay = $Searcher.Get() | Select-Object -ExpandProperty StandardDay
- switch ($intMonth) {
- 1 {$strMonth = "Janeiro"}
- 2 {$strMonth = "Fevereiro"}
- 3 {$strMonth = "Março"}
- 4 {$strMonth = "Abril"}
- 5 {$strMonth = "Maio"}
- 6 {$strMonth = "Junho"}
- 7 {$strMonth = "Julho"}
- 8 {$strMonth = "Agosto"}
- 9 {$strMonth = "Setembro"}
- 10 {$strMonth = "Outubro"}
- 11 {$strMonth = "Novembro"}
- 12 {$strMonth = "Dezembro"}
- }
- switch ($intDayOfWeek) {
- 0 {$strDayOfWeek = "Domingo"}
- 1 {$strDayOfWeek = "Segunda"}
- 2 {$strDayOfWeek = "Terça"}
- 3 {$strDayOfWeek = "Quarta"}
- 4 {$strDayOfWeek = "Quinta"}
- 5 {$strDayOfWeek = "Sexta"}
- 6 {$strDayOfWeek = "Sabado"}
- }
- switch ($intDay)
- {
- 1 {$strDay = "Primeiro"}
- 2 {$strDay = "Segundo"}
- 3 {$strDay = "Terceiro"}
- 4 {$strDay = "Quarto"}
- 5 {$strDay = "Ultimo"}
- }
- $TERMINOHV= [STRING]::Concat( $strDay,' ',$strDayOfWeek,' de ', $strMonth)
- # Use this check for if there's a non-terminating error.
- if ($?) {
- $LDT = Get-WmiObject -Class Win32_LocalTime -ComputerName ${Computer} -Credential $login
- $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"
- $LDATEC= Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute -FORMAT "dd/MM/yyyy"
- $LTIMEC= Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute -FORMAT "hh:mm:ss"
- # Checar diferença de horário
- $LOCALDATE=Get-Date
- $REMOTEDATE=Get-Date -Day $LDT.DAY -Month $LDT.Month -Year $LDT.YEAR -Hour $LDT.Hour -Minute $LDT.Minute
- $TIMEDIFF= (NEW-TIMESPAN –Start $LOCALDATE –End $REMOTEDATE).Minutes
- # Here we specify the WMI query S.O.
- $Searcher = [WmiSearcher]'Select * From Win32_OperatingSystem'
- # This is where we set the timeout to 5 seconds.
- $Searcher.Options.TimeOut = "0:0:1"
- $Searcher.Scope = $ManagementScope
- # Perform the query and retrieve the desired property, and expand it to a
- # string with -ExpandProperty.
- $ServidorSO = $Searcher.Get() | Select-Object -ExpandProperty Caption
- if ($TZONEDAYLIGHT -eq -60) {
- Write-Output "${Computer};OK;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;$INICIOHV;$TERMINOHV;$ServidorSO" | Out-File $ArquivoSaida -Append
- Write-Output "${Computer};OK;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;$INICIOHV;$TERMINOHV;$ServidorSO"
- } else {
- Write-Output "${Computer};NG;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;;;$ServidorSO" | Out-File $ArquivoSaida -Append
- Write-Output "${Computer};NG;$TZONE;$TZONEDAYLIGHT;$LDATEC;$LTIMEC;$TIMEDIFF;;;$ServidorSO"
- }
- }
- # If a non-terminating error occurs, we get here, but I think all errors are terminating with $searcher.Get() ...
- else {
- Write-Output "${Computer};NG;Error: $($Error[0])" | Out-File $ArquivoSaida -Append
- Write-Output "${Computer};NG;Error: $($Error[0])"
- }
- }
- # If WMI has a terminating error, we get here.
- catch {
- Write-Output "${Computer};NG;Error (terminating): $($Error[0])" | Out-File $ArquivoSaida -Append
- Write-Output "${Computer};NG;Error (terminating): $($Error[0])"
- }
- }else {
- ##### SENÃO PINGAR
- Write-Output "${Computer};NG;Error: Resolve nome no DNS mas não pinga" | Out-File $ArquivoSaida -Append
- Write-Output "${Computer};NG;Error: Resolve nome no DNS mas não pinga"
- }
- }
- catch {
- Write-Output "${Computer};NG;Error (terminating): $($Error[0])" | Out-File $ArquivoSaida -Append
- Write-Output "${Computer};NG;Error (terminating): $($Error[0])"
- }
- }
- <# breakdown
- -query Select * From Win32_TimeZone
- -query Select * From Win32_OperatingSystem
- -Class Win32_LocalTime
- HOSTNAME: ${Computer} #informado no carregamento do script
- CONFIGURAÇÃO HV: OK #status do hverao no server
- TIME ZONE: $TZONE #win32_timezone.caption
- DLBIAS: $TZONEDAYLIGHT #win32_timezone.DaylightBias
- DATA ATUAL: $LDATEC #Win32_LocalTime @(year month day hour minute) -format "dd/MM/yyyy hh:mm:ss"
- HORA ATUAL: $LTIMEC #Win32_LocalTime @(year month day hour minute) -format "dd/MM/yyyy hh:mm:ss"
- 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))
- INÍCIO HV: $INICIOHV #
- TERMINHO HV: $TERMINOHV
- SO: $ServidorSO
- #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement