Solved

Powershell question

Posted on 2014-02-13
9
424 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 34

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
 
LVL 34

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 68

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 68

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now