[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 840
  • Last Modified:

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??
0
zequestioner
Asked:
zequestioner
1 Solution
 
oBdACommented:
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)?
0
 
Brad GrouxCommented:
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
0
 
Steve KnightIT ConsultancyCommented:
You can do something like this in batch.... change the key/value to the one you want:

Steve

@echo off
SET Key="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SystemInformation"
Set Value="SystemProductName"
for /f "tokens=3" %%a in ('reg query %Key% /v %Value%^| find %value%') do set IPAddress=%%a
echo IP Address found is %ipaddress%

if "%ipaddress:~0,10%"=="192.168.1." (
  ECHO On subnet 192.168.1
  ECHO do something here
  EXIT /B
)

REM check any more subnets here etc.

Open in new window

0
 
zequestionerAuthor Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
No problem, glad to help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now