PowerShell 使用变量从 Select-String 输出中删除路径

PowerShell Remove path from Select-String Output using a variable

首先,我要道歉。我不使用 Powershell。这对我来说是一次很棒的学习经历!如果我的代码让你畏缩,我很抱歉:)

我在多个建筑物中工作,有时很难判断用户在哪里,如果能搜索一下用户今天登录的位置会很好。

我在用户登录时使用批处理脚本保存到文件中,以便在提交故障单时进行搜索,但不包括计算机机器甚至建筑物。每天都会自动生成一个新文件。

这是绑定为 GPO 的登录批处理脚本:

 echo User: , %username% , Computer: , %computername% , Date\Time: , %date% %time% >> \path\to\my\saved\file-%date:~-4,4%%date:~-10,2%%date:~-7,2%.csv

这是我用来搜索用户的 powershell 脚本:

Set-StrictMode -Version latest 

$path = '\path\to\my\saved\'
$ext='.csv'
$file='file'
$realFile="$path$file-$(get-date -F 'yyyyMMdd')$ext"
$control = Read-Host -Prompt 'Which User are you looking for?'
$output = $path + "\output.log"
$CSV2String  = Select-String -Pattern $control -Path $realFile

Function getStringMatch 
{     
      $result = Select-String -Pattern $control -Path $realFile -Quiet 
      If ($result -eq $True) 
      { 
        $match = $realFile
        Clear-Host
        Write-Host "Success!  $control found logged in today!" -Foregroundcolor "green"
        ForEach ($line in $CSV2String) {  
        $text = $line -Split ","
        ForEach($string in $text) { 
        Write-Host "$string" 
            } 
        }
        Select-String -Pattern $control -Path $realFile | Out-File $output -Append 
      } ElseIf ($result -eq $False){
        Clear-Host
        Write-Host "Error $control not found logged in, please check the User Name and try again!" -ForegroundColor "red"
        Write-Host "'$control' Not logged in!" -ForegroundColor "yellow"
      } 
} 

getStringMatch

这似乎工作得很好,但是当我使用脚本时,输出对我来说看起来很奇怪,它显示了它所搜索的文件的路径,我不希望这样。我只是想要信息的输出。

Success!  SearchedUser found logged in today!
\path\to\my\saved\file-20160209.csv:1:User:
 SearchedUser
 Computer:
 MyComputer-01
 Date\Time:
 Tue 02/09/2016  9:31:41.93

如何从我的输出中删除“\path\to\my\saved\file-20160209.csv:1:”部分?我也需要它根据一天进行更改,如果可能的话我想使用一个变量。

我玩过 -Replace 但我无法让它完成我想要的任务。

如有任何帮助,我们将不胜感激:)

当你打电话时:

$CSV2String  = Select-String -Pattern $control -Path $realFile

它 returns 一个 MatchInfo 对象数组。目前,您正在对 MatchInfo 的默认输出格式(包含 MatchInfo 属性的串联:Path:LineNumber:Line)调用 Split。每个 MatchInfo 对象都有一个 Line 属性,它只包含匹配的文本行。因此,您可以将代码更改为:

ForEach ($line in $CSV2String) 
{  
    $text = $line.Line -Split ","

不是答案,只是分享我完成的脚本,随心所欲!

完成的工作脚本: edit 对 output.log 的细微改动 我使它看起来与 powershell 输出相同,如果有人有更好的主意,我可能会再次更改它。 edit 2 取出了我不小心留下的一些秘密 ;)

###########################################################  
# AUTHOR  : Jaymes Driver  
# DATE    : 02-08-2016   
# COMMENT : Second part to log on script.
#           Search the "log" file for instance of user 
#           output which machine user is logged into. 
########################################################### 

#ERROR REPORTING ALL 
Set-StrictMode -Version latest 

#Set Path for the reference file
$path = '\Path\To\My\'
$ext = '.csv'
$file = 'file'
$realFile = "$path$file-$(get-date -F 'yyyyMMdd')$ext"
$control = Read-Host -Prompt 'Which User are you looking for?'
$output = $path + "\output.log"

Function getStringMatch 
{     
    #-Quiet returns a True/False Value, we want to make sure the result is true!
    $result = Select-String -Pattern $control -Path $realFile -Quiet 

    If ($result -eq $True) { 
        $match = $realFile
        #Moved CSV2String here so in the future if file check is added, errors are not thrown
        $CSV2String = Select-String -Pattern $control -Path $realFile
        #Clear the screen for easier to read output
        Clear-Host
        Write-Host "Success!  '$control' found logged in today!" -Foregroundcolor "green"
        #for every instance we find do the following
            ForEach ($line in $CSV2String) { 
            # use below code if you need to replace any spaces in the data of csv file 
            #$line = $line.text - Replace " ", ""
            #Split the outputs
            $text = $line.Line -Split ","
            #For every output, write it
                ForEach($string in $text) { 
                Write-Host "$string" 
                } 
            }
        #Write the successful search to the log
        ForEach($string in $text) {
        $string | Out-File $output -Append 
        }
        #If the value is false, then what?
    } ElseIf ($result -eq $False){
        Clear-Host
        #Clear the screen so its easier to read the output
        Write-Host "Error '$control' not found logged in, please check the User Name and try again!" -ForegroundColor "red"
        Write-Host "'$control' Not found to be logged in!" -ForegroundColor "yellow"
      } 
} 


#Do the darn thing!
getStringMatch