Public/WebPSScript.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
<#
.SYNOPSIS
Allows you to execute a PowerShell Script as a URL Link
 
.DESCRIPTION
Allows you to execute a PowerShell Script as a URL Link
 
.PARAMETER WebPSScript
The URL of the PowerShell Script to execute. Redirects are not allowed
 
.LINK
https://osd.osdeploy.com/module/functions/webpsscript
 
.NOTES
21.3.12 Renamed from Invoke-UrlExpression
21.3.8 Initial Release
#>

function Invoke-WebPSScript {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$WebPSScript
    )

    $WebPSScript = $WebPSScript -replace '%20',' '

    Write-Host -ForegroundColor Cyan "$WebPSScript"
    if (-NOT (Test-WebConnection $WebPSScript)) {
        Return
    }

    #[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls1
    
    $WebClient = New-Object System.Net.WebClient
    $WebPSCommand = $WebClient.DownloadString("$WebPSScript")
    Invoke-Expression -Command $WebPSCommand
    $WebClient.Dispose()
}