Skip to content

Instantly share code, notes, and snippets.

@miyabis
Last active September 30, 2018 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyabis/c46d8af6fbc8a4d7b6f8385c07862a58 to your computer and use it in GitHub Desktop.
Save miyabis/c46d8af6fbc8a4d7b6f8385c07862a58 to your computer and use it in GitHub Desktop.
Mattermost にPC情報を送信する PowerShell

Chat PC Info

主にディスク情報をチャットに送信する PowerShell スクリプトです。 チャットは Mattermost に送信出来ることを確認しています。 これをベースに Slack などにも送信できるんじゃないかと思われます。

パラメータ 内容
$chatUrl 内向きのウェブフックURL
$chatChannel 送信先のチャンネル
$userName ユーザー名。省略時はホスト名
$iconUrl ユーザーのアイコンURL
$authorIcon Attachments の author_icon
$chatUrl = "https://chat-server/hooks/aaaabbbbccccddddeeeeffffgggg"
$chatChannel = "town-square"
$userName = ""
$iconUrl = "https://www.shareicon.net/data/128x128/2015/09/16/101922_windows_512x512.png"
$authorIcon = "https://www.shareicon.net/data/16x16/2016/03/28/467235_drive_64x64.png"
function ConvertTo-Json20([object]$item) {
add-type -assembly system.web.extensions
$ps_js = new-object system.web.script.serialization.javascriptSerializer
return $ps_js.Serialize($item)
}
$Computer = Get-WmiObject -Class Win32_ComputerSystem
$hostname = $Computer.Name
$OS = Get-WmiObject Win32_OperatingSystem
$Memory = Get-WmiObject -Class Win32_PhysicalMemory | %{ $_.Capacity} | Measure-Object -Sum | %{ ($_.sum) }
$MemoryUsesPer = Get-WmiObject Win32_OperatingSystem | %{(($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)/$_.TotalVisibleMemorySize) * 100}
if ( [string]::IsNullOrEmpty($userName) ){
$userName = $hostname
}
$Disks = Get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }
$rows = @()
foreach ($Disk in $Disks) {
$fields = @()
# ボリューム名
$VolumeName = $Disk.VolumeName
if ( [string]::IsNullOrEmpty($VolumeName) ){
$VolumeName = "Local Disk"
}
# ドライブ名
$DriveName = $Disk.DeviceID
# サイズ
$Size = [long]$Disk.Size
$fields += @{
short = $true
title = "Size"
value = ("{0,6:0.00} GB" -F ($Size / 1GB))
}
# 使用量
$UsedSize = [long]$Disk.Size - [long]$Disk.FreeSpace
$fields += @{
short = $true
title = "Used Size"
value = ("{0,6:0.00} GB" -F ($UsedSize / 1GB))
}
# 空き容量
$fields += @{
short = $true
title = "Free Space"
value = ("{0,6:0.00} GB" -F ($Disk.FreeSpace / 1GB))
}
# 使用率
$DriveUsesPer = (($UsedSize / $Size) * 100)
$fields += @{
short = $true
title = "Drive Uses Per"
value = ("{0,6:0.00} %" -F $DriveUsesPer)
}
$color = "#26A0DA"
if ($DriveUsesPer -gt 90) {
$color = "#DA1917"
}
$attachment = @{
author_name = "{0} ( {1} )" -f $VolumeName, $DriveName
author_icon = $authorIcon
author_link = "file://" + $hostname
color = $color
fields = $fields
}
$rows += $attachment
}
$payload = @{
channel = $chatChannel
username = $userName
icon_url = $iconUrl
text = "{0} {1} {2} Core {3,6:0.00} GB ({4,3:0} % )`r`n{5} - {6}" -f $OS.Caption, $OS.CSDVersion, $Computer.NumberOfLogicalProcessors, ($Memory / 1GB), $MemoryUsesPer, $Computer.Model, $Computer.Manufacturer
attachments = $rows
}
$payload = ConvertTo-Json20 $payload
$bytes = [System.Text.Encoding]::UTF8.GetBytes($payload)
# 自己証明書を使うときはこれがないとエラーになる
#add-type @"
#using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
#}
#"@
#[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$request = [System.Net.WebRequest]::Create($chatUrl)
$request.ContentType = "application/json"
$request.Method = "POST"
$request.ContentLength = $bytes.Length
$requestStream = [System.IO.Stream]$request.GetRequestStream()
$requestStream.write($bytes, 0, $bytes.Length)
$requestStream.Close()
[System.Net.WebResponse]$response = $request.GetResponse()
$response.Close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment