PowerShell/scripts/enter-chat.ps1

93 lines
1.9 KiB
PowerShell
Raw Normal View History

2023-10-31 12:20:46 +01:00
<#
2021-08-29 17:50:03 +02:00
.SYNOPSIS
2021-09-24 17:19:49 +02:00
Enters a chat using a common network shared file
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script enters a chat using a common network shared file.
2021-08-29 17:50:03 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./enter-chat.ps1
2021-08-29 17:50:03 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-08-29 17:50:03 +02:00
#>
# make sure you adjust this path
2021-04-12 18:58:18 +02:00
# it must point to a network share where you have read and write permissions
$ServerShare = "\\myserver\chathome"
function Enter-Chat
{
param
(
[Parameter(Mandatory)]
[string]
$ChatChannelName,
[string]
$Name = $env:USERNAME,
[Switch]
$ShowOldPosts,
$HomeShare = $ServerShare
)
if ($ShowOldPosts)
{
$Option = ''
}
else
{
$Option = '-Tail 0'
}
$Path = Join-Path -Path $HomeShare -ChildPath "$ChatChannelName.txt"
$exists = Test-Path -Path $Path
if ($exists -eq $false)
{
$null = New-Item -Path $Path -Force -ItemType File
}
$process = Start-Process -FilePath powershell -ArgumentList "-noprofile -windowstyle hidden -command Get-COntent -Path '$Path' $Option -Wait | Out-GridView -Title 'Chat: [$ChatChannelName]'" -PassThru
Write-Host "To exit, enter: quit"
"[$Name entered the chat]" | Add-Content -Path $Path
do
{
Write-Host "[$ChatChannelName]: " -ForegroundColor Green -NoNewline
$inputText = Read-Host
$isStopCommand = 'quit','exit','stop','leave' -contains $inputText
if ($isStopCommand -eq $false)
{
"[$Name] $inputText" | Add-Content -Path $Path
}
} until ($isStopCommand -eq $true)
"[$Name left the chat]" | Add-Content -Path $Path
$process | Stop-Process
}
function Get-ChatChannel
{
param
(
$HomeShare = $ServerShare
)
Get-ChildItem -Path $HomeShare -Filter *.txt -File |
ForEach-Object {
[PSCustomObject]@{
ChannelName = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
LastActive = $_.LastWriteTime
Started = $_.CreationTime
}
}
}