2017-04-02 102 views
0

所以我有一個基本的程序(令人難以置信的馬車,但我們非常喜歡它),使用共享文件夾,在校的幾個人都可以訪問(路徑已更改爲易用性)。它被設計爲用作消息應用程序,每個用戶都寫入相同的記事本文件,以使用Get-Content和-Wait參數向Poweshell腳本發送消息。我已經使用「/」添加了一些命令,但是我想要一個(即/在線)用戶可以鍵入並查看當前正在使用該程序的所有其他人。如何檢查當前使用PowerShell程序的用戶?

我試圖建立由自己的用戶名每個用戶每x秒更新不同的文本文件,邊擦了此前的紀錄:

while (1){ 
    Clear-Content -Path C:\users\Freddie\Desktop\ConvoOnline.txt 
    Start-Sleep -Milliseconds 5000 
    Add-Content -Path C:\users\Freddie\Desktop\ConvoOnline.txt $env:UserName 
} 

因此,這可以在後來被稱爲:

elseif($_ -match "/online"){Get-Content -Path C:\users\Freddie\Desktop\ConvoOnline.txt} 

但是,這並不工作,也不會同步跟進用戶之間,所以一個用戶要擦去當前用戶,並只將apear爲活動,直到其他用戶的週期溼巾他們的名字。爲了避免XY問題,我想要一個相當簡單的方法(仍然只使用兩個文件最大值)來確定哪些用戶正在使用(因此更新)他們正在運行的Powershell腳本。

整個代碼:

Add-Type -AssemblyName System.speech 
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer 
$speak.Volume = 100 
Write-Host "Type /helpp, save it, then hit backspace and save it again  for a guide and list of commands!" 
Get-Content -Path C:\users\Freddie\Desktop\Convo.txt -Wait | 
%{$_ -replace "^", "$env:UserName "} | 
%{if($_ -match "/cls"){cls} ` 
elseif($_ -match "/online"){Get-Content -Path C:\users\Freddie\Desktop \ConvoOnline.txt} ` 
elseif(($_ -match "/afk") -and ($env:UserName -eq "Freddie")){Write-Host  "$env:UserName has gone afk"} ` 
elseif(($_ -match "/say") -and ($env:UserName -eq "Freddie")) {$speak.Speak($_.Substring(($_.length)-10))} ` 
elseif($_ -match "/whisper"){ 
$array = @($_ -split "\s+") 
if($array[2] -eq "$env:UserName"){ 
Write-Host $array[2] 
} ` 
} ` 
elseif($_ -match "/help"){ 
Write-Host "Help: ` 
1. Press Ctrl+S in Notepad to send your message ` 
2. Make sure you delete it after it's been sent ` 
3. If your message doesn't send properly, just hit backspace and all but  the last letter will be sent ` 
    ` 
COMMANDS: ` 
    ` 
/online - Lists all users currently in the chat ` 
/cls - Clears you screen of all current and previous messages ` 
/whisper [USERNAME] [MESSAGE] - This allows you to send a message  privately to a user" 
} 
else{Write-Host "$_"}} 
# 
# 
# 
# 
#Add a command: elseif($_ -match "/[COMMAND]"){[FUNCTION]} 
# 
#Make it user-specific: elseif($_ -match "/[COMMAND]" -and $envUserName  -eq "[USERNAME]"){[FUNCTION]} 
+1

請考慮提供[MCVE(Minimal,Complete,and Verifiable Example)](http://stackoverflow.com/help/mcve)。 – mklement0

回答

0

你可以用附加的內容和清除前5秒寫入的數據的另一個PS1文件添加時間戳(你可以做到這一點在同一個PS1文件,但另一PS1文件是更好)

修改的用戶的在線更新用部分:

while ($true){ 

    Add-Content -Path d:\ConvoOnline.txt "$($env:UserName);$(get-date)" 
    Start-Sleep -Milliseconds 5000 

} 

另一腳本手錶秒和5秒之前清除內容,所以在線文件總是更新

while($true){ 
    Start-Sleep -Milliseconds 5000 
    $data = get-content -Path D:\ConvoOnline.txt 
    clear-content -Path D:\ConvoOnline.txt 
    if($data){ 
    $data | %{if(!([datetime]($_.split(";")[1]) -lt (get-date).addmilliseconds(-4500))){Add-Content -Path d:\ConvoOnline.txt $_}} 
    } 
    } 
相關問題