-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathseveral-functions.ps1
162 lines (117 loc) · 4.11 KB
/
several-functions.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# several functions to demonstrate during class
function Get-ProcessTopMemory {
param(
$top=10
)
Get-Process | Sort-Object ws | Select-Object -first $top
}
# tbv auto loading module
function motd {
Write-Host "with great power comes great responsibility"
}
function Invoke-Speak {
param($message='Great programmers never die. They just decompile.')
$a = New-Object -ComObject SAPI.SpVoice
$a.Volume = 100
$a.Rate = 0 # -10 tot 10
$a.Speak($Message)
}
# gebruik motd als input
function Get-Inventory {
# untested
$prefix = 'inventory'
'OperatingSystem', 'QuickFixEngineering', 'Service', 'ScheduledJob', 'Process', 'Share', 'Volume' |
Foreach-Object { Get-CimInstance win32_$_ | Export-Clixml "$prefix-$_.xml" }
Get-ChildItem -Path ${env:ProgramFiles}, ${env:ProgramFiles(x86)} -Recurse |
Export-Clixml "$prefix-programfiles.xml"
Get-ScheduledTask | Export-Clixml "$prefix-ScheduledTasks.xml"
Get-PsDrive | Export-Clixml "$prefix-PsDrive.xml"
}
function Show-SystemUsage {
$os = Get-CimInstance Win32_OperatingSystem
"{0:N2} GB Memory" -f ($os.TotalVisibleMemorySize/1MB)
Get-CimInstance Win32_LogicalDisk -Filter 'DriveType = 3' | ForEach-Object {
"{0:N1} GB {1}" -f ($_.Size/1GB), $_.DeviceID
}
}
function Get-ServiceMemoryUsage {
<#
.SYNOPSIS
Returns memory usage of a service
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$ServiceName
)
$svc = Get-CimInstance win32_service |
Where-Object Name -like $ServiceName |
Where-Object State -eq Running
if(!$svc) { throw "Service $ServiceName not found or not running" }
$proc = Get-Process | Where-Object Id -eq $svc.ProcessId
write-host $proc
$HashParams = @{
Name = $svc.Name
WS = $proc.WS
}
New-Object PSObject -Property $HashParams
}
function Get-IpInfo {
Invoke-RestMethod 'https://ipinfo.io/json'
# output contains GPS coords comma-separated and .-decimal thousands separator
}
function Get-ISS {
Invoke-RestMethod 'http://api.wheretheiss.at/v1/satellites/25544'
# output contains GPS coords in a different format as compared to Get-IpInfo
}
# combine previous two demos to calculate distance between my IP adres and the ISS using Get-GeoDistance
function Get-GeoDistance {
<#
.EXAMPLE
Get-GeoDistance -latitude1 52 -longitude1 4 -latitude2 40 -longitude2 -74
.NOTES
BETA! Not tested yet!
#>
param (
[string]$latitude1,
[string]$longitude1,
[string]$latitude2,
[string]$longitude2,
[double]$Radius = 6371 # Earth's radius in kilometers
)
# Convert degrees to radians
$dLat = ($latitude2 - $latitude1) * [Math]::PI / 180
$dLon = ($longitude2 - $longitude1) * [Math]::PI / 180
$latitude1 = $latitude1 * [Math]::PI / 180
$latitude2 = $latitude2 * [Math]::PI / 180
$a = [Math]::Sin($dLat / 2) * [Math]::Sin($dLat / 2) +
[Math]::Cos($latitude1) * [Math]::Cos($latitude2) *
[Math]::Sin($dLon / 2) * [Math]::Sin($dLon / 2)
$c = 2 * [Math]::Atan2([Math]::Sqrt($a), [Math]::Sqrt(1-$a))
$distance = $Radius * $c
return $distance
}
function MultiPing {
param($computername)
$computername | ForEach-Object {
$r = Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue # -Quiet geeft geen responsetime
if ($r.ResponseTime -ge $10) { write-host $_ -ForegroundColor Red }
elseif ($r.ResponseTime -ge $5) { write-host $_ -ForegroundColor Yellow }
else { write-host $_ }
}
}
function ProcessMonitor {
# a bit more complex
$procs1 = Get-Process
do {
# mspaint
Start-Sleep 1
$procs2 = Get-Process
$diff = Compare-Object $procs1 $procs2 -Property ProcessName
$diff | ForEach-Object {
if ($_.sideIndicator -eq "=>") { Write-Host $_.ProcessName -foregroundcolor green }
if ($_.sideIndicator -eq "<=") { Write-Host $_.ProcessName -foregroundcolor yellow }
}
$procs1 = $procs2
}
}