Commit a63b4c41 authored by globa's avatar globa

add eventlogs

parent 3edec9d9
 # Параметры для записи в Event Log
$eventLogName = "Tg bot" # Имя Event Log
$source = "InvetoryTgBot" # Источник
function Write-log() {
param(
[int64]$EventId,
[System.Diagnostics.EventLogEntryType]$EventType,
[string]$Message
)
# Запись события в Event Log
Write-EventLog -LogName $eventLogName -Source $source -EntryType $EventType -EventId $EventId -Message $message
}
\ No newline at end of file
$token = "320805042:AAFsnz44hhAmHvUy4i3fgT9oAA9xk0VbkHc"
$ids = ('177113518','162513986')
\ No newline at end of file
$db = ".\db.sqlite"
. ".\EventLog.ps1"
# Подключение файла с конфигурацией
......@@ -43,23 +44,25 @@ function getUpdates($URL)
$ht["f_name"] = $f_name
$ht["l_name"] = $l_name
$ht["username"] = $username
# Пишем в логи
Write-log -EventId 8 -EventType ([System.Diagnostics.EventLogEntryType]::Information) -Message $($ht| ConvertTo-Json)
return $ht
}
}
function sendMessage($URL, $chat_id, $text)
{
# создаем HashTable, можно объявлять ее и таким способом
$ht = @{
text = $text
# указан способ разметки Markdown
parse_mode = "html"
chat_id = $chat_id
}
# Данные нужно отправлять в формате json
$json = $ht | ConvertTo-Json
# Делаем через Invoke-RestMethod, но никто не запрещает сделать и через Invoke-WebRequest
# Method Post - т.к. отправляем данные, по умолчанию Get
Invoke-RestMethod $URL -Method Post -ContentType 'application/json; charset=utf-8' -Body $json | Out-Null
Write-log -EventId 10 -EventType ([System.Diagnostics.EventLogEntryType]::Information) -Message $json
}
function Get-YearEnding([int]$num) {
......@@ -71,13 +74,14 @@ function Get-YearEnding([int]$num) {
return "лет"
}
}
while($true) # вечный цикл
while($true)
{
$return = getUpdates $URL_get
if($return)
{
if($return.chat_id -in $ids){
$response = Invoke-SqliteQuery -DataBase $db -Query "SELECT * From inventory WHERE inventory_num LIKE '$($return["text"].ToUpper())'"
$response = Invoke-SqliteQuery -DataBase $db -Query "SELECT * From inventory WHERE inventory_num LIKE @inventory_num" -SqlParameters @{"inventory_num" = $($return["text"].ToUpper())}
if($response -ne $null){
foreach($r in $response){
$TimeSpan = [math]::Round(((Get-Date) - [DateTime]::ParseExact($r.date,'dd.MM.yyyy',$null)).Days / 365, 0)
......@@ -99,4 +103,3 @@ while($true) # вечный цикл
}
}
Start-Sleep -s $timeout
\ No newline at end of file
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment