-1

I have a string like the one below and I need to parse it out using PowerShell. I only want to keep the string up to the "EntityPath" and take out everything after the entity path:

Endpoint=sb://abcdefg.servicebus.windows.net/;SharedAccessKeyName=listenkey_1137;SharedAccessKey=W2c26OiBwae9f/vgPcJWgtD709oTTJu1VlB8i4OkqUc=;EntityPath=listen_1137
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Amy
  • 185
  • 15
  • Possible duplicate of [Powershell remove text after first instance of special character](http://stackoverflow.com/questions/24044226/powershell-remove-text-after-first-instance-of-special-character) – Moshe perez Apr 13 '17 at 01:20

4 Answers4

2

try this :

$mystring="Endpoint=sb://abcdefg.servicebus.windows.net/;SharedAccessKeyName=listenkey_1137;SharedAccessKey=W2c26OiBwae9f/vgPcJWgtD709oTTJu1VlB8i4OkqUc=;EntityPath=listen_1137"
$NameValue=$mystring -replace ";", "`n" | ConvertFrom-StringData
$NameValue.EntityPath
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • 1
    This is an awesome solution! I've been doing Powershell for some time now but haven't seen a reason to try the `ConvertFrom-StringData` method before. Thanks for an simple elegant way to get the value! – Arian Kulp Dec 15 '21 at 17:22
0
$testStr = "Endpoint=sb://abcdefg.servicebus.windows.net/;SharedAccessKeyName=listenkey_1137;SharedAccessKey=W2c26OiBwae9f/vgPcJWgtD709oTTJu1VlB8i4OkqUc=;EntityPath=listen_1137"

$parts = $testStr -split "EntityPath"

Write-Host $parts[0]

This might work

0

I'm not certain what you are asking now but it's a second question I suppose. I'll attempt to understand your question as it is somewhat broken English.

function Invoke-MyWork($testStr)
{
     $parts = $testStr -split "EntityPath"
     $parts[0]
}
$originalValue = "Endpoint=sb://abcdefg.servicebus.windows.net/;SharedAccessKeyName=listenkey_1137;SharedAccessKey=W2c26OiBwae9f/vgPcJWgtD709oTTJu1VlB8i4OkqUc=;EntityPath=listen_1137"

$upToEntityPath = Invoke-MyWork $originalValue


Write-Host "UpToEntityPath: $($upToEntityPath)"
Write-Host "OriginalValue: $($originalValue)"
0

Here a solution using regex.

$ep = "Endpoint=sb://abcdefg.servicebus.windows.net/;SharedAccessKeyName=listenkey_1137;SharedAccessKey=W2c26OiBwae9f/vgPcJWgtD709oTTJu1VlB8i4OkqUc=;EntityPath=listen_1137"
[regex]::Match($ep, 'EntityPath=([^;|\s]+)').Groups[1].Value # output: listen_1137
[regex]::Match($ep, '(EntityPath=[^;|\s]+)').Groups[1].Value # output: EntityPath=listen_1137
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172