Solved

Powershell question

Posted on 2014-02-13
9
443 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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 70

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 70

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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 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