背景
此脚本,用于检测哪些VPN账户正在登陆连接;
检测频率:1分钟一次
实现
function feishu_alert($info){
$time = (Get-Date).ToShortTimeString()
$par = @{
"msg_type" = "text";
"title" = "VPN在线账号";
"content" = @{
"text" = $info
}
}
$parjson = $par | ConvertTo-Json
#echo $parjson
# 指定hook
$ddhook = "xxxxx"
# 使Json支持中文
$PostData = [System.Text.Encoding]::UTF8.GetBytes($parjson)
$Header = @{"Content-Type" = "application/json;charset=utf-8"}
# UseBasicParsing对URI只进行简单解析,可提高效率
Invoke-WebRequest -Uri $ddhook -Method Post -Body $PostData -Headers $Header -UseBasicParsing
}
$CD=@{Name="持续连接(分钟)";Expression={[math]::Floor(($_.ConnectionDuration)/60)}}
function Get-Active-VPNUser(){
Get-Date
Write-Output "当前共连接VPN用户为 $TotalUser 个"
Get-RemoteAccessConnectionStatistics |
Format-Table UserName, ClientIPAddress, TunnelType, ConnectionStartTime, $CD -AutoSize -Wrap
}
while(1){
$TotalUser=(Get-RemoteAccessConnectionStatistics | Measure-Object).count
# $TotalUser=$((Get-RemoteAccessConnectionStatisticsSummary).TotalConnections)
$date=Get-Date -Format "yyyy年MM月dd日"
Get-Active-VPNUser >> "D:\Logs\VPN\$($date)_Active_VPN_User_10.2.234.17.csv"
#feishu_alert(Get-Active-VPNUser | Out-String)
sleep 60
}