I have a script which allows me to grab certain machine info when a user runs the script, and the result gets written on a text file on a server share. The script works fine...however, I would like for it not to repeat the headers when the next user runs the script. I searched around the forums but couldn't find anything that would allow me to accomplish this. Apologies if my language is dull. I'm sure it's something pretty easy and would appreciate if someone could give me some pointers. I'm not a programmer but I'm very fascinated on writing scripts that makes my job easier.
Currently, this is how the result looks like.
User Name, Host Name, Manufacturer, Serial Number, Operating System, Model
CA\username, NAPHX-C63E90K-L, LENOVO, PC63E90K, Microsoft Windows 7 Enterprise 6.1.7601, 20AMS1UD00
User Name, Host Name, Manufacturer, Serial Number, Operating System, Model
CA\username, USAPHX-6GL4R7-L, LENOVO, PC6GL4R7, Microsoft Windows 10 Enterprise 10.0.17134, 20FMS3XC00
User Name, Host Name, Manufacturer, Serial Number, Operating System, Model
CA\username, USAPHX-6KTBAY-L, LENOVO, PC6KTBAY, Microsoft Windows 10 Enterprise 10.0.17134, 20EQS2NM00
User Name, Host Name, Manufacturer, Serial Number, Operating System, Model
CA\username, MEXMEC-6Z0A6U-L, LENOVO, PC6Z0A6U, Microsoft Windows 10 Enterprise 10.0.17763, 20L8S6G20E
I want it to look like this instead.
User Name, Host Name, Manufacturer, Serial Number, Operating System, Model
CA\username, NAPHX-C63E90K-L, LENOVO, PC63E90K, Microsoft Windows 7 Enterprise 6.1.7601, 20AMS1UD00
CA\username, USAPHX-6GL4R7-L, LENOVO, PC6GL4R7, Microsoft Windows 10 Enterprise 10.0.17134, 20FMS3XC00
CA\username, USAPHX-6KTBAY-L, LENOVO, PC6KTBAY, Microsoft Windows 10 Enterprise 10.0.17134, 20EQS2NM00
CA\username, MEXMEC-6Z0A6U-L, LENOVO, PC6Z0A6U, Microsoft Windows 10 Enterprise 10.0.17763, 20L8S6G20E
Here's a sample of my current script.
' On Error Resume Next
' Constants for FileSystemObject
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
strFileOutput = "\\servershare\folder\info.txt"
' Create a Script Runtime FileSystemObject.
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Check to see if the output file exists. If so, open it for writing or appending.
' If not, create it and open it for writing.
If objFSO.FileExists(strFileOutput) Then
Set objOutputFile = objFSO.OpenTextFile (strFileOutput, FOR_APPENDING)
Else
Set objOutputFile = objFSO.CreateTextFile(strFileOutput)
End If
If Err <> 0 Then
Wscript.Echo "Unable to open " & strFileOutput & " for output."
WScript.Quit
End If
'Create Headers for Host, Manufacturer, Serial Number
objOutputFile.Writeline "User Name, Host Name, Manufacturer, Serial Number, Operating System, Model"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colSettings
PCMfg = objComputer.Manufacturer
PCName = objComputer.Name
UserName = objComputer.UserName
'Wscript.Echo "User: " & Manufacturer: " & PCMfg & VbCrLf & "PC Name: " & PCName
NEXT
Set colBIOS = objWMIService.ExecQuery ("Select * from Win32_BIOS")
For each objBIOS in colBIOS
SerNo = objBIOS.SerialNumber
'Wscript.Echo "Serial Number: " & SerNo
NEXT
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
OS = objOperatingSystem.Caption & " " & objOperatingSystem.Version
'Wscript.Echo "Operating System: " & objOperatingSystem.Caption & " " & objOperatingSystem.Version
Next
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT Version FROM Win32_ComputerSystemProduct")
For Each objItem in colItems
Model = objItem.Name
'Wscript.Echo "Model: " & objItem.Name
Next
objOutputFile.Writeline Username & ", " & PCName & ", " & PCMfg & ", " & SerNo & ", " & OS & ", "& Model
objOutputFile.Close
x=msgbox("Process is now complete. Thank you for your time!" ,0, "PC Info Complete")
Move the line that creates the headers to the file exists check, so that they are created only once per file:
If objFSO.FileExists(strFileOutput) Then
Set objOutputFile = objFSO.OpenTextFile (strFileOutput, FOR_APPENDING)
Else
Set objOutputFile = objFSO.CreateTextFile(strFileOutput)
'moved this line
'Create Headers for Host, Manufacturer, Serial Number
objOutputFile.Writeline "User Name, Host Name, Manufacturer, Serial Number, Operating System, Model"
End If
Is any a way to detect if headphones are plugged in or not via VBScript?
This link doesnt help Switching current active sound device using VBScript?
You should be able to use the Win32_SoundDevice WMI class. Here is a sample script that might be a good starting point:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_SoundDevice",,48)
For Each objItem in colItems
Wscript.Echo "Availability: " & objItem.Availability
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Status: " & objItem.Status
Wscript.Echo "StatusInfo: " & objItem.StatusInfo
Next
(source)
I would do some comparison runs before and after plugging in your headphones and go from there.
I want to detecting if the current running bat script is hidden by the caller, that means (nCmdShow=0) for example.
There is windows API to get this information, GetStartupInfo, but it can not be called by command prompt or VBScript(without third party libraries).
The following script can retrieve the startup information, but the problem is that's only works under WinXp, it's doesn't work under Win7. I am looking for a way can support across winxp - win8.
Dim wmiService
Set wmiService = GetObject("winmgmts:\\.\root\cimv2")
Dim startupInfo
Set startupInfo = wmiService.Get("Win32_ProcessStartup")
The following code works fine under xp, but doesn't work under win7, it's show all the startup information.
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ProcessStartup",,48)
For Each objItem in colItems
Wscript.Echo "CreateFlags: " & objItem.CreateFlags
Wscript.Echo "EnvironmentVariables: " & objItem.EnvironmentVariables
Wscript.Echo "ErrorMode: " & objItem.ErrorMode
Wscript.Echo "FillAttribute: " & objItem.FillAttribute
Wscript.Echo "PriorityClass: " & objItem.PriorityClass
Wscript.Echo "ShowWindow: " & objItem.ShowWindow
Wscript.Echo "Title: " & objItem.Title
Wscript.Echo "WinstationDesktop: " & objItem.WinstationDesktop
Wscript.Echo "X: " & objItem.X
Wscript.Echo "XCountChars: " & objItem.XCountChars
Wscript.Echo "XSize: " & objItem.XSize
Wscript.Echo "Y: " & objItem.Y
Wscript.Echo "YCountChars: " & objItem.YCountChars
Wscript.Echo "YSize: " & objItem.YSize
Next
There is a way of calling API calls in VBS or batch.
appactivate between multiple internet explorer instances
Although the sample given doesn't work 7 and later because of a name conflict. Rename sendmail to something else for 7 and later.
If a limited user you need to manually add the registry entries to hkcu\software\classes.
I need to create a subroutine to check for certain policies on a system. I'm currently just trying to do that.
strComputerFQDN is defined at the beginning of the program and that is working fine.
Do you see anything wrong?
Here is my code:
'*************************************************************************
' This Subroutine checks Local Policies
'*************************************************************************
Sub CheckPolicies()
Dim objGPOSrvc,colItems,objItem
Set objGPOSrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerFQDN & "\root\RSOP")
Set colItems = objGPOSrvc.ExecQuery("SELECT * FROM RSOP_GPO")
WScript.Echo("Check Policies")
WScript.Echo("------------------------------------")
For Each objItem in colItems
If InStr(UCase(objItem.Name),"WSUS") Then
If InStr(UCase(objItem.Name),"SERVER") Then
WScript.Echo("Policy applied: " & objItem.Name)
Else
WScript.Echo("Wrong WSUS Policy Applied - Check Computer object location")
End If
End If
Next
If strWSUSApplied = "FALSE" Then
WScript.Echo("No WSUS Policy Applied!")
End If
WScript.Echo vbCrLf
End Sub
The namespace should be root\RSOP\Computer
Set objGPOSrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerFQDN & "\root\RSOP\Computer")
or root\RSOP\User
Set objGPOSrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerFQDN & "\root\RSOP\User")
Most typically you would do something like this:
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\RSOP\Computer")
Set colItems = objWMIService.ExecQuery("Select * from RSOP_GPO")
For Each objItem in colItems
WScript.Echo "Name: " & objItem.Name
WScript.Echo "GUID Name: " & objItem.GUIDName
WScript.Echo "ID: " & objItem.ID
WScript.Echo "Access Denied: " & objItem.AccessDenied
WScript.Echo "Enabled: " & objItem.Enabled
WScript.Echo "File System path: " & objItem.FileSystemPath
WScript.Echo "Filter Allowed: " & objItem.FilterAllowed
WScript.Echo "Filter ID: " & objItem.FilterId
WScript.Echo "Version: " & objItem.Version
WScript.Echo
Next
If you receive Error 0x80041003, you will need to run this script with administrator credentials. For Vista and later, open your start menu and type cmd. When "Command Prompt" appears, right-click and choose Run As Administrator. You can now launch your script from the elevated command prompt without error.
Is there a way to retrieve the voltage being supplied to a computer in real time?
You can use WMI to get some of that information. I used the WMI Code Creator to come up with this VB script which returns the Win32_Processor CurrentVoltage value. If you have a laptop, there is also some battery information available in WMI.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Processor",,48)
For Each objItem in colItems
Wscript.Echo "-----------------------------------"
Wscript.Echo "Win32_Processor instance"
Wscript.Echo "-----------------------------------"
Wscript.Echo "CurrentVoltage: " & objItem.CurrentVoltage
Next