Powershell string.split

时间:2015-03-19 12:09:42

标签: powershell split

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

获取Eventlog Entry $版本后,包含以下字符串: "@{Message=New virus definition file loaded. Version: 150317001.}"

如何分割字符串以仅获取数字" 150317001"?

3 个答案:

答案 0 :(得分:1)

问题是您的消息字符串仍然是对象的属性(Message),因此您需要通过该属性名称引用它:

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

或使用Select -ExpandProperty获取值:

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

答案 1 :(得分:0)

这应该有效:

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

答案 2 :(得分:0)

只需使用正则表达式。这里是您的示例代码:

$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