Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell question

Posted on 2014-02-13
9
Medium Priority
?
449 Views
Last Modified: 2014-02-13
Hi!

I need a powershell script that gets the ip address of the host / PC that the script runs on, and place it into a .ini file. It needs to replace an ip address thats allready in the .ini file.

How can i achieve this?
0
Comment
Question by:SvenIA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 300 total points
ID: 39855608
This
Get-WmiObject -Class 'Win32_NetworkAdapterConfiguration' -Filter 'IPEnabled = True' | %{$_.IPAddress}

Open in new window


will give you IP addresses (v4 and v6) for all your enabled adapters.

If you want further help, we'll need the structure of the ini file.

HTH,
Dan
0
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 300 total points
ID: 39855619
to get current ip address use the following:
(Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | where{$_.IPEnabled -eq “True”}).IPAddress[0]

Open in new window


how the ini file looks like?
0
 
LVL 7

Author Comment

by:SvenIA
ID: 39855655
The IP Address after 'localHostAddress =' must be changed into the IP Address of the pc that the script runs on....

[NetworkManagementCard]
mode = single
enrolWithNMC = true
acceptCerts = true
IP_1 = 10.0.0.1
protocol = http
port = 80
IP_1_Outlet = 1

[EventLog]
logsize = 1000
logfile = EventLog.txt

[Shutdown]
shutdownCommandFileDelay = 0
shutdownCommandFile =
event_ShutdownStarting_enableLogging = true
turnOffUps = false
turnOffSOG = false
shutdownCommandFileEnabled = false
shutdownCommand = C:\Program Files\group1\bin\shutdown.cmd
shutdownDelay = 10
shutdownCommandDuration = 60
shutdownCommandFileDuration = 30

[Networking]
httpPort = 3052
httpsPort = 6547
protocol = https
localHostAddress = 10.0.0.2

[SoftwareUpdate]
enableUpdateCheck = false
updateServerURL = https://autoupdatev2.apcc
updateCheckInterval = 7
devBuild = false
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39855673
(Get-Content C:\your\path\test.ini) | 
% {$_ -replace "localHostAddress = .*$", "localHostAddress = " + ((Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" | where{$_.IPEnabled -eq “True”}).IPAddress[0])} | 
Set-Content C:\your\path\test.ini

Open in new window


Note: if you have multiple adapters, IPAddress[0] might not be the one you want.
0
 
LVL 7

Author Comment

by:SvenIA
ID: 39855692
Thanks, but it shows an error;

"The -replace operator only permits 2 elements on the right-hand side, not 3"
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1400 total points
ID: 39855719
It's even a bit more simple (no need for a loop here):
(Get-Content C:\your\path\test.ini) `
  -replace 'localHostAddress = .*$', `
                ('localHostAddress = ' + (Get-WmiObject Win32_NetworkAdapterConfiguration -filter 'IPEnabled="True"').IPAddress[0]) | 
  Set-Content C:\your\path\test.ini

Open in new window

and be careful with regular expressions and double quotes - RegEx should be included in single quotes, to avoid conflicts between string substitution of PS and RegEx.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39855723
The error is because the -replace has precedence over +, so we need to enclose the replacement expression in parens. I've done that already above, however the same applies to Dan's code.
0
 
LVL 7

Author Comment

by:SvenIA
ID: 39855725
Thanks Qlemo, still getting that same error like in my previous question....

"The -replace operator only permits 2 elements on the right-hand side, not 3"

Tried to fix it myself, but no luck. I really am a newby :)
0
 
LVL 7

Author Closing Comment

by:SvenIA
ID: 39855733
Thanks guys it works!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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 antispam), the admini…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

670 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