1

首先,我想道歉。我不使用Powershell。這對我來說是一個很好的學習經歷!如果我的代碼讓你感到沮喪,我很抱歉:)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 

如何刪除 「\路徑\爲\我\保存\文件20160209.csv:1:」 從我的輸出部分?我需要它根據當天的情況進行更改,如果可能的話,我想使用一個變量。

我打的 - 更換,但我無法得到它來完成我想要的任務。

任何援助,將不勝感激:)

+0

我想包括完成腳本:( –

回答

1

當你撥打:

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

返回MatchInfo對象的數組。目前,您正在呼籲MatchInfo的默認輸出格式斯普利特(其中包含MatchInfo屬性的串聯:路徑:LineNumber上:行)。每個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