Solved

Powershell question

Posted on 2014-02-13
9
435 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
Is Your AD Toolbox Looking More Like a Toybox?

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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