How do I get the computer name of a system and output it to a file in VBScript - windows-7

I am trying to get the computer name from the registry and write it to a file. At this point, my function call for obtaining the computer name from registry isn't working. Any advice would be appreciated.
Option Explicit
On Error Resume Next
Dim regComputerName, ComputerName
Set objShell = WScript.CreateObject("WScript.Shell")
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
regComputerName = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername"
ComputerName = obj.shell.RegRead(regComputerName)
oWrite.WriteLine(ComputerName,C:\text)

Reading registry values is error prone and may require elevated privileges in Windows 7. There's another way of getting the computer name, very similar to what you are doing right now:
Set objNetwork = WScript.CreateObject("WScript.Network")
ComputerName = objNetwork.ComputerName
MsgBox ComputerName
Also, the last line in your script: oWrite.WriteLine(ComputerName,C:\text) will not work for 2 reasons:
C:\text has to be in quotes, like this: "C:\text.txt"
In VB, only a function that results a value can be called with parenthesis. Call WriteLine like this instead: oWrite.WriteLine ComputerName, "C:\text.txt"
Finally, are you sure you are not referring to VBScript instead of VB in your question?

Your code is not working because of an error in this line:
ComputerName = obj.shell.RegRead(regComputerName)
Instead of obj.shell you should be referencing objShell. It should look like this:
Set objShell = WScript.CreateObject("WScript.Shell")
strRegKey = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\Computername"
strComputerName = objShell.RegRead(strRegKey)
WScript.Echo strComputerName
However, there are much more reliable ways of getting the computer name without having to deal with the registry.
From WSH (as suggested above)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputerName = WshNetwork.ComputerName
WScript.Echo "Computer Name: " & strComputerName
From an environmental variable...
Set wshShell = WScript.CreateObject("WScript.Shell")
strComputerName = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
WScript.Echo "Computer Name: " & strComputerName
From WMI...
strcomputer = "."
Set objWMISvc = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMISvc.ExecQuery("Select * from Win32_ComputerSystem",, 48)
For Each objItem in colItems
strComputerName = objItem.Name
WScript.Echo "Computer Name: " & strComputerName
Next
From ADSI...
Set objSysInfo = CreateObject("WinNTSystemInfo")
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
From ADSI (only works for domain members)...
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
...and one last way for Windows XP users only...
Set objPC = CreateObject("Shell.LocalMachine")
strComputerName = objPC.MachineName
WScript.Echo "Computer Name: " & strComputerName

Related

Vbscript to query computer name and then write to registry HKLM\software dword?

Hi could any one point me in the right direction to create Vbscript to query computer name and then write the query result to registry HKLM\software\test dword or string
this is what i have got so far i just dont know how to link the query then add the query result to the registry -thanks in advance
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName"
strValueName = "ComputerName"
oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
strValueName,strValue
From Help https://www.microsoft.com/en-au/download/details.aspx?id=2764
Computername
Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Domain = " & WshNetwork.UserDomain
WScript.Echo "Computer Name = " & WshNetwork.ComputerName
WScript.Echo "User Name = " & WshNetwork.UserName
Write Registry
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKCU\Software\ACME\FortuneTeller\", 1, "REG_BINARY"

Listing printers on remote machines. Not seeing the same results as I would if I were logged on locally as the user

My script is supposed to list all the printers installed on a remote machine and write that data to a text file while designating if the printer is Local or Network. When I run the script against my local machine with my profile logged on I get the following results:
Local
Microsoft XPS Document Writer
Network
\\PrintServer\PT-NJ-CPR-B-CORPIT-1
Network
\\PrintServer\PT-NJ-CPR-B-ITTEMP-1
Network
\\PrintServer\CPR5A26D1A
These results are exactly what I want however when I run the same script against a remote machine I still get results but they seem to be for a more generic user
Local
Send To OneNote 2010
Local
Microsoft XPS Document Writer
Local
Fax
My question is how do I customize my script to truly impersonate the logged on user thus returning me the full results even from a remote machine?
Const ForAppending = 8
Const ForReading = 1
Dim WshNetwork, objPrinter, intDrive, intNetLetter, fso
Set fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("C:\xVBS Scripts\Printer Scripts\Computers.txt", 1)
Do Until InputFile.AtEndOfStream
strComputer = InputFile.ReadLine
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objItem in colItems
UserName = objItem.UserName
arrUserName = Split(UserName, "\", -1, 1)
varUserName = arrUserName(1)
Next
filOutput = varUserName & ".txt"
If objFSO.FileExists(filOutput) Then
objFSO.DeleteFile(filOutput)
End If
Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)
For Each objPrinter in colInstalledPrinters
If objPrinter.Attributes And 64 Then
strPrinterType = "Local"
strTest = Left(objPrinter.Name, 2)
objOutputFile.WriteLine(strPrinterType)
objOutputFile.WriteLine(objPrinter.Name)
objOutputFile.WriteLine(vbNewLine)
Else
strPrinterType = "Network"
strTest = Left(objPrinter.Name, 2)
objOutputFile.WriteLine(strPrinterType)
objOutputFile.WriteLine(objPrinter.Name)
objOutputFile.WriteLine(vbNewLine)
End If
Next
Wscript.Sleep 1500
MsgBox "Printer mapping report is located" & vbNewLine & "in the following directory: " & filOutput , vbInformation, "Report Located At"
WshShell.Run "Notepad " & filOutput,1,False
Loop
InputFile.Close
Wscript.Quit
I dont think there is an actual answer to this. The more I learn about VB Script and Powershell it appears as if WMI is most useful when run interactively. It doesn't know how to process users who are not currently logged in. I bypass this problem by running the script as a GPO Link/Enforced that calls the script as a log on script. – JRN just now edit

Attempting to extract printers from users machine and then outputting to a text fill.

I am attempting to extract the printers from a users machine and then output to a text file but when I run the test I get a invalid procedure call or argument for this specific line of code.
Set objOutputFile = objFSO.OpenTextFile(outFile, ForAppending, True)
I have attempted to change OpenTextFileto CreateTextFile but I need the lines to appended to file as it will be running as a log on script.
I have done some research and used the Microsoft developer articles to help me debug the issue in the code but I don't have much experience in Visual Basic.
I have added the entire script to give context to the what is going on.
dim objComputerName, ObjNetwork , strText , objfile, StrComputer
dim wshnetwork
Set wshnetwork = CreateObject ("Wscript.network")
StrComputer = WshNetwork.ComputerName
If IsEmpty(StrComputer) Then Wscript.Quit
Set WshNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery("Select * from Win32_Printer")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
outFile = "C:\scripts\Printers" & StrComputer
Set objOutputFile = objFSO.OpenTextFile(outFile, ForAppending, True)
For Each objPrinter in colInstalledPrinters
strTest = Left(objPrinter.Name, 2)
objOutputFile.WriteLine(objPrinter.Name)
objfile.close
Next
Set objPrinter = WshNetwork.EnumPrinterConnections
'Set objOutputFile = objFSO.OpenTextFile (filOutput, ForAppending, True)
If objPrinter.Count = 0 Then
WScript.Echo "No Printers Mapped "
else
For intDrive = 0 To (objPrinter.Count -1) Step 2
intNetLetter = IntNetLetter +1
printer = "UNC Path " & objPrinter.Item(intDrive) & " = " & objPrinter.Item(intDrive +1) & " Printer : " & intDrive
objOutputFile.WriteLine(printer)
Next
end if
objOutputFile.Close``*
Invalid procedure call or argument
You passed an invalid parameter in your procedure call. This could be because the parameter was out of range, or contained invalid data. Alternately, you may have invoked a procedure at an unexpected time.
To correct this error
Verify that the parameters being passed to the procedure are valid.
Verify that you are calling the function at an appropriate time.
My guess is this line is an ilegal filename.
outFile = "C:\scripts\Printers" & StrComputer
On my computer this is c:\scripts\PrintersSerenity which is probably not right that your text file is called PrintersSerenity without an extension.

How can I pull the correct data from the HK Current User registry key instead of temp profile information

I am working on a script to pull the value in the key
HKCurrentUser\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop
Currently all it is returning is:
C:\WINDOWS\system32\config\systemprofile\Desktop
When I want/think it should return:
%USERPROFILE%\Desktop
Below is the script that is pulling the infomration from the key and as far as I can tell it should be pulling the correct information. Just wondering if someone can enlighten me as to what I am missing. It also returns the computer name and the logged in username which both return correctly. This is going to be run on quite a few machines remotely.
'These are the constants for the following KEYS'
Const HKClassesRoot = &H80000000 'HKEY_CLASSES_ROOT
Const HKCurrentUser = &H80000001 'HKEY_CURRENT_USER
Const HKLocalMachine = &H80000002 'HKEY_LOCAL_MACHINE
Const HKUsers = &H80000003 'HKEY_USERS
Const HKCurrentConfig = &H80000005 'HKEY_CURRENT_CONFIG
'Setup objects to interact with here'
Set wshShell = WScript.CreateObject("Wscript.Shell")
strComputer = wshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
Set objNetwork = CreateObject("Wscript.Network")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Define variable to store the current user and then pull the current user
Dim currentUser
strCurrentUser = objNetwork.UserName
'find the data in the string we want to get the value from'
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\"
strValueName = "Desktop"
'pull the info and store it in strValue'
objRegistry.GetStringValue HKCurrentUser,strKeyPath,strValueName,strValue
'setup for output of data to the file'
Dim strSpacer
Dim strData
strSpace = "+-------------------------------------------------------------------------------------------------------------------------+"
strData = "| " & strComputer & " == " & strCurrentUser & " == " & strValue & " |"
Dim strFileName
strFileName = "\\server\share\" & strCurrentUser & ".txt"
Set objFile = objFSO.OpenTextFile(strFileName,8,true)
objFile.write vbCrLf & strSpace & vbCrLf
objFile.write strData & vbCrLf
objFile.write strSpace & vbCrLf
'Close file'
objFile.Close
After review I found the answer to my own question. I was reading the registry incorrectly for what I was doing.
strRegkey = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"
strDataValue = wshShell.RegRead(strRegKey)
this returns the value stored currently in the key.
I suspect that the environment variable %USERPROFILE% from the registry value gets expanded to the profile of the user running the WMI service (LOCAL SYSTEM). GetStringValue seems to behave the same as GetExpandedStringValue when reading REG_EXPAND_SZ values.

Pulling a remote windows server architecture in VBScript

I have the following code. I am trying to get the information on whether or not the remote computer I am connecting to is 32-bit or 64 bit. I tried doing it at the last snippet of this code but it didn't work. Here is the error (I changed the remote system name):
WshShell.RegRead: Invalid root in registry key
"\*remotesystem*\HKEY_LOCAL_MACHINE\SYSTEM\CurrentCon
trolSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE".
Option Explicit
Dim objWMISrvc,strRemoteComputer,colOSItems,objItem,args,OsType
'String variables
Dim strName,strCaption,strVersion,strCSDVer,strSerial,WshShell
'Adding this in to transfer FQDN variable to this script from ASP.net
Set args = WScript.Arguments
strRemoteComputer = args.Item(0)
Set objWMISrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strRemoteComputer & "\root\cimv2")
CheckOSType objWMISrvc
Sub CheckOSType( objWMISrvc )
Set colOSItems = objWMISrvc.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colOSItems
strName = objItem.CSName
'strDesc = objItem.Description
'strManufac = objItem.Manufacturer
strCaption = objItem.Caption
strVersion = objItem.Version
strCSDVer = objItem.CSDVersion
strSerial = objItem.SerialNumber
Next
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("\\" & strRemoteComputer & "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
If OsType = "x86" then
WScript.Echo "Windows 32bit system detected"
elseif OsType = "AMD64" then
WScript.Echo "Windows 64bit system detected"
end if
End Sub
You can do this directly from WMI without reading the registry. Just loop through your computer names with this script. It returns either "32" or "64".
strComputer = "."
Set objWMIService = GetObject("winmgmt:\\" & strComputer & "\root\cimv2")
Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objProcessor in colProcessors
WScript.Echo objProcessor.AddressWidth 'or objProcessor.DataWidth
Exit For
Next
To read the registry of as remote machine you must use the the StdRegProv WMI class
Check this sample
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject( _
"winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
strValueName = "PROCESSOR_ARCHITECTURE"
oReg.GetStringValue _
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,OsType
WScript.Echo OsType

Resources