Solved

Powershell string and array manipulation

Posted on 2013-01-20
20
2,348 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
[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
  • 9
  • 5
  • 5
  • +1
20 Comments
 
LVL 143

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
Suggested Courses

626 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