Manu El
Manu El

Reputation: 1

Powershell string.split

$version = Get-EventLog 
               -Newest 1 
               -ComputerName $systemnummer 'Symantec Endpoint Protection Client' 
               -Message "*Version*" | Select message 
[string]$version = $version
$version = $version.Split(":")

After getting the Eventlog Entry $version contains the following string: "@{Message=New virus definition file loaded. Version: 150317001.}"

How can i split the String to get only the number "150317001"?

Upvotes: 0

Views: 203

Answers (3)

mjolinor
mjolinor

Reputation: 68243

The problem is that your message string is still a property (Message) of the object, so you need to reference it by that property name:

$version = Get-EventLog 
               -Newest 1 
               -ComputerName $systemnummer 'Symantec Endpoint Protection Client' 
               -Message "*Version*" | Select message 
$version = $version.Message.Split(":")[1]

Or use Select -ExpandProperty to get just the value:

$version = Get-EventLog 
               -Newest 1 
               -ComputerName $systemnummer 'Symantec Endpoint Protection Client' 
               -Message "*Version*" | Select -ExpandProperty message 
$version = $version.Split(":")[1]

Upvotes: 1

arco444
arco444

Reputation: 22821

This should work:

$version = Get-EventLog -Newest 1 -ComputerName $systemnummer 'Symantec Endpoint Protection Client' -Message "*Version*" | Select message 
$ver = $version.message -replace ".*(\d+).*", "`$1"

Upvotes: 0

Kotu
Kotu

Reputation: 1102

Just use regex for this. Here code for your example:

$content = '@{Message=New virus definition file loaded. Version: 150317001.}';
$regex = 'version\s*:\s*(\d+)';
$resultingMatches = [Regex]::Matches($content, $regex, "IgnoreCase")
$version = $resultingMatches[0].Groups[1].Value

Upvotes: 0

Related Questions