Link to home
Start Free TrialLog in
Avatar of zequestioner
zequestioner

asked on

Batch file to compare ip address with registry key

hello,

i have software that puts ip address in registry key such as:

HKLM\Software\Key

Key has ip address: 192.168.1.158

I want to compare the 'network' portion of the ip address in this key to a 'static network portion' such as 192.168.1  (/24) for this part i can set variable such as staticnetwork=192.168.1 and if registry contains this string as a positive match then i need to make an action such as install.exe


If the ip address in the key matches the correct network portion, then i need to perform action. and if not then i take separate action.

can you show me how this is accomplished??
Avatar of oBdA
oBdA

Please refine your description of the registry part, because it's not quite clear currently.
Do you have a key (a "folder" in regedit) as in "HKLM\Software\Acme\192.168.1.158", with some values in there, or dou you have a key like "HKLM\Software\Acme" with a value "IPAddress" and the data "192.168.1.158"?
Could you post a regedit export or a screenshot (with sensitive information removed, of course)?
This should be possible with PowerShell and WMI (Get-WmiObject Win32_NetworkAdapterConfiguration).

The Lonely Administrator has a great write up about how to get IP data using a function - http://jdhitsolutions.com/blog/2011/03/get-ip-data/

You can then use PowerShell's conditional logic to perform the if/then tasks - http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-conditional-logic/

Get-ItemProperty Cmdlet looks like to be the right track - http://technet.microsoft.com/en-us/library/ee176852.aspx

To learn more about PowerShell check out Microsoft Virtual Academy's fantastic free courses -
http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start
http://www.microsoftvirtualacademy.com/training-courses/advanced-tools-scripting-with-powershell-3-0-jump-start
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zequestioner

ASKER

I apologize for not being as specific, it is a registry key of type REG_SZ.

Steve, when I put in the key path and set the value to the 'Value Name' of the key and the Value Data is the IP address, the ip does not match the subnet even though it does. I think i figure it out b/c your ip has 10 cahracters and mine has 12 so now i got it to work just fine. thank you for the help.

Brad thank you for your help but steve solution is very easy and straightforward to figure out.

Thank you steve this is exactly what i need and is perfect solution.
No problem, glad to help.