-
Notifications
You must be signed in to change notification settings - Fork 1
/
ps_custom_function.ps1
executable file
·819 lines (712 loc) · 28.9 KB
/
ps_custom_function.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# New PSObject Template
if (-Not ('Windows.Media.Fonts' -as [Type])) {
Add-Type -AssemblyName 'PresentationCore'
}
$DismObjT = New-Object -TypeName PSObject -Property @{
"Feature" = ""
"State" = ""
"ComputerName" = ""
}
function isadmin() {
# Returns true/false
([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
# Add expiration timer for receiving the input to the built-in "read-host" cmdlet
# https://stackoverflow.com/questions/48261349/powershell-wait-for-specified-key-to-be-pressed-otherwise-timeout
# http://thecuriousgeek.org/2014/10/powershell-read-host-with-timeout/
# $answers = Read-Host-Timeout "Continue?[Y/n]" 5
function Read-Host-Timeout() {
Param (
[Parameter(Mandatory=$true,Position=1)]
[string]$prompt,
[Parameter(Mandatory=$false,Position=2)]
[int]$delayInSeconds
)
Write-host -nonewline "$($prompt): "
$sleep = 200
$timeout = $delayInSeconds * 1000
$charArray = New-Object System.Collections.ArrayList
# While loop waits for the first key to be pressed for input and
# then exits. If the timer expires it returns null
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while (-not [Console]::KeyAvailable) {
if ($stopwatch.ElapsedMilliseconds -gt $timeout) {
return $null
}
Start-Sleep -Milliseconds $sleep
}
# Retrieve the key pressed, add it to the char array that is storing
# all keys pressed and then write it to the same line as the prompt
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp").Character
$charArray.Add($key) | out-null
Write-host -nonewline $key
# This block is where the script keeps reading for a key. Every time
# a key is pressed, it checks if it's a carriage return. If so, it exits the
# loop and returns the string. If not it stores the key pressed and
# then checks if it's a backspace and does the necessary cursor
# moving and blanking out of the backspaced character, then resumes
# writing.
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")
While ($key.virtualKeyCode -ne 13) {
If ($key.virtualKeycode -eq 8) {
$charArray.Add($key.Character) | out-null
Write-host -nonewline $key.Character
$cursor = $host.ui.rawui.get_cursorPosition()
write-host -nonewline " "
$host.ui.rawui.set_cursorPosition($cursor)
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")
} else {
$charArray.Add($key.Character) | out-null
Write-host -nonewline $key.Character
$key = $host.ui.rawui.readkey("NoEcho,IncludeKeyUp")
}
}
""
$finalString = -join $charArray
return $finalString
}
function CheckDownloadPWSHNewVersion {
[Version]$ReleaseVersion = (Invoke-RestMethod 'https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/metadata.json').ReleaseTag -replace '^v'
if ($PSVersionTable.PSEdition -like "Core" -and $ReleaseVersion -gt $PSVersionTable.PSVersion) {
$latest = Invoke-RestMethod -Uri "https://api.github.com/repos/PowerShell/PowerShell/releases" | Where-Object { $_.tag_name -eq "v$ReleaseVersion" }
$downloadUrl = $latest.assets | Where-Object Name -like "*win-x64.msi" | Select-Object -ExpandProperty 'browser_download_url'
Invoke-WebRequest -Uri $downloadUrl -OutFile "$PSScriptRoot\$(Split-Path $downloadUrl -Leaf)"
}
## another method
# $latest = Invoke-RestMethod 'https://api.github.com/repos/PowerShell/PowerShell/releases/latest'
# $downloadUrl = $latest.assets | Where-Object Name -like "*win-x64.msi" | Select-Object -ExpandProperty 'browser_download_url'
# $fileName = Split-Path $downloadUrl -Leaf
# $webClient = New-Object System.Net.WebClient
# try {
# $webClient.DownloadFile($downloadUrl, "$PSScriptRoot\$fileName")
# }
# finally {
# $webClient.Dispose()
# }
}
# https://gallery.technet.microsoft.com/scriptcenter/Parse-DISM-Get-Features-d25dde0a
# Must enable PSremoting on remote PC
# Enable-PSRemoting
function GetDISMOnlineFeatures() {
Param (
# Set one or multiple computernames, also used for refering the logfile names with -UseLog. Default target is localhost computername, looks for\creates dism_<localhostname>.log
[string[]]$Computers = $env:COMPUTERNAME
)
# Creating Blank array for holding the result
$objResult = @()
foreach ($Computer in $Computers) {
# Read current values
$List = Invoke-Command -ComputerName $Computer {Dism /online /English /Get-Features}
# Use this if you get WinRM errors for above line, making the script local only
# $List = Dism /online /Get-Features
#Counter for getting alternate values
$i = 1
#Parsing the data
#$List | Where-Object { $_.StartsWith("Feature Name :") -OR $_.StartsWith("State :") }| # where(43ms) is slower than Select-String(20ms)
$List | Select-String -pattern "Feature Name :", "State :" | ForEach-Object {
if ($i%2) {
#Creating new object\Resetting for every item using template
$TempObj = $DismObjT | Select-Object *
#Assigning Value1
$TempObj.Feature = ([string]$_).split(":")[1].trim() ;$i=0
} else {
#Assigning Value2
$TempObj.State = ([string]$_).split(":")[1].trim() ;$i=1
$TempObj.ComputerName = $Computer
#Incrementing the object once both values filled
$objResult+=$TempObj
}
}
}
return $objResult
}
function GetDISMOnlineCapabilities() {
Param (
# Set one or multiple computernames, also used for refering the logfile names with -UseLog. Default target is localhost computername, looks for\creates dism_<localhostname>.log
[string[]]$Computers = $env:COMPUTERNAME
)
# Creating Blank array for holding the result
$objResult = @()
foreach ($Computer in $Computers) {
# Read current values
$List = Invoke-Command -ComputerName $Computer {Dism /online /English /Get-Capabilities}
#Counter for getting alternate values
$i = 1
#Parsing the data
#$List | Where-Object { $_.StartsWith("Feature Name :") -OR $_.StartsWith("State :") }| # where(43ms) is slower than Select-String(20ms)
$List | Select-String -pattern "Capability Identity :", "State :" | ForEach-Object {
if ($i%2) {
#Creating new object\Resetting for every item using template
$TempObj = $DismObjT | Select-Object *
#Assigning Value1
$TempObj.Feature = ([string]$_).split(":")[1].trim() ;$i=0
} else {
#Assigning Value2
$TempObj.State = ([string]$_).split(":")[1].trim() ;$i=1
$TempObj.ComputerName = $Computer
#Incrementing the object once both values filled
$objResult+=$TempObj
}
}
}
return $objResult
}
function GetIPGeolocation() {
Param ($ipaddress)
$resource = "http://ip-api.com/json/$ipaddress"
try {
$geoip = Invoke-RestMethod -Method Get -URI $resource
} catch {
Write-Verbose -Message "Catched an error"
$PSCmdlet.ThrowTerminatingError($PSitem)
}
# $geoip | Get-Member
$hash = @{
IP = $geoip.query
CountryCode = $geoip.countryCode
Country = $geoip.country
Region = $geoip.region
RegionName = $geoip.regionName
AS = $geoip.as
ISP = $geoip.isp
ORG = $geoip.org
City = $geoip.city
ZipCode = $geoip.zip
TimeZone = $geoip.timezone
Latitude = $geoip.lat
Longitude = $geoip.lon
}
$result = New-Object PSObject -Property $hash
return $result
}
function check_webservice_up() {
param($webservice_url)
if (($null -eq $webservice_url) -or ($webservice_url -eq "")) {
$webservice_url = "www.google.com"
}
curl -fsL --connect-timeout 3 --max-time 5 -I "$webservice_url"
if ($?) {
return $true
} else {
return $false
}
}
function check_socks5_proxy_up() {
Param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $proxy_url,
[Parameter(Mandatory = $false, Position = 1)]
[string] $webservice_url
)
if (($null -eq $webservice_url) -or ($webservice_url -eq "")) {
$webservice_url = "www.google.com"
}
curl -fsL --connect-timeout 3 --max-time 5 --socks5-hostname "$proxy_url" -I "$webservice_url"
if ($?) {
return $true
} else {
return $false
}
}
function check_http_proxy_up() {
Param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $proxy_url,
[Parameter(Mandatory = $false, Position = 1)]
[string] $webservice_url
)
if (($null -eq $webservice_url) -or ($webservice_url -eq "")) {
$webservice_url = "www.google.com"
}
curl -fsL --connect-timeout 3 --max-time 5 --proxy "$proxy_url" -I "$webservice_url"
if ($?) {
return $true
} else {
return $false
}
}
function Set-WinHTTP-Proxy {
<#
.Description
This function will set the proxy server using netsh.
.Example
Setting proxy information
Set-WinHTTP-Proxy -proxy "127.0.0.1:7890"
Set-WinHTTP-Proxy -proxy "socks=127.0.0.1:7890" -Bypass "localhost"
#>
Param (
[Parameter(Mandatory = $false, Position = 0)]
[string] $Proxy,
[Parameter(Mandatory = $false, Position = 1)]
[string] $Bypass
)
# netsh winhttp set proxy proxy-server="socks=127.0.0.1:7890" bypass-list="localhost"
if (($null -eq $Proxy) -or ($Proxy -eq "")) {
netsh winhttp reset proxy
} else {
if ($Proxy -eq "ie") {
netsh winhttp import proxy source=ie
} else {
if ($Bypass) {
netsh winhttp set proxy proxy-server="$Proxy" bypass-list="$Bypass"
} else {
netsh winhttp set proxy "$Proxy"
}
}
}
}
function Set-InternetProxy {
<#
.Description
This function will set the proxy server and (optinal) Automatic configuration script.
.Example
Setting proxy information
Set-InternetProxy -proxy "127.0.0.1:7890"
.Example
Setting proxy information and (optinal) Automatic Configuration Script
Set-InternetProxy -proxy "127.0.0.1:7890" -acs "http://127.0.0.1:7892"
#>
Param(
[Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[String[]] $Proxy,
[Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[String[]] $acs
)
Begin {
$regKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
}
# Get-ItemProperty -Path $regKey | Select-Object ProxyServer, ProxyEnable, ProxyOverride
Process {
Set-ItemProperty -path $regKey -Name ProxyEnable -value 1
Set-ItemProperty -path $regKey -Name ProxyServer -value $proxy
Set-ItemProperty -Path $regKey -Name ProxyOverride -Value '<local>'
if ($acs) {
Set-ItemProperty -path $regKey -Name AutoConfigURL -Value $acs
}
[System.Environment]::SetEnvironmentVariable('http_proxy', $proxy, 'User')
[System.Environment]::SetEnvironmentVariable('https_proxy', $proxy, 'User')
[System.Environment]::SetEnvironmentVariable('HTTP_PROXY', $proxy, 'User')
[System.Environment]::SetEnvironmentVariable('HTTPS_PROXY', $proxy, 'User')
}
End {
Write-Output "Proxy is now enabled, Proxy Server: $proxy"
if ($acs) {
Write-Output "Automatic Configuration Script: $acs"
} else {
Write-Output "Automatic Configuration Script: Not Defined"
}
}
}
function Clear-InternetProxy {
Begin {
$regKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
}
Process {
Set-ItemProperty -path $regKey -Name ProxyEnable -value 0
Set-ItemProperty -path $regKey -Name ProxyServer -value ''
Set-ItemProperty -Path $regKey -Name ProxyOverride -Value ''
Set-ItemProperty -path $regKey -Name AutoConfigURL -Value ''
[System.Environment]::SetEnvironmentVariable('http_proxy', $null, 'User')
[System.Environment]::SetEnvironmentVariable('https_proxy', $null, 'User')
[System.Environment]::SetEnvironmentVariable('HTTP_PROXY', $null, 'User')
[System.Environment]::SetEnvironmentVariable('HTTPS_PROXY', $null, 'User')
}
End {
Write-Output "Proxy is now disabled!"
}
}
function DownloadHosts() {
param (
[Parameter(Mandatory = $false, Position = 0)]
[string] $HostsURL,
[Parameter(Mandatory = $false, Position = 1)]
[string] $Proxy
)
if (-Not (isadmin)) {
Write-Host "This script needs to be run As Admin!" -ForegroundColor Red
return
}
if (($null -eq $HostsURL) -or ($HostsURL -eq "")) {
$HostsURL = "https://raw.githubusercontent.com/googlehosts/hosts/master/hosts-files/hosts"
}
$Hostfile = "$env:windir\System32\drivers\etc\hosts"
$HostOriginal = "$env:windir\System32\drivers\etc\hosts.original"
$Hostbackup = "$env:windir\System32\drivers\etc\hosts.bak"
$DOWNLOAD_TO = "$env:windir\System32\drivers\etc\hosts.download"
if (-Not (Test-Path $HostOriginal)) {
Copy-Item $Hostfile -Destination $HostOriginal
}
if (Test-Path $DOWNLOAD_TO) {
Remove-Item $DOWNLOAD_TO
}
if (($null -eq $Proxy) -or ($Proxy -eq "")) {
curl -fsL --connect-timeout 5 --ssl-no-revoke -o "$DOWNLOAD_TO" "$HostsURL"
} else {
curl -fsL --connect-timeout 5 --ssl-no-revoke --socks5-hostname "$Proxy" -o "$DOWNLOAD_TO" "$HostsURL"
}
if ($?) {
if ((Test-Path $DOWNLOAD_TO) -and ((Get-Item $DOWNLOAD_TO).length -gt 0)) {
Copy-Item $Hostfile -Destination $Hostbackup
Copy-Item $DOWNLOAD_TO -Destination $Hostfile
}
# flush dns
ipconfig -flushdns | Out-Null
}
}
function RestartWSL {
if (-Not (isadmin)) {
Write-Host "This script needs to be run As Admin!" -ForegroundColor Red
return
}
# Windows 10
if (Get-Service -Name "LxssManager" -ErrorAction SilentlyContinue) {
Stop-Service -Name "LxssManager"
Start-Service -Name "LxssManager"
}
# Windows 11
if (Get-Service -Name "WslService" -ErrorAction SilentlyContinue) {
Stop-Service -Name "WslService"
Start-Service -Name "WslService"
}
}
function RestartWSA {
if (-Not (isadmin)) {
Write-Host "This script needs to be run As Admin!" -ForegroundColor Red
return
}
if (Get-Service -Name "WsaService" -ErrorAction SilentlyContinue) {
Stop-Service -Name "WsaService"
Start-Service -Name "WsaService"
}
}
# https://www.powershellgallery.com/packages/RoughDraft/0.1/Content/Get-Font.ps1
function GetFonts() {
<#
.Synopsis
Gets the fonts available
.Description
Gets the fonts available on the current installation
.Example
GetFonts
.Example
GetFonts -IncludeDetail
#>
# [OutputType([Windows.Media.FontFamily], [string])]
param(
# If set, finds finds with this name
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true)]
[string]$Name,
# If set, will include all details of the font
[switch]$IncludeDetail,
# If set, will sort the results
[Switch]$Sort
)
begin {
$fontList = [Windows.Media.Fonts]::SystemFontFamilies
}
process {
#region Filter Font List
if ($Name.Trim()) {
$currentFontList = foreach ($f in $fontList) {
if ($f.Source -like "$name*") {
$f
}
}
} else {
$currentFontList = $fontList
}
#endregion Filter Font List
if ($IncludeDetail) {
if ($sort) {
$currentFontList |
Sort-Object Source |
Add-Member ScriptProperty Name { $this.Source } -PassThru -Force
} else {
$currentFontList |
Add-Member ScriptProperty Name { $this.Source } -PassThru -Force
}
} else {
if ($sort) {
$currentFontList |
Sort-Object Source |
Select-Object -ExpandProperty Source
} else {
$currentFontList |
Select-Object -ExpandProperty Source
}
}
}
}
function CheckSetGlobalProxy() {
param (
[string]$ProxyAddress = "127.0.0.1",
[string]$ProxyMixedPort = "7890",
[string]$ProxyHttpPort = "7890",
[switch]$InputAddress,
[string]$Msg = "Porxy address?"
)
if ($env:GLOBAL_PROXY_IP) { return }
if (!$ProxyHttpPort) {
$ProxyHttpPort = $ProxyMixedPort
}
$Proxy = ""
if (-Not (check_webservice_up)) {
$Proxy = "${ProxyAddress}:${ProxyMixedPort}"
if (-Not (check_socks5_proxy_up $Proxy)) {
if (-Not (check_http_proxy_up $Proxy)) {
$Proxy = ""
}
}
if (!$Proxy) {
$Proxy = "${ProxyAddress}:${ProxyHttpPort}"
if (-Not (check_http_proxy_up $Proxy)) {
$Proxy = ""
}
}
if (!$Proxy -and $InputAddress) {
if ($PROMPT_VALUE = Read-Host "$Msg[127.0.0.1:7890]") {
$Proxy = $PROMPT_VALUE
if (!$Proxy) {
$Proxy = "127.0.0.1:7890"
}
if (-Not (check_http_proxy_up $Proxy)) {
$Proxy = ""
}
} else {
$Proxy = ""
}
}
}
if ($Proxy) {
$env:GLOBAL_PROXY_IP = $ProxyAddress
$env:GLOBAL_PROXY_MIXED_PORT = $ProxyMixedPort
$env:GLOBAL_PROXY_HTTP_PORT = $ProxyHttpPort
$env:HTTP_PROXY="http://${ProxyAddress}:${ProxyHttpPort}"
$env:HTTPS_PROXY="http://${ProxyAddress}:${ProxyHttpPort}"
$env:NO_PROXY="localhost,127.0.0.1,::1"
} else {
if ($env:GLOBAL_PROXY_IP) {Remove-Item "Env:\GLOBAL_PROXY_IP"}
if ($env:GLOBAL_PROXY_MIXED_PORT) {Remove-Item "Env:\GLOBAL_PROXY_MIXED_PORT"}
if ($env:GLOBAL_PROXY_HTTP_PORT) {Remove-Item "Env:\GLOBAL_PROXY_HTTP_PORT"}
if ($env:HTTP_PROXY) {Remove-Item "Env:\HTTP_PROXY"}
if ($env:HTTPS_PROXY) {Remove-Item "Env:\HTTPS_PROXY"}
if ($env:NO_PROXY) {Remove-Item "Env:\NO_PROXY"}
}
# if (![string]::IsNullOrEmpty($Proxy)) {
# if (![string]::IsNullOrWhiteSpace($Proxy)) {
if ($Proxy) {
# Write-Host "HTTP_PROXY=$env:HTTP_PROXY`nHTTPS_PROXY=$env:HTTPS_PROXY`nNO_PROXY=$env:NO_PROXY`n" -ForegroundColor Yellow
Write-Host " ::" -ForegroundColor Green -NoNewline
Write-Host " HTTP_PROXY" -ForegroundColor Magenta -NoNewline
Write-Host "=" -ForegroundColor Cyan -NoNewline
Write-Host "$env:HTTP_PROXY" -ForegroundColor Yellow -NoNewline
Write-Host " HTTPS_PROXY" -ForegroundColor Magenta -NoNewline
Write-Host "=" -ForegroundColor Cyan -NoNewline
Write-Host "$env:HTTPS_PROXY" -ForegroundColor Yellow -NoNewline
Write-Host " NO_PROXY" -ForegroundColor Magenta -NoNewline
Write-Host "=" -ForegroundColor Cyan -NoNewline
Write-Host "$env:NO_PROXY" -ForegroundColor Yellow
}
}
function RebuildFontCache {
# https://eddiejackson.net/wp/?p=16137
# https://www.isunshare.com/windows-10/how-to-delete-font-cache-in-windows-10.html
if (-Not (isadmin)) {
Write-Host "This script needs to be run As Admin!" -ForegroundColor Red
return
}
Stop-Service -Name "FontCache"
Remove-Item "$env:windir\ServiceProfiles\LocalService\AppData\Local\FontCache" -Recurse -Force -Confirm:$false -ErrorAction Stop
Remove-Item "$env:windir\System32\FNTCACHE.DAT" -Force -Confirm:$false -ErrorAction Stop
Start-Service -Name "FontCache"
}
function ConvertTo-HexString {
<#
.SYNOPSIS
Convert to Hex String
.DESCRIPTION
Convert to Hex String
https://www.powershellgallery.com/packages/Utility.PS/
.EXAMPLE
Convert string to hex byte string seperated by spaces.
ConvertTo-HexString "What is a hex string?"
.EXAMPLE
Convert ASCII string to hex byte string with no seperation.
"ASCII string to hex string" | ConvertTo-HexString -Delimiter "" -Encoding Ascii
.INPUTS
System.Object
#>
[CmdletBinding()]
param (
# Value to convert
[Parameter(Mandatory=$true, Position = 0, ValueFromPipeline=$true)]
[object] $InputObjects,
# Delimiter between Hex pairs
[Parameter (Mandatory=$false)]
[string] $Delimiter = ' ',
# Encoding to use for text strings
[Parameter (Mandatory=$false)]
[ValidateSet('Ascii', 'UTF32', 'UTF7', 'UTF8', 'BigEndianUnicode', 'Unicode')]
[string] $Encoding = 'Default'
)
begin {
function Transform ([byte[]]$InputBytes) {
[string[]] $outHexString = New-Object string[] $InputBytes.Count
for ($iByte = 0; $iByte -lt $InputBytes.Count; $iByte++) {
$outHexString[$iByte] = $InputBytes[$iByte].ToString('X2')
}
return $outHexString -join $Delimiter
}
## Create list to capture byte stream from piped input.
[System.Collections.Generic.List[byte]] $listBytes = New-Object System.Collections.Generic.List[byte]
}
process
{
if ($InputObjects -is [byte[]]) {
Write-Output (Transform $InputObjects)
} else {
foreach ($InputObject in $InputObjects) {
[byte[]] $InputBytes = $null
if ($InputObject -is [byte]) {
## Populate list with byte stream from piped input.
if ($listBytes.Count -eq 0) {
Write-Verbose 'Creating byte array from byte stream.'
Write-Warning ('For better performance when piping a single byte array, use "Write-Output $byteArray -NoEnumerate | {0}".' -f $MyInvocation.MyCommand)
}
$listBytes.Add($InputObject)
} elseif ($InputObject -is [byte[]]) {
$InputBytes = $InputObject
} elseif ($InputObject -is [string]) {
$InputBytes = [Text.Encoding]::$Encoding.GetBytes($InputObject)
} elseif ($InputObject -is [bool] -or $InputObject -is [char] -or $InputObject -is [single] -or $InputObject -is [double] -or $InputObject -is [int16] -or $InputObject -is [int32] -or $InputObject -is [int64] -or $InputObject -is [uint16] -or $InputObject -is [uint32] -or $InputObject -is [uint64]) {
$InputBytes = [System.BitConverter]::GetBytes($InputObject)
} elseif ($InputObject -is [guid]) {
$InputBytes = $InputObject.ToByteArray()
} elseif ($InputObject -is [System.IO.FileSystemInfo]) {
if ($PSVersionTable.PSVersion -ge [version]'6.0') {
$InputBytes = Get-Content $InputObject.FullName -Raw -AsByteStream
} else {
$InputBytes = Get-Content $InputObject.FullName -Raw -Encoding Byte
}
} else {
## Non-Terminating Error
$Exception = New-Object ArgumentException -ArgumentList ('Cannot convert input of type {0} to Hex string.' -f $InputObject.GetType())
Write-Error -Exception $Exception -Category ([System.Management.Automation.ErrorCategory]::ParserError) -CategoryActivity $MyInvocation.MyCommand -ErrorId 'ConvertHexFailureTypeNotSupported' -TargetObject $InputObject
}
if ($null -ne $InputBytes -and $InputBytes.Count -gt 0) {
Write-Output (Transform $InputBytes)
}
}
}
}
end {
## Output captured byte stream from piped input.
if ($listBytes.Count -gt 0) {
Write-Output (Transform $listBytes.ToArray())
}
}
}
function ConvertFrom-HexString {
<#
.SYNOPSIS
Convert from Hex String
.DESCRIPTION
Convert from Hex String
https://www.powershellgallery.com/packages/Utility.PS/
.EXAMPLE
ConvertFrom-HexString "68 65 6C 6C 6F 20 77 6F 72 6C 64" # hello world
#>
[CmdletBinding()]
param (
# Value to convert
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[string[]] $InputObject,
# Delimiter between Hex pairs
[Parameter (Mandatory=$false)]
[string] $Delimiter = " ",
# Output raw byte array
[Parameter (Mandatory=$false)]
[switch] $RawBytes,
# Encoding to use for text strings
[Parameter (Mandatory=$false)]
[ValidateSet("Ascii", "UTF32", "UTF7", "UTF8", "BigEndianUnicode", "Unicode")]
[string] $Encoding = "Default"
)
process
{
$listBytes = New-Object object[] $InputObject.Count
for ($iString = 0; $iString -lt $InputObject.Count; $iString++) {
[string] $strHex = $InputObject[$iString]
if ($strHex.Substring(2,1) -eq $Delimiter) {
[string[]] $listHex = $strHex -split $Delimiter
} else {
[string[]] $listHex = New-Object string[] ($strHex.Length/2)
for ($iByte = 0; $iByte -lt $strHex.Length; $iByte += 2) {
$listHex[[System.Math]::Truncate($iByte/2)] = $strHex.Substring($iByte, 2)
}
}
[byte[]] $outBytes = New-Object byte[] $listHex.Count
for ($iByte = 0; $iByte -lt $listHex.Count; $iByte++)
{
$outBytes[$iByte] = [byte]::Parse($listHex[$iByte],[System.Globalization.NumberStyles]::HexNumber)
}
if ($RawBytes) { $listBytes[$iString] = $outBytes } else {
$outString = ([Text.Encoding]::$Encoding.GetString($outBytes))
Write-Output $outString
}
}
if ($RawBytes) {
return $listBytes
}
}
}
# unix like `cut` command
function cut() {
param (
[Parameter(ValueFromPipeline = $True)]
[string]$inputobject,
[string]$delimiter='\s+',
[string[]]$field
)
process {
if ($null -eq $field) {
$inputobject -split $delimiter
} else {
($inputobject -split $delimiter)[$field]
}
}
}
# Finding Illegal Characters in Path
function findIllegalCharsInPath() {
param (
[string]$pathToCheck
)
# get invalid characters and escape them for use with RegEx
$illegal = [Regex]::Escape(-join [Io.Path]::GetInvalidPathChars())
$pattern = "[$illegal]"
# find illegal characters
$invalid = [regex]::Matches($pathToCheck, $pattern, 'IgnoreCase').Value | Sort-Object -Unique
# $invalid | Format-Hex
if ($null -ne $invalid) {
Write-Host "Don't use these characters in path: $invalid" -ForegroundColor Red
} else {
Write-Host "No invalid path characters in: $pathToCheck" -ForegroundColor Blue
}
}
# Finding Illegal Characters in Filename
function findIllegalCharsInFilename() {
param (
[string]$fileToCheck
)
# get invalid characters and escape them for use with RegEx
$illegal = [Regex]::Escape(-join [Io.Path]::GetInvalidFileNameChars())
$pattern = "[$illegal]"
# find illegal characters
$invalid = [regex]::Matches($fileToCheck, $pattern, 'IgnoreCase').Value | Sort-Object -Unique
if ($null -ne $invalid) {
Write-Host "Don't use these characters in path: $invalid" -ForegroundColor Red
} else {
Write-Host "No invalid file characters in: $fileToCheck" -ForegroundColor Blue
}
}