none
Check if Daylight Savings is active on remote servers RRS feed

  • Question

  • I know I can check if Daylight Savings is TRUE on the local machine by running the command:

    (Get-Date).IsDaylightSavingTime()

    But how do I run this on a number of remote servers?

    • Moved by Bill_Stewart Wednesday, February 14, 2018 7:14 PM Abandoned
    Tuesday, November 21, 2017 11:28 PM

All replies

  • Invoke-Command {(Get-Date).IsDaylightSavingTime()} -computer remotepc

    \_(ツ)_/

    Tuesday, November 21, 2017 11:38 PM
  • Awesome. Using this info, I've come up with the following code to get the server list from AD:


    $SourceComputersList=Get-ADComputer -SearchBase 'ou=OU1,ou=OU2,dc=MyDomain,dc=com' -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem | Select Name
    
    Try
        {
        foreach ($oComputerName in $SourceComputersList)
    	    {
            $ComputerName = $oComputerName.Name
            Write-Host "Processing " $ComputerName -ForegroundColor Green
            $IsDayLightSavingsActive = Invoke-Command {(Get-Date).IsDaylightSavingTime()} -computer $ComputerName # -ErrorAction Stop
            Switch ($IsDayLightSavingsActive)
                {
                $Null 
                    {
                    Write-Host "Error accessing server $ComputerName" -Foregroundcolor Red
                    }
                {($_ -eq "True") -or ($_ -eq "False")}
                    {
                    Write-Host "Daylight savings is $IsDayLightSavingsActive on computer $ComputerName"
                    }
                }
    
    
    	    }
        }
    
    # Catch  [System.Management.Automation.RuntimeException]#PSRemotingTransportException
    Catch [ System.Management.Automation.Remoting.PSRemotingTransportException]
        {
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "Most probably this computer is offline." -Foregroundcolor Red
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "ErrorMessage = " $_.Exception.Message -Foregroundcolor Red
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "FailedItem = " $_.Exception.ItemName -Foregroundcolor Red
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "Exception Full Name: " $_.Exception.GetType().FullName -ForegroundColor Yellow 
        Write-Host ""
        }
    
    Catch
        {
        Write-Host "Uncaught error:" -Foregroundcolor Red
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "ErrorMessage = " $_.Exception.Message -Foregroundcolor Red
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "FailedItem = " $_.Exception.ItemName -Foregroundcolor Red
        Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
        Write-Host "Exception Full Name: " $_.Exception.GetType().FullName -ForegroundColor Yellow 
        Write-Host ""
        }
    

    My problem is that when it finds a server which resolves to an IP, but, say has an Access Denied issue, the variable $IsDayLightSavingsActive is Null and the Switch never executes and goes back to the ForEach loop?? Why?


    | +-- JDMils |

    Thursday, December 28, 2017 3:53 AM
  • Your Try/Catch is constructed incorrectly.  I must be inside the loop to work correctly.


    \_(ツ)_/

    Thursday, December 28, 2017 5:37 AM
  • I doin't see how the Try/Catch is constructed incorrectly- The whole code is inside the Try/Catch!?

    Anyhow, to test your comment I put the Try/Catch within the For/Each with the same issue:

    $SourceComputersList=Get-ADComputer -SearchBase 'ou=OU1,ou=OU2,dc=MyDomain,dc=com' -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem | Select Name
    # $SourceComputersList= Get-Content "APACServers.txt"
    
        foreach ($oComputerName in $SourceComputersList)
    	    { #FOREACH
            Try
                { #TRY
                $ComputerName = $oComputerName.Name
                Write-Host "Processing " $ComputerName -ForegroundColor Green
                $IsDayLightSavingsActive = Invoke-Command {(Get-Date).IsDaylightSavingTime()} -computer $ComputerName # -ErrorAction Stop
            
                Switch ($IsDayLightSavingsActive)
                    { #SWITCH
                    ($_ -eq $Null )
                        { #ITEM1
                        Write-Host "Error accessing server $ComputerName" -Foregroundcolor Red
                        } #ITEM1
                    {($_ -eq "True") -or ($_ -eq "False")}
                        { #ITEM2
                        Write-Host "Daylight savings is $IsDayLightSavingsActive on computer $ComputerName"
                        } #ITEM2
                    } #SWITCH
            } #TRY
    
            Catch [ System.Management.Automation.Remoting.PSRemotingTransportException]
                { #CATCH1
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "Most probably this computer is offline." -Foregroundcolor Red
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "ErrorMessage = " $_.Exception.Message -Foregroundcolor Red
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "FailedItem = " $_.Exception.ItemName -Foregroundcolor Red
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "Exception Full Name: " $_.Exception.GetType().FullName -ForegroundColor Yellow 
                Write-Host ""
                } #CATCH1
    
            Catch
                { #CATCH2
                Write-Host "Uncaught error:" -Foregroundcolor Red
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "ErrorMessage = " $_.Exception.Message -Foregroundcolor Red
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "FailedItem = " $_.Exception.ItemName -Foregroundcolor Red
                Write-Host "[$ComputerName] " -NoNewline -ForegroundColor Yellow
                Write-Host "Exception Full Name: " $_.Exception.GetType().FullName -ForegroundColor Yellow 
                Write-Host ""
                } #CATCH2
    
    	    } #FOREACH
    


    | +-- JDMils |

    Thursday, December 28, 2017 10:07 PM
  • Any one error aborts the whole script. Not how we use a try/catch. Is that what you want?

    \_(ツ)_/

    Thursday, December 28, 2017 11:05 PM