Home > Mobile >  String Matching and finding 1 of similar values
String Matching and finding 1 of similar values

Time:01-19

using a program called GAM I run a command like this.

$sharedID = (& gam user $EMAIL show teamdrives matchname $SharedName | Where-Object {$_ -match "Shared Drive ID: " })

The problem is that I have a few Shared Drives with similar names. I am getting a return of two values that are large strings with lots of information.

Example Names. I would only need house.

House
HouseFire
House Boat

I need the shared drive ID that equals the name. Instead I am getting

Shared Drive X, ID y
Shared Drive A, ID F

I can't modify the GAM command so whatever the answer is. The answer needs to happen in the pipeline or after it on new line(s) of code.

I tried

Where-Object {$_ -match $SName -and $_ -match "Shared Drive ID: " }

This one above gave me nothing. I get that.

and

Where-Object {$_ -match $SName }

the above is still giving me 2 answers.

I know how to get the ID out of the string if I get down to 1 string.

CodePudding user response:

Try following Regex :

$drives = @("Shared Drive X, ID y","Shared Drive A, ID F")
$pattern = "^Shared Drive\s(?<drive>[^,] ), ID (?<id>.*)"
foreach($drive in $drives)
{
   $drive -match $pattern | Out-Null
   Write-Host "drive = "$Matches.drive "id = " $Matches.id
}
  • Related