Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell string and array manipulation

Posted on 2013-01-20
20
Medium Priority
?
2,441 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 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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
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…

824 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