0

I´m struggling in a simple report script. for example

$report.FullFormattedMessage = "This is the deployment test for Servername for Stackoverflow in datacenter onTV"
$report.FullFormattedMessage.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                        
-------- -------- ----                                     --------                                                                                                        
True     True     String                                   System.Object  

Now I want to pick some certians words out like...

$dc = should be the 'onTV'
$srv = should be the 'Servername'
$srv = $report.FullFormattedMessage.contains... or -match ?? something like this?

The trick with .split()[] is not working for me because the $report looks different some times. How could I do that?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
vespavbb
  • 27
  • 2
  • 9

2 Answers2

0

oh ok I found a solution, I dont know if this is best... but I show you:

$lines = $report.FullFormattedMessage.split() 

   $dc= ForEach ($line in $lines){
    
    $line | Where-Object {$_ -match "onTV*"} 
    }
    
    $srv= ForEach ($line in $lines){
    
    $line | Where-Object {$_ -match "Server*"} 
    }
vespavbb
  • 27
  • 2
  • 9
0

I'd probably do something like

$report.FullFormattedMessage = "This is the deployment test for Servername for Stackoverflow in datacenter onTV"
$dc  = if ($report.FullFormattedMessage -match '\b(onTV)\b') { $Matches[1] }        # see details 1
$srv = if ($report.FullFormattedMessage -match '\b(Server[\w]*)') { $Matches[1] }   # see details 2

# $dc   --> "onTV"
# $srv  --> "Servername"

Regex details 1

\b             Assert position at a word boundary
(              Match the regular expression below and capture its match into backreference number 1
   onTV        Match the characters “onTV” literally
)             
\b             Assert position at a word boundary

Regex details 2

\b             Assert position at a word boundary
(              Match the regular expression below and capture its match into backreference number 1
   Server      Match the characters “Server” literally
   [\w]        Match a single character that is a “word character” (letters, digits, etc.)
      *        Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
Theo
  • 57,719
  • 8
  • 24
  • 41