How can we enumerate all the network connections in order to extract the IP address of the VPN connection using WMI? On XP, Win32_NetworkAdapterConfiguration works fine but on Vista it only seems to enumerate the physical connections...
If you look at the comments under the documentation for Win32_NetworkAdapterConfiguration you'll see a reference to Win32_NetworkAdapter when dealing with Vista.
'Vista only code???
Set colAdapters = objWMIService.Execquery ("SELECT * FROM Win32_NetworkAdapter WHERE NetEnabled = True")
For Each nic in colAdapters
msg = "nic.DeviceId: " & nic.DeviceId & vbCRLF _
& "nic.Name: " & nic.Name & vbCRLF _
Next
From this you should be able to retreive the InterfaceIndex and lookup the IP address from the Win32_IP4RouteTable class.
It certainly is a roundabout way of getting the information compared to using Win32_NetworkAdapterConfiguration.
Found it in the MSFT classes! Windows Specific implementation of CIM interface object:
gwmi msft_netIPAddress -Namespace 'root/standardcimv2' | format-list -Property InterfaceAlias,IPAddress
Related
I need to change my host IP Address on Linux app using Qt. I have readed documentation about QHostAddress and method setAddress in this class that say the following:
"Sets the IPv4 or IPv6 address specified by the string representation >specified by address (e.g. "127.0.0.1"). Returns true and sets the address >if the address was successfully parsed; otherwise returns false."
I know that it is possible using QProccess but I'm trying to use Qt-way in order to do that. I'm very confused becuase my app is not in running with root privileges so I find very difficult perform this action using Qt class directly. Then I try this:
QHostAddress hostAddress;
bool ipChange = hostAddress.setAddress("192.168.1.143");
if(ipChange) qDebug() << "IP ADDRESS CHANGED";
else qDebug() << "IP ADDRESS NOT CHANGED";
The result of this code is "IP ADDRESS CHANGED" but doing ifconfig in a terminal, my IP address has not been modified. So, my questions are:
How I can do that?
Why I can see IP ADDRESS CHANGED if this method obviously doesn't works?
You are changing the address stored in hostAddress. You can now use hostAddress to (e.g.) open a stream socket to a port on 192.168.1.143. This has no relation to any IP addresses of the host you happen to be running on - QHostAddress is just a representation of any IP address.
To set an address for a network interface on the host machine, you will need to be root, and to use the native facilities (or an external process - /sbin/ifconfig, for example).
I am very new at scripting and I need some help. I have an unattended windows XP install that I created. We use two different Nvidia cards, about 50 of each, they use different drivers. I would like to be able to install the correct driver, based on the PNP Device ID. The script below outputs the PNP Device ID, next I want to capture the PNP Device ID and install the correct driver. I just need to be able to read the PNP Device ID, then run a silent install for the correct driver and software. I need to have the full Nvidia software installed, not just the driver. Any help would be greatly appreciated.
Paul
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_VideoController")
For Each objItem in colItems
For Each strCapability in objItem.AcceleratorCapabilities
Wscript.Echo "Accelerator Capability: " & strCapability
Next
Wscript.Echo "PNP Device ID: " & objItem.PNPDeviceID
Next
Const EVENT_SUCCESS = 0
I would recommend using Windows PowerShell for this, instead of VBscript. If you're new to scripting, then I'd suggest that learning PowerShell would be much more advantageous than learning VBscript. You'll be able to do a lot more, with a lot less code.
Let's move on to some code:
# If a video controller matches the string on the right of the -match operator ...
if ((Get-WmiObject -Class Win32_VideoController) -match 'VEN_10DE&DEV_0DFC') {
# ... then run this program
Start-Process -FilePath setup.exe -ArgumentList '/silent /log:"nvidia.log"';
}
Has anyone got any suggestions to speed up this WMI query? I'm updating a client application every 5 seconds to show the CPU stats. It was much quicker on Windows 2003 but takes at least 5 seconds to return an integer for 4 CPU cores:
Private Sub GetProcessorIdleTime(ByVal Server As String)
Dim searcher As New ManagementObjectSearcher("\\" & Server & "\root\CIMV2", "SELECT LoadPercentage FROM Win32_Processor")
Dim collection As ManagementObjectCollection = searcher.[Get]()
For Each row In collection
TextBox1.Text = TextBox1.Text & vbCrLf & Convert.ToInt32(row("LoadPercentage"))
Next
End Sub
Or is there a better way to retrive this information remotely?
To improve the performance, you must re-use the WMI connection to the remote server, establishing a connection is one of the more expensive tasks when you execute a WQL sentence.
In your code you are setting a new WMI remote connection each time. So rewrite your code creating a new method to establish the remote connection and then reuse (share) the ManagementObjectSearcher object in your GetProcessorIdleTime method.
I'm trying to determine if a laptop is connected to AC power.
The OS Im running under is WinPE.
My app is written in native C++.
WMI queries using Win32_Battery are not supported and the GetSystemPowerStatus API always returns '1' for ACLineStatus (running on AC power or not).
Any ideas?
Additonal investigation:
Just tried the API 'CallNtPowerInformation' with POWER_INFORMATION_LEVEL::SystemBatteryState. The SYSTEM_BATTERY_STATUS structure element AcOnLine also returns 1 regardless of power supply status. Probably just calls the same system level code but thought I'd add it in here.
I managed to answer my own question and it proved to be very simple in the end.
In WinPE the following noddy script returned null when executed because the battery wasn't being recognised:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Battery",,48)
For Each objItem in colItems
Wscript.Echo objItem.BatteryStatus
Wscript.Echo objItem.Caption
Next
I found a battery device driver in my PE image (\windows\inf\battery.inf) which once installed resulted in the battery being recognised and the above script returning the expected values. i.e. BatteryStatus = 2 (The system has access to AC so no battery is being discharged) or BatteryStatus = 1 (The battery is discharging i.e. AC not connected).
Driver can be installed in the PE image itself or loaded on demand. i.e. drvload
Specifically, I want to listen to when programs are run and record information such as: timestamp, executable, windows name and user.
Alternatively, use the WMI interface to find out what's running and take appropriate action. In the VBScript code below the WMI subsystem is being queried with Select * from Win32_Process so as to change the process priority. Find out what other attributes are available for Win32_Process and you should find stuff that's heading in the direction you want to go.
Const NORMAL_PRIORITY = 32
Const LOW_PRIORITY = 64
Const REALTIME_PRIORITY = 128
Const HIGH_PRIORITY = 256
Const BELOWNORMAL_PRIORITY = 16384
Const ABOVENORMAL_PRIORITY = 32768
Function SetPriority( sProcess, nPriority )
Dim sComputer
Dim oWMIService
Dim cProcesses
Dim oProcess
Dim bDone
bDone = False
sComputer = "."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set cProcesses = oWMIService.ExecQuery ("Select * from Win32_Process Where Name = '" & sProcess & "'")
For Each oProcess in cProcesses
oProcess.SetPriority( nPriority )
bDone = True
Next
SetPriority = bDone
End Function
The most obscene way of doing this is the Google-desktop way
Namely to have your DLL load into every process that is ever started and to log information.
If you're interested more, install google desktop and watch its dll load into your processes. Then look in the registry to see it does it.
Be mindful that this is entering to the realm of virus like behaviour.
I would use the PSAPI function EnumProcesses() to periodically get a list of running processes.
You could set up a WMI permanent event subscription to monitor process creation and log the details. I have some samples here - one of the samples monitors notepad.exe creation and logs the events in a txt file. Permanent event subscription monitors events 'at all times', but if you want to monitor events 'for the duration of your application', you can use WMI COM API with C++ - the WQL event query is the same in both cases. The documentation is here.
Look into using the Perfmon API's (check MSDN for references).