Reputation: 13
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 :)
Upvotes: 1
Views: 1996
Reputation: 13
Not an Answer, just sharing my finished script, do with it what you please!
Completed Working script: edit slight change to output.log I made it look the same as the powershell output, I may change it again if someone has a better idea. edit 2 took out some secret bits I left in on accident ;)
###########################################################
# 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
Upvotes: 0
Reputation: 12443
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 ","
Upvotes: 1