-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.ps1
391 lines (332 loc) · 11.8 KB
/
profile.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# My profile functions
Function Test-IsAdmin
{ <#
.Synopsis
Tests if the user is an administrator
.Description
Returns true if a user is an administrator, false if the user is not an administrator
.Example
Test-IsAdmin
#>
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = New-Object Security.Principal.WindowsPrincipal $identity
$principal.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
######----------------- Loaded at runtime ------------------#####
if(-not (Test-IsAdmin)){
$Host.ui.RawUI.WindowTitle = "Regular PowerShell"
$Host.UI.RawUI.ForegroundColor = "Green"
}else{
$Host.ui.RawUI.WindowTitle = "SUPER POWERSHELL"
$Host.UI.RawUI.ForegroundColor = "White"
$Host.UI.RawUI.BackgroundColor = "Black"
}
Set-Location $env:USERPROFILE
#############################
######------------------- Functions -------------------######
function Sign {
<#
.Synopsis
Signs powershell script with my cert
.Parameter scriptPath
Path to script to sign
.Example
sign C:\PathTo\Script.ps1
#>
Param($scriptPath)
$cert=(dir cert:currentuser\my\ -CodeSigningCert)
$time="http://timestamp.comodoca.com/authenticode"
Set-AuthenticodeSignature $scriptPath $cert -TimestampServer $time
}
function Get-Excuse {
If ( !( Get-Variable -Scope Global -Name Excuses -ErrorAction SilentlyContinue ) ) {
$Global:Excuses = (Invoke-WebRequest http://pages.cs.wisc.edu/~ballard/bofh/excuses).Content.Split([Environment]::NewLine)
}
Get-Random $Global:Excuses
}
Set-Alias ge Get-Excuse
function Remove-Excuses {
Remove-Variable -Scope Global -Name Excuses
}
function Get-DNS {
<#
.Synopsis
Gets the DNS entries for the specified computer from all 3 DNS servers and shows which is currently online
.Parameter computerName
Specifies the computer name
.Parameter DCServers
Specifies a list of Domain controller servers
.Example
Get-DNS dmnatedesk
#>
Param(
[Parameter(Mandatory = $true, Position = 1)]
[string]$computerName,
[Parameter(Mandatory = $true, Position = 2)]
[string[]]$DCServers
)
$Results = @()
foreach ($DC in $DCServers) {
$result = Resolve-DnsName -Name $ComputerName -Server $DC -Type A
$Results += $result
}
foreach ($Result in $Results) {
if (Test-Connection -ComputerName $Result.IPAddress -Count 1 -Quiet) {
write-host $Result.IPAddress -ForegroundColor Green
}
else {
write-host $Result.IPAddress -ForegroundColor Red
}
}
}
function LL {
<#
.Synopsis
Replaces the color of certain files in LL call
.Parameter directory
Specifies the directory currently in
.Example
ll
#>
param ($dir = ".", $all = $false)
$origFg = $host.ui.rawui.foregroundColor
if ( $all ) { $toList = ls -force $dir }
else { $toList = ls $dir }
foreach ($Item in $toList) {
Switch ($Item.Extension) {
".ps1" {$host.ui.rawui.foregroundColor = "Blue"}
".Exe" {$host.ui.rawui.foregroundColor = "Yellow"}
".msi" {$host.ui.rawui.foregroundColor = "Yellow"}
".cmd" {$host.ui.rawui.foregroundColor = "Red"}
".vbs" {$host.ui.rawui.foregroundColor = "Red"}
".bat" {$host.ui.rawui.foregroundColor = "Red"}
Default {$host.ui.rawui.foregroundColor = "White"}
}
if ($item.Mode.StartsWith("d")) {$host.ui.rawui.foregroundColor = "Green"}
$item
}
$host.ui.rawui.foregroundColor = $origFg
}
function lla
{
param ( $dir=".")
ll $dir $true
}
function la { Get-ChildItem -force }
function .. { Push-Location ..}
function ... { Push-Location ..\..}
function .... { Push-Location ..\..\..}
function ..... { Push-Location ..\..\..\..}
function ...... { Push-Location ..\..\..\..\..}
function ....... { Push-Location ..\..\..\..\..\..}
function test-dns {
<#
.Synopsis
Clears the DNSCache and checks connection to new DNS-given IP address
.Parameter computerName
Specifies the computer name
.Example
Test-DNS natedesk
#>
param ($computer)
Clear-DnsClientCache
$ip = ([system.net.dns]::resolve($computer)).AddressList.IPAddressToString
$newIP = $ip
while($newIP -eq $ip){
Clear-DnsClientCache
$newIP = ([system.net.dns]::resolve($computer)).AddressList.IPAddressToString
if(Test-Connection $computer -Count 1 -Quiet){
$status = "Online"
}else{
$status = "Offline"
}
write-host ((get-date -Format "h:mm tt") + " - - " + $newIP + " - - " + $status)
Start-sleep 5
}
}
function Get-ScheduledTask {
<#
.SYNOPSIS
Script that returns scheduled tasks on a computer
.DESCRIPTION
This script uses the Schedule.Service COM-object to query the local or a remote computer in order to gather a formatted list including the Author, UserId and description of the task. This information is parsed from the XML attributed to provide a more human readable format
.PARAMETER Computername
The computer that will be queried by this script, local administrative permissions are required to query this information
.NOTES
Name: Get-ScheduledTask.ps1
Author: Jaap Brasser
DateCreated: 2012-05-23
DateUpdated: 2015-08-17
# Site: http://www.jaapbrasser.com
Version: 1.3.2
.LINK
# http://www.jaapbrasser.com
.EXAMPLE
.\Get-ScheduledTask.ps1 -ComputerName server01
Description
-----------
This command query mycomputer1 and display a formatted list of all scheduled tasks on that computer
.EXAMPLE
.\Get-ScheduledTask.ps1
Description
-----------
This command query localhost and display a formatted list of all scheduled tasks on the local computer
.EXAMPLE
.\Get-ScheduledTask.ps1 -ComputerName server01 | Select-Object -Property Name,Trigger
Description
-----------
This command query server01 for scheduled tasks and display only the TaskName and the assigned trigger(s)
.EXAMPLE
.\Get-ScheduledTask.ps1 | Where-Object {$_.Name -eq 'TaskName') | Select-Object -ExpandProperty Trigger
Description
-----------
This command queries the local system for a scheduled task named 'TaskName' and display the expanded view of the assisgned trigger(s)
.EXAMPLE
Get-Content C:\Servers.txt | ForEach-Object { .\Get-ScheduledTask.ps1 -ComputerName $_ }
Description
-----------
Reads the contents of C:\Servers.txt and pipes the output to Get-ScheduledTask.ps1 and outputs the results to the console
#>
param(
[string]$ComputerName = $env:COMPUTERNAME,
[switch]$RootFolder
)
#region Functions
function Get-AllTaskSubFolders {
[cmdletbinding()]
param (
# Set to use $Schedule as default parameter so it automatically list all files
# For current schedule object if it exists.
$FolderRef = $Schedule.getfolder("\")
)
if ($FolderRef.Path -eq '\') {
$FolderRef
}
if (-not $RootFolder) {
$ArrFolders = @()
if (($Folders = $folderRef.getfolders(1))) {
$Folders | ForEach-Object {
$ArrFolders += $_
if ($_.getfolders(1)) {
Get-AllTaskSubFolders -FolderRef $_
}
}
}
$ArrFolders
}
}
function Get-TaskTrigger {
[cmdletbinding()]
param (
$Task
)
$Triggers = ([xml]$Task.xml).task.Triggers
if ($Triggers) {
$Triggers | Get-Member -MemberType Property | ForEach-Object {
$Triggers.($_.Name)
}
}
}
#endregion Functions
try {
$Schedule = New-Object -ComObject 'Schedule.Service'
}
catch {
Write-Warning "Schedule.Service COM Object not found, this script requires this object"
return
}
$Schedule.connect($ComputerName)
$AllFolders = Get-AllTaskSubFolders
foreach ($Folder in $AllFolders) {
if (($Tasks = $Folder.GetTasks(1))) {
$Tasks | Foreach-Object {
New-Object -TypeName PSCustomObject -Property @{
'Name' = $_.name
'Path' = $_.path
'State' = switch ($_.State) {
0 {'Unknown'}
1 {'Disabled'}
2 {'Queued'}
3 {'Ready'}
4 {'Running'}
Default {'Unknown'}
}
'Enabled' = $_.enabled
'LastRunTime' = $_.lastruntime
'LastTaskResult' = $_.lasttaskresult
'NumberOfMissedRuns' = $_.numberofmissedruns
'NextRunTime' = $_.nextruntime
'Author' = ([xml]$_.xml).Task.RegistrationInfo.Author
'UserId' = ([xml]$_.xml).Task.Principals.Principal.UserID
'Description' = ([xml]$_.xml).Task.RegistrationInfo.Description
'Trigger' = Get-TaskTrigger -Task $_
'ComputerName' = $Schedule.TargetServer
}
}
}
}
}
function Get-AllLogons {
param(
[Parameter(Mandatory = $true)]
[String]$computerName,
[Parameter(Mandatory = $false)]
$days = 15
)
$endDate = Get-Date
$startDate = ($endDate).AddDays( - $days)
get-winevent -computer $computerName -FilterHashtable @{logname = 'security'; id = 4624} | Select-Object @{N = 'User'; E = {$_.Properties[5].Value}}, @{N = 'Logon Type'; E = {$_.Properties[8].Value}}, TimeCreated
}
function Shrug {
#'¯\_(ツ)_/¯' | Set-Clipboard
-join [char[]](175, 92, 95, 40, 12484, 41, 95, 47, 175) | Set-Clipboard
}
function Throw-Table {
#'(╯°□°)╯︵ ┻━┻' | Set-Clipboard
(40, 9583, 176, 9633, 176, 65289, 9583, 65077, 32, 9531, 9473, 9531 | ForEach-Object {[char]$_}) -join "" | Set-Clipboard
}
function Stop-VMWare{
if(-not (Test-IsAdmin)){
write-host "You must run this as admin" -ForegroundColor 'Yellow'
}else{
get-service -DisplayName VMware* | Stop-Service
get-process vmware-tray | Stop-Process
}
}
function Start-VMware{
if(-not (Test-IsAdmin)){
write-host "You must run this as admin" -ForegroundColor 'Yellow'
}else{
get-service -DisplayName VMware* | Start-Service
}
}
function python {
$Host.UI.RawUI.BackgroundColor = "DarkGreen"
$Host.UI.RawUI.ForegroundColor = "White"
$Host.ui.RawUI.WindowTitle = "PYTHON"
Clear-Host
&python.exe
$Host.UI.RawUI.BackgroundColor = "Black"
$Host.UI.RawUI.ForegroundColor = "Green"
$Host.ui.RawUI.WindowTitle = "Regular Powershell"
Clear-Host
}
function Get-CompleteHistory {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false, Position = 1)]
[string]$strSearch # String to search
)
if ($strSearch) {
$lines = Get-Content $env:APPDATA\Microsoft\Windows\Powershell\PSReadline\ConsoleHost_history.txt | Select-String "$strSearch" -Context 1, 1
$count = $lines.Count
foreach ($line in $lines) {
Write-Host $line.context.precontext[0]
Write-Host $line.line -ForegroundColor Yellow
write-host $line.context.postcontext[0] + "`r`n"
}
}
else {
Get-Content $env:APPDATA\Microsoft\Windows\Powershell\PSReadline\ConsoleHost_history.txt
}
}