How to assign Static IP based on MAC Address using powershell/script in Windows? - wmi

I would like to write up a script to assign static IP based on mac addresses, as I am having trouble with "USB to ethernet" adapters lose it's IP settings and assign to different interface Names.
I am running on windows 10 environment and have found a wmi script online that I think might work.
Code I am using:
wmic nicconfig where macaddress="0F:98:90:D6:42:92" call EnableStatic ("192.168.1.1"), ("255.255.255.0")
Error output:
"Invalid format.
Hint: = [, ]."
Thanks

Something like this
$netAdapter = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.MACAddress -eq '0F:98:90:D6:42:92'}
$netAdapter.EnableStatic("192.168.1.1", "255.255.255.0")

Related

How can we get a CPU temperature through WMI?

I installed WMI code creator from here, and I'm wondering how we can use it to get the CPU temperature.
The application gives many options (as shown below), but I am not sure where I have to click to get the CPU temperature.
I went to the description of WMI code creator and saw the following:
The WMI Code Creator tool allows you to generate VBScript, C#, and VB
.NET code that uses WMI to complete a management task such as querying
for management data, executing a method from a WMI class, or receiving
event notifications using WMI.
Namespace: root\wmi
Path: MSAcpi_ThermalZoneTemperature
To run this (using wmic) from the Windows command line (cmd.exe) the command would be:
wmic /namespace:\\root\wmi PATH MSAcpi_ThermalZoneTemperature get CriticalTripPoint, CurrentTemperature
Attention: the results are in Kelvin * 10, so you need to divide the result by 10, and then subtract 273.15 to get °Celsius.
More information:
WUtils.com : MSAcpi_ThermalZoneTemperature Properties
Wikipedia : Kelvin (and conversion to/from °C and °F)
Wikipedia : Windows Management Instrumentation (WMI)
MSDN : WMI Command Line (WMIC)
Tech Advisor : What's the Best CPU Temperature?
SpeedFan : Fan & Temperature control utility (Freeware)
systeminformation: systeminformation npm package (for nodejs)
For those looking for a PowerShell solution:
Get-CimInstance -Namespace root/wmi -ClassName MsAcpi_ThermalZoneTemperature -Filter "Active='True' and CurrentTemperature<>2732" -Property InstanceName, CurrentTemperature |
Select-Object InstanceName, #{n='CurrentTemperatureC';e={'{0:n0} C' -f (($_.CurrentTemperature - 2732) / 10.0)}}
The WMI/CIM filter here is only looking for active sensors that aren't returning 0 C as the temperature. My system returns several sensors with that value and I assume they're just disabled or otherwise non-functional. The InstanceName property should give you an approximate idea where the sensor is located. In my systems, even though the property is supposed to be in tenths of degrees K, it's always reporting in whole degree values and it appears to round the -273.15 C absolute zero to -273.2. Other systems or sensors may vary.
Note that you must be an administrator to query the MsAcpi_ThermalZoneTemperature class.
My HP laptop has HP specific WMI objects that contains temperature in Celcius units and fan RPM speed objects.
Running this WMIC command in administrator mode will retrieve them:
wmic /NAMESPACE:\\root\HP\InstrumentedBIOS path HPBIOS_BIOSNumericSensor get Name,CurrentReading
Adding for syntax for looping it every 5 seconds:
FOR /L %G IN (1,1,100) DO wmic /NAMESPACE:\\root\HP\InstrumentedBIOS path HPBIOS_BIOSNumericSensor get Name,CurrentReading && PING localhost -l 0 -n 5 >NUL
On my Dell-Laptop I could get the CPU-Temperature in Celsius like this:
$data = Get-WMIObject -Query "SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation" -Namespace "root/CIMV2"
#($data)[0].HighPrecisionTemperature

GetComputerName return is too short for my computer name... what to do?

My computer name is similar to this: "ABC12-PEACEBRINGER" (18 characters) and it's a windows xp machine although the final program also needs to run on windows 7.
When I use the following c++ code the computer name gets cut off and the return is
ABC12-PEACEBRIN.
TCHAR MachineName[32];
DWORD buf = 32;
GetComputerNameA(MachineName, &buf);
Btw. when I type ipconfig /all into my cmd I do get the complete computer name.
I tried some different solutions from the msdn homepage but the result remains the same.
Does anyone know a solution to get a computer name of that length?!
...Changing the name is not a solution ;-)
NetBIOS names (which GetComputerName returns) are limited to 15 characters.
You likely want some other variation -- look at GetComputerNameEx to see your options.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724301%28v=vs.85%29.aspx

vimrc to detect remote connection

At the moment I have to hard code the names of servers on my vimrc in order to either make it different on the remote machine. This is done by conditional statement using hostname() function in vim. I want to make the conditional to be based on the status of remote connection and not on the hostname. So...
The first possible solution I found was using the following bash command in system():
cat /proc/$PPID/status | head -1 | cut -f2
This does not work because I use GNU screen and this will not detect my connection status properly.
The second possible solution I am exploring right now is using who am i This reliably shows whether or not remote connection has been made from which client, but I have trouble getting it working with system()
if substitute(system('who am i'), "theclient", ????, "") == ""
...
How could I get ???? to extract my client name somehow??
Even if the second solution works, allowing me to use .vimrc for many different remote machines, it is still tied to one client. I want the conditional to work in all remote session, regardless of the client name. So I am wondering, is this possible?
The following line allows me to create a variable that detects the remote connection status:
let g:remoteSession = ($STY == "")
Now you can surround the lines that you want to be ignored in the remote connection via:
if g:remoteSession
...
endif
On a side note, I do not know how expensive it is look up the environment variable compared to the global variable, but I am guessing the difference is negligible. The system call in an environment like cygwin where fork() is inefficient, it is worth doing the optimization.
Instead of adding conditional logic to a shared ~/.vimrc, you could alternatively source system-local settings. I use the following:
" Source system-specific .vimrc first.
if filereadable(expand('~/local/.vimrc'))
source ~/local/.vimrc
endif
" Stop sourcing if inclusion guard exists.
if exists('g:loaded_vimrc')
finish
endif
" Common settings of .vimrc here...
I find this more scalable than trying to maintain an ever-changing list of hostnames in a central location.

Getting Mac Address in Groovy

Just want to ask if there's a way of getting your local machine's Mac address as String.
I need to save my local machine's mac address into a domain class. I tried using this code to get my Mac address:
String address = "ifconfig".execute().text()
But this line it also returns a lot of details about my ip address, all I need to get is the Mac Address which is found after the substring "HWaddr". I'm thinking if i could extract this substring using regex but I am not sure how to do it.
You can get the MAC address for an interface using java.net.NetworkInterface. Note that it is possible to have multiple hardware network interfaces, so it's possible to have more than one MAC address. In addition, most machines will have at least one interface without a hardware address: the loopback interface.
This will get a list of all the MAC addresses as Strings, including nulls for interfaces without a MAC address:
import java.net.NetworkInterface
def macs = NetworkInterface.networkInterfaces.collect { iface ->
iface.hardwareAddress?.encodeHex().toString()
}
You can use the NetworkInterface to get this information, especially the getHardwareAddress() method which return a byte array

Get Machine id of MAC OS X

I want machine unique id such as processor id, hdd id, uuid of MAC PC through c++ program.
Can anyone please tell me how it implements?
Thanks.
only about 7 years later, but here's an answer to those stumbling across this that we've been using.
It uses the IOPlatformExpertDevice class to access the Mac Serial number/hardware uuid
There are two ways to do this, the first uses C++, the second python. I have personally used the second way, and can verify it fetches the hardware uuid as given by System Information.
First method, not tested by myself, but uses the same class so has at least the potential to work, see https://gist.github.com/tmiz/1294978 for a routine in C++ on how to retrieve the "serial number" which is not be the same as the hardware uuid from system information, but from some tweaking, you should be able to get the hardware uuid.
Second method (see python code below), in python, which uses the ioreg command, which is executed via a separate process, then the results processed with a regular expression to get the uuid. This method definitely retrieves the hardware uuid as I've checked it with the System Information app in macos 10.14 and previous versions of 10.13 and 10.12.
May these methods serve you well, they do not return the mac address and as such should function well as a uuid for the machine, not just the network interface.
Finally you can read about ioreg here -> http://www.manpagez.com/man/8/ioreg/ and the I/O Kit more generally here -> https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/Families_Ref/Families_Ref.html#//apple_ref/doc/uid/TP0000021-BABHIGFE
import platform, re, os
os_type = platform.system()
if os_type == 'Darwin':
machine_uuid_str = ''
p = os.popen('ioreg -rd1 -c IOPlatformExpertDevice | grep -E \'(UUID)\'', "r")
while 1:
line = p.readline()
if not line: break
machine_uuid_str += line
match_obj = re.compile('[A-Z,0-9]{8,8}-' +\
'[A-Z,0-9]{4,4}-' +\
'[A-Z,0-9]{4,4}-' +\
'[A-Z,0-9]{4,4}-' +\
'[A-Z,0-9]{12,12}')
results = match_obj.findall(machine_uuid_str)
return results[0]
Outside of a few ancient processors, x86 CPUs do not have software-visible serial numbers.
Apple recommends that you use the MAC address of the computer's primary network interface (i.e, the onboard Ethernet controller if present, or the wireless interface otherwise) as a unique identifier for the system. Sample code for doing this is available in Apple's Validating Mac App Store Receipts documentation (under "Get the Computer's GUID").