2017-04-02 101 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 $_}} 
    } 
    } 
相关问题