Monday, April 4, 2011

VB Script to list the current logged in user

The Scipt below reads computers from c:\temp\computers.txt and lists the user who is currently logged in.

'List user currently logged on to a desktop

WScript.Echo "List Currently logged in user for Computers in c:\temp\computers.txt"
On Error Resume Next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\temp\computers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, VbCrLf)

For Each strComputer In arrComputers  
 Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
 For Each objItem in colItems
 WScript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
 Next
 Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
Next
WScript.Echo "Script Finished"

VB Script to Execute Wake on LAN on a different subnet

Wake on Lan packets won't travel over different subnets.

Below is a script which remotely executes the command on a host computer on the same subnet to send the WOL packet.

You will need
  • WOL capable computer you wish to wake.
  • A host machine on the same subnet as the comptuer you wish to wake.
  • WOL software http://sourceforge.net/projects/wake-on-lan/ extract the software to C:\temp\wol
  • MAC address of computer you wish to wake (you can get from DHCP) .
The script copies the WOL files from C:\temp\WOL to the host then remotely executes the command.

'Copy Wake on LAN files to remote computer then execute WOL
'Allows you to select remote computer on a different subnet as a host and wake another computerOption Explicit
Dim strHostComputer, strMAC, strIPComputer, objFSO
Dim objWMIService, objProcess, errReturn
Dim strComputerOn, Count, strCommand
Dim intProcessID, strComputer, colPingedComputers
Dim objComputer
strHostComputer =Inputbox("Wake Computer on another Subnet" & chr(10) & chr(13) & chr(10) & chr(13) & "Enter HOST PC Number on Remote Subnet","Input Required")
If strHostComputer = ""Then
 WScript.Echo"Host blank exiting script!"
 WScript.Quit
Endif
strMAC =Inputbox("Enter MAC of PC you want to WAKE" & chr(10) & chr(13) & chr(10) & chr(13) & "MAC can be obtained from DHCP Scope, Address Leases","Input Required")
If strMAC = ""Then
 WScript.Echo"MAC blank exiting script!"
 WScript.Quit
Endif
strIPComputer =Inputbox("Enter IP Address of Computer you want to WAKE","Input Required")
If strIPComputer = ""Then
 WScript.Echo"IP Blank will send request but can't check progress"
Endif

Const OverWriteFiles = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\temp\WOL\bin\wol.exe") Then
    objFSO.CopyFolder"C:\temp\WOL" , "\\" & strHostComputer & "\C$\temp\WOL" , OverWriteFiles
Else
    Wscript.Echo"Wake on Lan File C:\temp\WOL\bin\wol.exe not found. Download from http://sourceforge.net/projects/wake-on-lan"
    WScript.Quit
EndIf


strCommand = "C:\temp\WOL\bin\wol.exe " & strMAC

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strHostComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")

errReturn = objProcess.Create(strCommand, null, null, intProcessID)

If errReturn = 0Then
'WScript.Echo strHostComputer &" " & strCommand & " executed with a process ID: " & intProcessID
Else
WScript.Echo strHostComputer & " " & strCommand & " not started due to error: " & errReturn
EndIf
If strIPComputer = ""Then
 WScript.Quit
Endif
' Verify Computer Availability
Wscript.Echo"Waking Remote Computer - usually takes about 1 minute"
Count = 0
DoWhile strComputerOn <> 1
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPingedComputers = objWMIService.ExecQuery _
    ("Select * from Win32_PingStatus Where Address = '" & strIPComputer & "'")
ForEach objComputerin colPingedComputers
    If objComputer.StatusCode = 0Then
        Wscript.Echo"Remote Computer " & strIPComputer & " Powered On!!!"
        strComputerOn=1
    Else
        WScript.sleep20000
        Count = Count + 1
        If Count = 7Then
        Wscript.Echo"Can't Connect to Remote Machine WOL has failed :(" & chr(10) & chr(13) & chr(10) & chr(13) & "Or Remote Machine has new IP"
        WScript.Quit
        Endif
   EndIf
Next
Loop

VB Script to Shutdown Comptuers from a text file

The below script Shuts down Computers in a text file C:\temp\computers.txt

' Read Arguments from a Text File

Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\computers.txt", ForReading)
i = 0
WScript.Echo "SHUTDOWN Computers in c:\temp\computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop

For Each objItem in objDictionary
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\"& objDictionary.Item(objItem) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & objDictionary.Item(objItem) & "\Admin$")
      'Reboot Desktops
      'strComputer = objDictionary.Item(objItem)
      WScript.Echo objDictionary.Item(objItem)
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate,(Shutdown)}\\" & _
           objDictionary.Item(objItem) & "\root\cimv2")

    Set colOperatingSystems = objWMIService.ExecQuery _
        ("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem in colOperatingSystems
        objOperatingSystem.Win32Shutdown(1)
    Next
    
      Else
      'WScript.Echo "Can't Connect to " & objDictionary.Item(objItem) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      'strCantConnect = "1"
  End If
Next
If strCantConnect = "1" Then
WScript.Echo "Script Finished!"
Else WScript.Echo "All Desktops have been Shutdown!"
End if

VB Script to Set the Default Domain for Computers in a text file

The following script Sets the Default Domain at login for computers in C:\temp\computers.txt

' Change Registry Keys so the default domain name is BOB when  people login
Const HKEY_LOCAL_MACHINE = &H80000002

' Read Arguments from a Text File
Const ForReading = 1

Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\computers.txt", ForReading)
i = 0
WScript.Echo "Set Login Domain to BOB Computers in c:\temp\computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop

For Each objItem in objDictionary
    Set objFSO = CreateObject("Scripting.FileSystemObject")
 
      WScript.Echo objDictionary.Item(objItem)
      strComputer = objDictionary.Item(objItem)

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
strValueName = "DefaultDomainName"
strValue = "BOB"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

strValueName = "CachePrimaryDomain"
strValue = "BOB"
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue

Next
If strCantConnect = "1" Then
WScript.Echo "Script Finished!"
Else WScript.Echo "All Desktops have had Default Domain Set!"
End if 

VB Script to Reboot Computers from a text file

The VB Script below reboots machines in C:\temp\computers.txt

' Read Arguments from a Text File

Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\computers.txt", ForReading)
i = 0
WScript.Echo "Reboot Computers in c:\temp\computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop

For Each objItem in objDictionary
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\"& objDictionary.Item(objItem) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & objDictionary.Item(objItem) & "\Admin$")
      'Reboot Desktops
      'strComputer = objDictionary.Item(objItem)
      WScript.Echo objDictionary.Item(objItem)
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
           objDictionary.Item(objItem) & "\root\cimv2")

    Set colOperatingSystems = objWMIService.ExecQuery _
        ("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem in colOperatingSystems
        objOperatingSystem.Reboot()
    Next
    
      Else
      WScript.Echo "Can't Connect to " & objDictionary.Item(objItem) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
Next
If strCantConnect = "1" Then
WScript.Echo "Script Finished!"
Else WScript.Echo "All Desktops have been Rebooted!"
End if

VB Script to get IP and MAC from remote computer

The following script gets the IP and MAC address form computers in C:\temp\computers.txt and writes them to files in C:\temp\desktop

'Gathers the IP address and MAC from
'Computers in c:\temp\computers.txt
'Outputs to c:\temp\desktop

Option Explicit
Dim objDictionary, objFSO, objTextFile, strRemoteComputer
Dim strNextLine, arrServiceList, i, strCantConnect, strLogFileLocation
Dim objFile, objFolder, objTextFile1, n, objWMIService, colAdapters, objAdapter

' Read a Text File into an Array
Const ForAppending = 8
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\temp\computers.txt", ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
        Wscript.Echo "Service: " & arrServiceList(i)
    Next

'Get Computer name via input
'strComputer =Inputbox("Get MAC Address and write to c:\Temp" & chr(10) & chr(13) & chr(10) & chr(13) & "Enter PC Number","Input Required")

 ' Verify connection to the Desktop
 strCantConnect = "0"
 Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\"& arrServiceList(0) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & arrServiceList(0) & "\Admin$")
  Else
      WScript.Echo "Can't Connect to " & arrServiceList(0) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
' Create the File System Object
strLogFileLocation = "C:\Temp\Desktop\" & arrServiceList(0) & ".txt"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strLogFileLocation) Then
 'objFSO.DeleteFile(strLogFileLocation)
 'Set objFile = objFSO.CreateTextFile(strLogFileLocation)
Else
   Set objFile = objFSO.CreateTextFile(strLogFileLocation)
End If
set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
If strCantConnect = "0" Then
Set objTextFile1 = objFSO.OpenTextFile _
(strLogFileLocation, ForAppending, True)
objTextFile1.WriteLine()
objTextFile1.WriteLine("#################################################################################")
End if

'Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.CreateTextFile(strLogFileLocation)

' List IP Configuration Data
strRemoteComputer = arrServiceList(0)
If strCantConnect = "0" Then
 Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strRemoteComputer & "\root\cimv2")

 Set colAdapters = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 n = 1
 End If
   
 For Each objAdapter in colAdapters
If strCantConnect = "0"  Then
 '   WScript.Echo "Network Adapter " & n
 objTextFile1.WriteLine("=================")
 objTextFile1.WriteLine("Host name: " & objAdapter.DNSHostName)
 objTextFile1.WriteLine("Description: " & objAdapter.Description)
 objTextFile1.WriteLine("Physical (MAC) address: " & objAdapter.MACAddress)

  Else
  End If 
    If Not IsNull(objAdapter.IPAddress) Then
       For i = 0 To UBound(objAdapter.IPAddress)
  If strCantConnect = "0" Then
  objTextFile1.WriteLine("IP address:             " & objAdapter.IPAddress(i))
  objTextFile1.WriteLine()
  End if
       Next
    End If

    n = n + 1
  
Next
objTextFile1.Close
If strCantConnect = "0" Then
'WScript.Echo "Please review " & strLogFileLocation & " for MAC Address"
Else
End If
'

Loop
WScript.Echo "Script Finished! Please Review c:\Temp\Desktop for results"

VBScript to Set DNS Suffix, Check File, Check Registry

The below script...
  • Reads computer names from c:\temp\compters.txt
  • Sets the DNS suffix to append under Advanced TCP/IP properties 
  • Verifies a file exists C:\Temp\efs_found.txt and the file size is 0
  • Checks a registry entry

'Check Computers in c:\temp\computers.txt
'Set DNS suffix
'Check a file exists and the file size
'Check Registry setting

Option Explicit
Dim objFSO, objTextFile, strNextLine, strCantConnect, arrServiceList
Dim objFolder, strKeyPath, strValueName, strValue, oReg, i, objFile

' Read a Text File into an Array
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\temp\computers.txt", ForReading)
WScript.Echo "Setting DNS Suffix, Checking Encypted Files, Checking ODBC on C:\Temp\Computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    strCantConnect = "0"
  ' Verify connection to the Desktop
 Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\" & arrServiceList(0) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & arrServiceList(0) & "\Admin$")
  Else
      WScript.Echo "Can't Connect to " & arrServiceList(0) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
 
 ' Create Registry DNS Search String Value
 If  strCantConnect = "0" Then
 Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    arrServiceList(0) & "\root\default:StdRegProv")
  strKeyPath = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters"
 strValueName = "SearchList"
 strValue = "contoso,contoso.local"
 oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 'WScript.Echo "DNS Search Domains Added to "& arrServiceList(0) & " Registry"
 Else
 End If

 ' Verify that the EFS checking script
has been run
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 If objFSO.FileExists("\\" &  arrServiceList(0) & "\C$\Temp\efs_found.txt") Then
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objFile = objFSO.GetFile("\\"  & arrServiceList(0) & "\C$\Temp\efs_found.txt")
   If objFile.Size = "0" Then
   'WScript.Echo "No encypted Files Exist on "  & arrServiceList(0)
   Else
   WScript.Echo "Encypted Files Exist on "  & arrServiceList(0) & " Unencrypt before Migrating!"
   End If
 Else
 If  strCantConnect = "0" Then
    Wscript.Echo "Script for checking for Encryption Files hasn't been run."& chr(10) & chr(13) & "Login to "  & arrServiceList(0) & "and run
FindEFS.bat"& chr(10) & chr(13) & "Before continuing!"
 End If
  End If
 ' Check ODBC settings for Dataworks
 If  strCantConnect = "0" Then
  Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
     arrServiceList(0) & "\root\default:StdRegProv")
   strKeyPath = "SOFTWARE\ODBC\ODBC.INI\DataWorks"
  strValueName = "Trusted_Connection"
  oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 If strValue = "Yes" Then
  WScript.Echo "Dataworks ODBC Settings NOT SET CORRECTLY on "  & arrServiceList(0) &"!"
  Else
     'Wscript.Echo "Dataworks ODBC Settings look OK on "  & arrServiceList(0)
     End If 
    End if
        For i = 1 to Ubound(arrServiceList)
        Wscript.Echo "Service: " & arrServiceList(i)

    Next
Loop
Wscript.Echo "Finished Checking and setting DNS!"

Sunday, April 3, 2011

VB Script for checking computers in a text file are turned on

Below is a handy script for checking Pc's in a text file are turned on and you can connect to the Admin share. 

'Check Computers in c:\temp\computers.txt are turned on
'And you can access admin share

Option Explicit
Dim objDictionary, objFSO, objTextFile, i, strNextLine
Dim objItem, objFolder, strCantConnect

' Read Arguments from a Text File
Const ForReading = 1

Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\computers.txt", ForReading)
i = 0
WScript.Echo "Verify you can connect to Computers in c:\temp\computers.txt"
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop

For Each objItem in objDictionary
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\"& objDictionary.Item(objItem) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & objDictionary.Item(objItem) & "\Admin$")
  Else
      WScript.Echo "Can't Connect to " & objDictionary.Item(objItem) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
Next
If strCantConnect = "1" Then
WScript.Echo "Script Finished!"
Else WScript.Echo "All Desktops are Switched on!"
End if