Search code examples
powershellbatch-filelogin-scriptuser-trackingselect-string

PowerShell Remove path from Select-String Output using a variable


First of all, I want to apologize. I do not work with Powershell. This was a great learning experience for me! If my code makes you cringe, I am sorry :)

I work with multiple buildings, Sometimes it is difficult to tell where a user is and it would be nice to search to see where the user has logged in today.

I am using a batch script when users log in to save to a file so that I can search it when a trouble ticket is submitted, but the computer machine or even the building is not included. A new file will be generated automatically on each day.

This is the logon batch script tied as a 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

This is the powershell script I am using to search for the user:

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

This seems to work great, however when I use the script the output looks weird to me, it displays the path to the file it has searched, I don't want that. I just want the output of the information.

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

How do I remove the "\path\to\my\saved\file-20160209.csv:1:" portion from my output? I need it to change based on the day as well, I would like to use a variable if possible.

I played with the -Replace but I was not able to get it to accomplish the task I wanted.

Any assistance would be appreciated :)


Solution

  • When you call:

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

    it returns an array of MatchInfo objects. Currently, you are calling Split on the MatchInfo's default output format (which contains the concatenation of the MatchInfo's properties: Path:LineNumber:Line). Each MatchInfo object has a Line property that contains only the line of text that was matched. So you can change your code to something like:

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