Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need script to display info in desktop 1

Status
Not open for further replies.

Techie64

Technical User
Mar 22, 2005
29
US
I need help in writing a script for XP pro
machines (batch, NT script, or VBS) that will display any of the following on the desktop when the user logs in:

the computer name (preferred)
the user ID
IP address
the time logged in

I prefer the computer name to be displayed, but the others would be helpful. I have some experience in writing batches, but don't know how to get the above results. Thanks.
 
When you say desktop do you litterally mean on the desktop in the Wallpaper sense or do you mean some kind of dialog box?

If it is the Dialog box then use the script below, it will get the first 3 items you wanted. The time logged in is a little harder but I will post it later if someone else hasn't already.

Code:
On Error Resume Next
strComputer = "."

Dim IP_Address : IP_Address = GetIP()

Set objShell = Wscript.CreateObject("Wscript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")
Set objNetwork = Wscript.CreateObject("Wscript.Network")

samUser = objNetwork.UserName
computerName = objNetwork.ComputerName

objShell.Popup "This Computer Name is: " & computerName & vbCrLF & "The Current UserID is: " & samUser & 

vbCrLF & "Your Ip Address is: " & IP_Address & vbCrLF & vbCrLF & "Click 'OK' to close this window 

(auto-closes in 60 seconds).",60,"Computer Information Collection",vbInformation+vbOKOnly+vbDefaultButton1
Wscript.Quit

Function GetIP()
  Dim ws : Set ws = CreateObject("WScript.Shell")
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
  Dim ThisLine, IP
  If ws.Environment("SYSTEM")("OS") = "" Then
    ws.run "winipcfg /batch " & TmpFile, 0, True
  Else
    ws.run "%comspec% /c ipconfig > " & TmpFile, 0, True
  End If
  With fso.GetFile(TmpFile).OpenAsTextStream
    Do While NOT .AtEndOfStream
      ThisLine = .ReadLine
      If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
    Loop
    .Close
  End With
  'WinXP (NT? 2K?) leaves a carriage return at the end of line
  If IP <> "" Then
    If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
  End If
  GetIP = IP
  fso.GetFile(TmpFile).Delete  
  Set fso = Nothing
  Set ws = Nothing
End Function

Greg Palmer
Freeware Utilities for Windows Administrators.
 
OK got it finished, pop the following code into a text file and save it with a .vbs extension.

Code:
On Error Resume Next
strComputer = "."

Dim strBoot, strBootDay, strBootHour, strBootMins, strBootMonth, strBootYear
Dim strComputer1, strMsg
Dim IP_Address : IP_Address = GetIP()

Set objShell = Wscript.CreateObject("Wscript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")
Set objNetwork = Wscript.CreateObject("Wscript.Network")

samUser = objNetwork.UserName
computerName = objNetwork.ComputerName

' Connect to specified computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
' Display error number and description if applicable
If Err Then ShowError

Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
	strTimeShift = Right( objItem.LastBootUpTime, 4 ) / 60
	strBootYear  = Left( objItem.LastBootUpTime, 4 )
	strBootMonth = Mid( objItem.LastBootUpTime,  5, 2 )
	strBootDay   = Mid( objItem.LastBootUpTime,  7, 2 )
	strBootDate  = DateValue( strBootDay & "-" & strBootMonth & "-" & strBootYear )
	strBootHour  = Mid( objItem.LastBootUpTime,  9, 2 ) - strTimeShift
	strBootMins  = Mid( objItem.LastBootUpTime, 11, 2 )
	If strBootHour < 0 Then
		strBootHour = strBootHour + 24
		strBootDate = DateAdd( "d", -1, DateValue( strBootDate ) )
	End If
	If strBootHour > 23 Then
		strBootHour = strBootHour - 24
		strBootDate = DateAdd( "d", 1, DateValue( strBootDate ) )
	End If
	strBootTime  = strBootHour & ":" & strBootMins
	strBoot = strBootDate & ", " & strBootTime
	strMsg  = "Last boot time was " & strBoot
Next




objShell.Popup "This Computer Name is: " & computerName & vbCrLF & "The Current UserID is: " & samUser & 

vbCrLF & "Your Ip Address is: " & IP_Address & vbCrLF & strMsg & vbCrLF & vbCrLF & "Click 'OK' to close this 

window (auto-closes in 60 seconds).",60,"Computer Information 

Collection",vbInformation+vbOKOnly+vbDefaultButton1
Wscript.Quit

Function GetIP()
  Dim ws : Set ws = CreateObject("WScript.Shell")
  Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
  Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
  Dim ThisLine, IP
  If ws.Environment("SYSTEM")("OS") = "" Then
    ws.run "winipcfg /batch " & TmpFile, 0, True
  Else
    ws.run "%comspec% /c ipconfig > " & TmpFile, 0, True
  End If
  With fso.GetFile(TmpFile).OpenAsTextStream
    Do While NOT .AtEndOfStream
      ThisLine = .ReadLine
      If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
    Loop
    .Close
  End With
  'WinXP (NT? 2K?) leaves a carriage return at the end of line
  If IP <> "" Then
    If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
  End If
  GetIP = IP
  fso.GetFile(TmpFile).Delete  
  Set fso = Nothing
  Set ws = Nothing
End Function

'Done
WScript.Quit(0)


Sub ShowError()
	strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _
	       & Err.Description & vbCrLf & vbCrLf & vbCrLf
	Syntax
End Sub

Greg Palmer
Freeware Utilities for Windows Administrators.
 
Thanks! I will try this and inform you how it goes. Thanks again Greg.
 
For anyone interested in using the script I provided it seems that the formatting that is applied by Tek-Tips in the code window that it adds extra line breaks.

If you want to download it you can do so from
Greg Palmer
Freeware Utilities for Windows Administrators.
 
Thanks Greg! It works! But you already knew that didn't you. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top