-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.ps1
71 lines (63 loc) · 2.57 KB
/
init.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# init.ps1 This has been turned into a function
#
function Send-Message($stage="Init", $state="Unknown", $duration="N/A", $comment="N/A")
{
# Define session variables
$hostname = $env:COMPUTERNAME
$username = $env:USERNAME
$domain = $env:USERDOMAIN
$timestamp = Get-Date -Format "yyyy-MM-ddTHH:mm:ssK"
$init_message = "[{0}] Hostname: {1}, Username: {2}\{3}, Admin: {4}, Elevated: {5}, Stage: {6}, State: {7}, Duration: {8}, Comment: {9}"
#$init_message_json = '{{"timestamp": "{0}, "hostname": "{1}", "username": "{2}\{3}", "admin": {4}, "elevated": {5}, "stage": "{6}", "state": "{7}", "duration": "{8}", "comment": "{9}"}}'
# Determine if this is an admin user, and if this session is elevated
$admin_user = ($username -split "-")[1]
if ($admin_user -ne $null)
{
$is_admin = $true
$is_elevated = [bool]([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")
} else
{
$is_admin = $false
$is_elevated = [bool]([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")
}
# Debug output
#write-host('')
$message = ($init_message -f $timestamp, $hostname, $domain, $username, $is_admin, $is_elevated, $stage, $state, $duration, $comment)
#write-host('')
#$json_message = ($init_message_json -f $timestamp, $hostname, $domain, $username, $is_admin, $is_elevated, $stage, $state, $duration, $comment | convertto-json | convertfrom-json)
write-host('')
# Create TCP socket to logging server and send $tcp_message
$remoteHost="172.25.0.181"
$port=8443
try
{
Write-Host "Connecting to $remoteHost on port $port ... " -NoNewLine
try
{
$socket = New-Object System.Net.Sockets.TcpClient( $remoteHost, $port )
Write-Host -ForegroundColor Green "OK"
}
catch
{
Write-Host -ForegroundColor Red "failed"
exit -1
}
$stream = $socket.GetStream( )
$writer = New-Object System.IO.StreamWriter( $stream )
# $buffer = New-Object System.Byte[] 1024
# $encoding = New-Object System.Text.AsciiEncoding
# $tcp_message = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($json_message))
start-sleep -m 500
write-host("Sending message: {0}" -f $message)
$writer.WriteLine( $message )
#write-host("Sending message: {0}" -f $json_message)
#$writer.WriteLine( $json_message )
$writer.Flush( )
}
finally
{
if( $writer ) { $writer.Close( ) }
if( $stream ) { $stream.Close( ) }
}
}