Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell string and array manipulation

Posted on 2013-01-20
20
Medium Priority
?
2,402 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
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…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…

730 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