简体   繁体   English

从Powershell输出Search-String中提取单词

[英]Extract word from powershell output Search-String

Hi All I'm trying to extract a word from a text output. 大家好,我想从文本输出中提取一个单词。 It should be pretty easy but I've already spent so much time on it. 这应该很容易,但是我已经花了很多时间了。 Right now I can extract the line but not just the word. 现在,我可以提取一行,而不仅仅是单词。

For example 例如

w32tm /query /status | Select-String -pattern "CMOS"

outputs the line "Source: Local CMOS Clock" 输出行“来源:本地CMOS时钟”

I only want to extract "Local CMOS Clock" 我只想提取“本地CMOS时钟”

$var1=w32tm /query /status | Select-String -pattern "CMOS"

$var2=($var1 -split ':')[1] | Out-String

I was able to come up with the above it seems to work I'm not sure if there's a better way, I'm trying to evaluate it through a true/false seem to always pass as true though For example 我能够提出上述建议,但似乎行得通。我不确定是否有更好的方法,我试图通过对/错进行评估,尽管看起来总是正确的。例如

if($var2 = "Local CMOS Clock"){
Write-Output "True";
}Else{
Write-Output "False";
}

Always true: even when the condition is wrong 始终为真:即使条件错误

thanks in advance. 提前致谢。

I'm not entirely sure of your motives, but here's a cleaner way to get to the answer you're looking for: 我不确定您的动机,但是这是获得所需答案的一种更干净的方法:

Build a PSObject containing the output 构建包含输出的PSObject

The PSObject will contain the output of w32tm. PSObject将包含w32tm的输出。 The code works by piping the command output through a loop, at the beginning we make a HashTable and then this is used to build a PowerShell object which is easier to manipulate: 该代码通过循环输出命令输出来工作,首先我们创建一个HashTable,然后将其用于构建易于操作的PowerShell对象:

# Pipe the w32tm command through a foreach
# Build a hashtable object containing the keys
# $_ represents each entry in the command output, which is then split by ':'
$w32_obj = w32tm /query /status | ForEach-Object -Begin {$w32_dict = @{}} -Process {
    # Ignore blank entries
    if ($_ -ne '') {
        $fields = $_ -split ': '
        # This part sets the elements of the w32_dict. 
        # Some rows contain more than one colon, 
        # so we combine all (except 0) the split output strings together using 'join'
        $w32_dict[$fields[0]] = $($fields[1..$($fields.Count)] -join ':').Trim()
    }
} -End {New-Object psobject -Property $w32_dict}

View the PSObject 查看PSObject

Simply run this to display the new PSObject that has been created: 只需运行以下命令即可显示已创建的新PSObject:

$w32_obj

Now check the 'Source' 现在检查“来源”

Now we can ask for the 'Source' object from $w32_obj by using the dot-notation: $w32_obj.Source : 现在,我们可以从要求“源”对象$w32_obj :使用点符号$w32_obj.Source

if($w32_obj.Source -eq "Local CMOS Clock"){
Write-Output "True";
}Else{
Write-Output "False";
}

Further reading 进一步阅读

This shows the conversion from HashTable to PSobject and vice-versa 这显示了从HashTable到PSobject的转换,反之亦然

PSCustomObject to Hashtable PSCustomObject到Hashtable

here's yet another way to get the False/True from w32tm. 这是从w32tm获取错误/真实的另一种方法。 my system does not have "cmos" in the output, so i use 'system clock', but the idea will work for your situation. 我的系统在输出中没有“ cmos”,因此我使用“系统时钟”,但该想法将适合您的情况。

[bool]((w32tm /query /status) -match 'system clock')

the above returns a $True on my system. 上面的代码在我的系统上返回了$True that seems a tad more direct than the method you used. 这似乎比您使用的方法更直接。 [ grin ] [ 咧嘴 ]

take care, 照顾自己,
lee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM