Windows Perl Scripting Forums » General

Read/Configure time setting on remote host

(4 posts)

Tags:


  1. akearins
    Member

    Hi,

    I am trying to write a script that will allow a newtork administrator obtain and configure the time on a remote host.

    Reason for this script is that hosts who time is not in sync with the license manager are not picking up license for the software.

    Help appreciated
    Posted 11 years ago #
  2. msperl
    Administrator

    Use WMI to query and set time and date on remote systems. You may need to adjust privileges for your account to set time/date on the remote machine.

    use Win32::OLE qw(in);

    my $datetime = Win32::OLE->new("WbemScripting.SWbemDateTime") or die;
    my $machine = shift @ARGV or ".";
    $machine =~ s/^[\\\/]+//;
    my $wmiservices = Win32::OLE->GetObject("winmgmts:{impersonationLevel=impersonate,(security)}//$machine") or die;
    foreach my $os ( in( $wmiservices->InstancesOf("Win32_OperatingSystem")))
    {
      print "Last Boot Time:".$os->{LastBootUpTime}."\n";
      print "Current time:".$os->{LocalDateTime}."\n";
      $datetime->{Value} = $os->{LocalDateTime};
      printf( "Current Time: %02d-%02d-%04d at %02d:%02d:%02d\n", $datetime->{Month}, $datetime->{Day}, $datetime->{Year}, $datetime->{Hours}, $datetime->{Minutes}, $datetime->{Seconds} );
      print "Setting time + 2 hours:";
      $datetime->{Hours} += 2;
      printf( "Current Time: %02d-%02d-%04d at %02d:%02d:%02d\n", $datetime->{Month}, $datetime->{Day}, $datetime->{Year}, $datetime->{Hours}, $datetime->{Minutes}, $datetime->{Seconds} );
      print "\tHard value: $datetime->{Value}\n";
      $Result = $os->SetDateTime($datetime->{Value});
      print "Result: $Result\n";
    }



    Details:
    DateTime object: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/swbemdatetime.asp
    Win32_OperatingSystem: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_operatingsystem.asp
    Win32_OperatingSystem.SetDateTime(): http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/setdatetime_method_in_class_win32_operatingsystem.asp
    Posted 11 years ago #
  3. akearins
    Member

    Thanks very much for the reply much appreciated.
    The code runs fine on my local machine however i need to get remote host <STDIN> from the domain and set the time on that host remotely....

    Again any help welcome
    Posted 11 years ago #
  4. msperl
    Administrator

    Pass in the name of the remote host when you run the script:

    Perl ScriptName.pl \\remoteserver

    Posted 11 years ago #

RSS feed for this topic

Reply

You must log in to post.