Solved

Powershell question

Posted on 2014-02-13
9
438 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 75 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 75 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 69

Accepted Solution

by:
Qlemo earned 350 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 69

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

Independent Software Vendors: 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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
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…

733 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