Tuesday, January 29, 2008

How can I access the System Time on a Remote PC using Iron Python?

Hey, Programming Guy! How can I access the system time on a remote computer using Iron Python?
- K.O.

import clr, time, sys
sys.stdout.write( "Computer Name or IP:")
computername = sys.stdin.readline().replace("\n","")
sys.stdout.write( "Login name:")
username = sys.stdin.readline().replace("\n","")
sys.stdout.write( "Password:")
password = sys.stdin.readline().replace("\n","")
clr.AddReference("System.Management")
from System.Management import *
connOpts = ConnectionOptions()
connOpts.Username = username
connOpts.Password = password
scope = ManagementScope("\\\\" + computername + "\\root\\cimv2", connOpts)
scope.Connect()
query = ManagementObjectSearcher("Select * from win32_OperatingSystem")
query.Scope = scope
for result in query.Get():
strTime = time.strptime(result.GetPropertyValue("LocalDateTime")[0:14],"%Y%m%d%H%M%S")
print time.strftime("%Y/%m/%d %H:%M:%S",strTime)

Monday, January 28, 2008

How can I access the System Time on a Remote PC using VB.NET?

Hey, Programming Guy! How can I access the system time on a remote computer using VB.Net?
- Sweena
Hi, Sweena, there are two different techniques for accessing the resources of a remote computer using Visual Basic Dot Net, namely: WMI Scripting and Dot Net's Management package. The first example demonstrate how to retrieve the system time of a remote computer using WMI Scripting Object. The second example uses the Dot Net's Management package for accessing the system time of a remote computer:

Imports System.Net.NetworkInformation
Imports WbemScripting

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(getSystemTime("192.168.1.1", "Administrator", "password"))
End Sub

Private Function getSystemTime(ByVal TargetIP As String, ByVal LoginName As String, ByVal Password As String) As String
Dim oPing As Ping
oPing = New Ping
getSystemTime = ""
If oPing.Send(TargetIP).Status = IPStatus.Success Then
Dim objWbemLocator As SWbemLocator
Dim objWMIservice As SWbemServices
Dim objWMIobjectSet As SWbemObjectSet
Dim objColumn As SWbemObject
Dim strTime As String
objWbemLocator = New SWbemLocator
objWMIservice = objWbemLocator.ConnectServer(TargetIP, "root\cimv2", LoginName, Password)
objWMIobjectSet = objWMIservice.ExecQuery("Select * from Win32_OperatingSystem")
strTime = ""
For Each objColumn In objWMIobjectSet
strTime = objColumn.Properties_.Item("LocalDateTime").Value
Next
getSystemTime = strTime.Substring(8, 2) & ":" & strTime.Substring(10, 2) & ":" & strTime.Substring(12, 2)
End If
End Function
End Class


Imports System.Net.NetworkInformation
Imports System.Management

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(getSystemTime("192.168.1.1", "Administrator", "password"))
End Sub

Private Function getSystemTime(ByVal TargetIP As String, ByVal LoginName As String, ByVal Password As String) As String
Dim oPing As Ping
Dim strTime
oPing = New Ping
getSystemTime = ""
strTime = ""
If oPing.Send(TargetIP).Status = IPStatus.Success Then
Dim connOpts As ConnectionOptions
Dim scope As ManagementScope
Dim query As ManagementObjectSearcher
Dim objColumn As ManagementBaseObject
connOpts = New ConnectionOptions
connOpts.Username = LoginName
connOpts.Password = Password
scope = New ManagementScope("\\" & TargetIP & "\root\cimv2", connOpts)
scope.Connect()
query = New ManagementObjectSearcher("Select * from Win32_OperatingSystem")
query.Scope = scope
For Each objColumn In query.get
strTime = objColumn.GetPropertyValue("LocalDateTime").ToString
Next
getSystemTime = strTime.Substring(8, 2) & ":" & strTime.Substring(10, 2) & ":" & strTime.Substring(12, 2)
End If
End Function
End Class