Solved

Powershell string and array manipulation

Posted on 2013-01-20
20
2,240 Views
Last Modified: 2013-01-21
Hi,

I have an issue with manipulating a string I need. I'm doing a script for helpdesk to guide them through modifying DHCP reservations. I would like to create an error check so they don't remove an IP that should not be removed.

I'm using netsh called from powershell script. What I can't figure out is how to manipulate strings and arrays to get check values.

If I do

$checkarray = Invoke-Expression "cmd /c netsh dhcp server \\s-echa-vpsv-16 scope 10.1.1.0 show reservedip"

When I check the output I see the following

$checkarray

Changed the current scope context to 10.1.1.0 scope.

===============================================================
  Reservation Address -    Unique ID                          
===============================================================

    10.1.1.218        -    33-44-55-66-77-88-99-
    10.1.1.210        -    11-22-33-44-55-66-
    10.1.1.240        -    77-66-55-44-33-22-
    10.1.1.220        -    22-33-44-55-66-77-


No of ReservedIPs : 4 in the Scope : 10.1.1.0.

Command completed successfully.

Now from this output, I need to find the IP that has been populated to $reservedip and check that the $oldmac variable matches the mac given for the IP in this list.

If I do

$checkarray | Select-String $reservedip

I get

10.1.1.218        -    33-44-55-66-77-88-99-

How do I split this in two sets that I can compare to the data input by user>

if I do
$doublecheck = $checkarray | Select-String $reservedip
$chechip = $doublecheck.split(" ")

I get

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does
n't contain a method named 'split'.
At line:1 char:30
+ $chechip = $doublecheck.split <<<< (" ")
    + CategoryInfo          : InvalidOperation: (split:String) [], RuntimeExce
   ption
    + FullyQualifiedErrorId : MethodNotFound
0
Comment
Question by:SMCWindows
  • 9
  • 5
  • 5
  • +1
20 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38800139
please try Split instead of split.
0
 

Author Comment

by:SMCWindows
ID: 38800180
same result
0
 
LVL 2

Expert Comment

by:Crusadin
ID: 38800211
$checkarray = Invoke-Expression "cmd /c netsh dhcp server \\s-echa-vpsv-16 scope 10.1.1.0"
$ReserveAddress = ($checkarray | Select-String $reservedip).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[0]
$UniqueID = (($checkarray | Select-String $reservedip).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)) | ?{$_.Length -gt 8}
$ReserveAddress
$UniqueID
0
 
LVL 5

Expert Comment

by:coraxal
ID: 38800215
You could use a regular expression to extract the IP address....like this:
$regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’
$chechip = $doublecheck | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }

Open in new window

0
 

Author Comment

by:SMCWindows
ID: 38800223
crusadin I get

Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] does
n't contain a method named 'Split'.
At line:1 char:66
+ $ReserveAddress = ($checkarray | Select-String $reservedip).Split <<<< (" ",[
System.StringSplitOptions]::RemoveEmptyEntries)[0]
    + CategoryInfo          : InvalidOperation: (Split:String) [], RuntimeExce
   ption
    + FullyQualifiedErrorId : MethodNotFound

on the second line.
0
 

Author Comment

by:SMCWindows
ID: 38800231
ty coralx, I was able to extract the IP with the regular expression. Now I need to get the MAC too. Does not have to be in 00-00-00-00-00-00 format.
0
 
LVL 5

Accepted Solution

by:
coraxal earned 500 total points
ID: 38800253
ok...I'd use two regular expressions then...one for the IP and one for the MAC...like this

 
$regexIP = "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
$regexMAC = "((?:(\d{1,2}|[a-fA-F]{1,2}){2})(?::|-*)){6}"

$IP = $doublecheck | select-string -Pattern $regexIP -AllMatches | % { $_.Matches } | % { $_.Value }
$MAC = $doublecheck | select-string -Pattern $regexMAC -AllMatches | % { $_.Matches } | % { $_.Value }

Open in new window

0
 
LVL 2

Expert Comment

by:Crusadin
ID: 38800256
$checkarray = Invoke-Expression "cmd /c netsh dhcp server \\s-echa-vpsv-16 scope 10.1.1.0" | ?
$ReserveAddress = ([String]$checkarray | Select-String $reservedip).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[0]
$UniqueID = (([String]$checkarray | Select-String $reservedip).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)) | ?{$_.Length -gt 8}
$ReserveAddress
$UniqueID 

Open in new window

0
 

Author Comment

by:SMCWindows
ID: 38800295
$MAC gives out the IP and not the mac
0
 
LVL 2

Expert Comment

by:Crusadin
ID: 38800306
Can you give my last post a try?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Expert Comment

by:coraxal
ID: 38800307
Humm...there are two regex patterns...make sure you use $regexIP for IP address and $regexMAC for MAC address.  Works on my end with the string you provided.
0
 
LVL 2

Expert Comment

by:Crusadin
ID: 38800318
last one had a syntax error..

$checkarray = Invoke-Expression "cmd /c netsh dhcp server \\s-echa-vpsv-16 scope 10.1.1.0"
$ReserveAddress = ([String]$checkarray | Select-String $reservedip).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[0]
$UniqueID = (([String]$checkarray | Select-String $reservedip).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)) | ?{$_.Length -gt 8}
$ReserveAddress
$UniqueID 

Open in new window

0
 

Author Comment

by:SMCWindows
ID: 38800320
crusadin I tried it. The ? at the end of line one prompts an entry in ISE. What should I put in. And line 2 still comes up with the same error on split.

coraxal, I copied your expressions as is
0
 
LVL 2

Expert Comment

by:Crusadin
ID: 38800354
Ok new approach,

$Array = New-Object System.Collections.ArrayList
$checkarray = Invoke-Expression "cmd /c netsh dhcp server \\s-echa-vpsv-16 scope 10.1.1.0 show reservedip"
$checkarray | ?{$_ -like $reservedip} | %{[Void]$Array.Add([String]$_)}
Foreach ($Object in $Array)
			{
				$ReserveAddress = ([String]$Object).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)[0]
				$UniqueID = ((([String]$Object).Split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)) | ?{$_.Length -gt 8})[1]
				$ReserveAddress
				$UniqueID
			}

Open in new window

0
 
LVL 5

Expert Comment

by:coraxal
ID: 38800362
If you're getting the IP address, then the pattern being used is the IP address pattern....the two patterns I provided are very different...check out the output on my side
string.png
0
 

Author Comment

by:SMCWindows
ID: 38800381
crusadin no error messages but no results either
0
 

Author Comment

by:SMCWindows
ID: 38800396
coralx when i insert the data to $doublecheck like you did it works nicely, but I'm getting the data selecting string from $checkarray
0
 
LVL 5

Assisted Solution

by:coraxal
coraxal earned 500 total points
ID: 38800410
I got that....so sounds like the result is not a string. What's the type of $doublecheck? If not string, you can do this:

[string]$doublecheck = $checkarray | Select-String $reservedip
0
 

Author Comment

by:SMCWindows
ID: 38800436
Thanks crusadin works perfect!
0
 

Author Closing Comment

by:SMCWindows
ID: 38800438
Thanks to both experts for a speedy solution. Coraxal got it in the end
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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 …
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

708 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

17 Experts available now in Live!

Get 1:1 Help Now