0

我只是想写一个函数,它接受一个字符串并隔离一个子字符串并返回它。然后将该值复制到 csv 文件中。

我的功能如下所示:

Function FileName ($msg)
{
$msg -match ', (.*) owned'
$result=$matches[1]
return $result
}

例如,我希望结果包含“New Text Document.txt - Notepad”,我调用:

FileName "Document 3, New Text Document.txt - Notepad owned by SYSTEM on \\COMPUTERNAME was printed on {736660DE-4F30-4949-A1B1-68701735DAA8} through port {736660DE-4F30-4949-A1B1-68701735DAA8}.  Size in bytes: 127183. Pages printed: 1. No user action is required."

但是当我将它导出到 csv 文件时,它显示为“True New Text Document.txt - Notepad”。如何删除“真”?

4

1 回答 1

2

-match是一个布尔运算符,根据是否找到匹配项返回 $true 或 $false。如果您没有捕获在变量中执行运算符或重定向它的结果,它将成为函数输出的一部分。通常你会像这样使用它:

function FileName ($msg)
{
    if ($msg -match ', (.*) owned')
    {
        $matches[1]
    }
    else { .. figure out what to do here .. }
}
于 2013-01-03T00:21:09.883 回答