首先,我要道歉。我不与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,但我无法完成我想要的任务。
任何帮助将不胜感激:)
答案 0 :(得分:1)
致电时:
$CSV2String = Select-String -Pattern $control -Path $realFile
它返回一个MatchInfo对象数组。目前,您正在以MatchInfo的默认输出格式(包含MatchInfo属性的串联:Path:LineNumber:Line)调用Split。每个MatchInfo对象都有一个Line属性,该属性仅包含匹配的文本行。因此,您可以将代码更改为:
ForEach ($line in $CSV2String)
{
$text = $line.Line -Split ","
答案 1 :(得分:0)
不是答案,只是分享我完成的脚本,随心所欲地做它!
完成工作脚本: 编辑略微更改为output.log我使它看起来与powershell输出相同,如果有人有更好的想法,我可能会再次更改它。 编辑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