首先,我想道歉。我不使用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:」 從我的輸出部分?我需要它根據當天的情況進行更改,如果可能的話,我想使用一個變量。
我打的 - 更換,但我無法得到它來完成我想要的任務。
任何援助,將不勝感激:)
我想包括完成腳本:( –