Solved

Powershell string and array manipulation

Posted on 2013-01-20
20
2,288 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

679 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