function Send-TelegramTextMessage { [CmdletBinding()] Param ( [Parameter(Mandatory = $true, HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$BotToken, #you could set a token right here if you wanted [Parameter(Mandatory = $true, HelpMessage = '-#########')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$ChatID, #you could set a Chat ID right here if you wanted [Parameter(Mandatory = $true, HelpMessage = 'Text of the message to be sent')] [ValidateNotNull()] [ValidateNotNullOrEmpty()] [string]$Message, [Parameter(Mandatory = $false, HelpMessage = 'HTML vs Markdown for message formatting')] [ValidateSet("Markdown", "HTML")] [string]$ParseMode = "Markdown", #set to Markdown by default [Parameter(Mandatory = $false, HelpMessage = 'Disables link previews')] [bool]$Preview = $false, #set to false by default [Parameter(Mandatory = $false, HelpMessage = 'Sends the message silently')] [bool]$Notification = $false #set to false by default ) #------------------------------------------------------------------------ $results = $true #assume the best #------------------------------------------------------------------------ $payload = @{ "chat_id" = $ChatID; "text" = $Message "parse_mode" = $ParseMode; "disable_web_page_preview" = $Preview; "disable_notification" = $Notification }#payload #------------------------------------------------------------------------ try { Write-Verbose -Message "Sending message..." $eval = Invoke-RestMethod ` -Uri ("https://api.telegram.org/bot{0}/sendMessage" -f $BotToken) ` -Method post ` -ContentType "application/json" ` -Body (ConvertTo-Json -Compress -InputObject $payload) ` -ErrorAction Stop if (!($eval.ok -eq "True")) { Write-Warning -Message "Message did not send successfully" $results = $false }#if_StatusDescription }#try_messageSend catch { Write-Warning "An error was encountered sending the Telegram message:" Write-Error $_ $results = $false }#catch_messageSend return $results #------------------------------------------------------------------------ }#function_Send-TelegramTextMessage ## Esto tiene que estar habilitado para que funcione en powershell 4 ## [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" $partitions= Get-WmiObject -Class Win32_LogicalDisk -Filter 'DriveType = 3' |Select-Object PSComputerName, Caption,@{N='Capacity_GB'; E={[math]::Round(($_.Size / 1GB), 2)}},@{N='FreeSpace_GB'; E={[math]::Round(($_.FreeSpace / 1GB), 2)}},@{N='PercentUsed'; E={[math]::Round(((($_.Size - $_.FreeSpace) / $_.Size) * 100), 2) }},@{N='PercentFree'; E={[math]::Round((($_.FreeSpace / $_.Size) * 100), 2) }} ## Ingreso a cada disco del sistema ## foreach($z in $partitions) { #Write-Host "Particion : $($z.Caption)" ; #Write-Host "Capacidad Total : $($z.Capacity_GB) GB" ; #Write-Host "Espacio Libre : $($z.FreeSpace_GB) GB" ; #Write-Host "Porcentaje Usado : $($z.PercentUsed) %" ; #Write-Host "Porcentaje Libre : $($z.PercentFree) %" ; #Write-Host ""; ## Nombre del Equipo ## $mira= get-content env:computername ## IPs de la maquina ## $ip= (Get-WmiObject Win32_NetworkAdapterConfiguration | ? {$_.IPAddress -ne $null}).ipaddress ## Tamaño y porcentaje real de cada disco ## $porci= $z.PercentFree $size= $z.FreeSpace_GB ## Tamaño y porcentaje, en el que quiero que envie el alerta. ## $Porciento= "15" $tamaño= "15" ## Token del Bot Telegram y ChatID ## $bot= "1185443058:AAE-gfS2UxxnSDt-xI-LdaCvHXf-c56M2tk" $chat= "-420922357" ## test de información que arroja ## #Write-Host $porci #Write-Host $size #Write-Host $porci #Write-Host $mira if ( $porci -le $Porciento ) { if ( $size -le $tamaño ) { Send-TelegramTextMessage -BotToken $bot -ChatID $chat -Message "Alerta el espacio en disco de $mira $ip es $porci % $size GB " } } }