?
Solved

Powershell script - IP of local PC

Posted on 2012-09-02
6
Medium Priority
?
1,010 Views
Last Modified: 2012-09-03
I am attempt to write a powershell script that will extract the IP address of a local PC it is being run on.

So far I have written this:

$strComputerID = "."
$Ipinfo = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE and DHCPEnabled=TRUE" -comp $strComputerID
# get-member -inputobject $Ipinfo ----------- I am using this line to identify the type of info available
get-member -name IPAddress $IPinfo

What am I missing. I understand $Ipinfo is an array, but I can't extract individual information from it like IPAddress or DefaultIPGateway and so on.

Ideas?
0
Comment
Question by:alienvoice
6 Comments
 
LVL 14

Accepted Solution

by:
theruck earned 400 total points
ID: 38359975
ipconfig | find "IPv4"

maybe you could say what are you trying to achieve. looks like you are trying something easy to do vey complicated way
0
 
LVL 15

Assisted Solution

by:Ian Meredith
Ian Meredith earned 400 total points
ID: 38359983
Not sure if this is what you're after, but this script gets the ip address...

http://pshscripts.blogspot.com.au/2009/01/get-ipaddressps1.html
0
 
LVL 16

Assisted Solution

by:Dale Harris
Dale Harris earned 400 total points
ID: 38360062
Try this:
ipconfig | Select-String IPv4
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 6

Assisted Solution

by:slidingfox
slidingfox earned 400 total points
ID: 38360138
Because IP Info is an array, you'll can't access a property for the entire array.

Try $IpInfo[0] for the first item in the array, then $IPInfo[1] for the next, and so on...

If you want to access all the items in the array

Try the following:

$ipinfo | Select -ExpandProperty IPAddress

or

$ipinfo | Select -ExpandProperty DefaultIPGateway
0
 
LVL 3

Assisted Solution

by:Akulsh
Akulsh earned 400 total points
ID: 38361184
In single lines:

For IP Address -
(get-wmiobject win32_NetworkAdapterConfiguration -filter "IPEnabled=True and DHCPEnabled=True").IPAddress

For Default Getway:
(get-wmiobject win32_NetworkAdapterConfiguration -filter "IPEnabled=True and DHCPEnabled=True").DefaultIPGateway

and so on for
IPSubnet
DHCPServer  
DNSDomain
DNSHostName
DNSServerSearchOrder
0
 
LVL 15

Author Comment

by:alienvoice
ID: 38361933
Thanks everyone, suggestions were appreciated. Most of the suggestions worked, I have gone with code as listed below:

$strComputerID = "."
$Ipinfo = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE and DHCPEnabled=TRUE" -comp $strComputerID
$IPinfo.IPAddress

Reason behind this is, it will allow me to modify the input if needed at a later date and it will run a check to verify the IP is DHCP enabled, (which is important for the situation it will be used in).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question