Skip to content

Commit 9b5cfc4

Browse files
author
devizer
committed
Run-Remote-Script (ps1) [skip ci]
1 parent 4969855 commit 9b5cfc4

2 files changed

Lines changed: 159 additions & 4 deletions

File tree

Install-SqlServer-Version-Management.ps1

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Param(
44
)
55

66
$ModuleName = 'SqlServer-Version-Management';
7-
$ModuleVersion = '2.1.177';
7+
$ModuleVersion = '2.1.179';
88
$ModuleFiles = @(
99
@{
1010
FileName = 'SqlServer-Version-Management\SqlServer-Version-Management.psd1';
@@ -15,7 +15,7 @@ $ModuleFiles = @(
1515
" ModuleToProcess = @('SqlServer-Version-Management.psm1')",
1616
"",
1717
" # Version below is automatically incremented on build",
18-
" ModuleVersion = `"2.1.177`"",
18+
" ModuleVersion = `"2.1.179`"",
1919
"",
2020
" GUID = 'dd03b53d-575a-4056-ae08-e6dfea3384ea'",
2121
"",
@@ -480,7 +480,7 @@ $ModuleFiles = @(
480480
" Troubleshoot-Info `"Starting download ```"`" -Highlight `"`$url`" `"```" using aria2c as ```"`" -Highlight `"`$outfile`" `"```"`"",
481481
" # `"-k`", `"2M`",",
482482
" `$startAt = [System.Diagnostics.Stopwatch]::StartNew()",
483-
" & aria2c.exe @(`"--allow-overwrite=true`", `"--check-certificate=false`", `"-x`", `"16`", `"-j`", `"16`", `"-d`", `"`$(`$dirName)`", `"-o`", `"`$([System.IO.Path]::GetFileName(`$outfile))`", `"`$url`");",
483+
" & aria2c.exe @(`"--allow-overwrite=true`", `"--check-certificate=false`", `"-x`", `"16`", `"-j`", `"16`", `"-d`", `"`$(`$dirName)`", `"-o`", `"`$([System.IO.Path]::GetFileName(`$outfile))`", `"`$url`") | out-null;",
484484
" if (`$?) { ",
485485
" <# Write-Host `"aria2 rocks (`$([System.IO.Path]::GetFileName(`$outfile)))`"; #> ",
486486
" try { `$length = (new-object System.IO.FileInfo(`$outfile)).Length; } catch {}; `$milliSeconds = `$startAt.ElapsedMilliseconds;",
@@ -1687,6 +1687,83 @@ $ModuleFiles = @(
16871687
"# @(42) | Reverse-Pipe",
16881688
"# @(1,2,3,4,`"42`") | Reverse-Pipe",
16891689
"",
1690+
"# Include File: [\Includes\Run-Remote-Script.ps1]",
1691+
"function Get-FileName-by-Uri([string] `$url) {",
1692+
" `$uri = [uri]`$url",
1693+
" `$ret = `$uri.Segments[-1]",
1694+
" if (`$ret -eq `"download`" -and `$uri.Segments.Count -gt 1) {",
1695+
" `$ret = `$uri.Segments[-2]",
1696+
" `$ret=`"`$ret`".TrimEnd(`"/`")",
1697+
" }",
1698+
" return `$ret",
1699+
"}",
1700+
"",
1701+
"function Run-Remote-Script() {",
1702+
" `$pars = @(`$args)",
1703+
" `$url=`$null",
1704+
" `$runner=`"`"",
1705+
" `$passthrowArgs=@()",
1706+
" for(`$p=0; `$p -lt `$pars.Length; `$p++) {",
1707+
" `$vType=`"'null'`"; if (`$v -ne `$null) { `$vType = `"`$(`$v.GetType().Name)`" }",
1708+
" # Write-Host `"[DEBUG ARGS] v=[`$v], `$vType`" -ForegroundColor Yellow",
1709+
" `$v = `$pars[`$p];",
1710+
" if ((`$p -le 1) -and (`$v -eq `"-r`" -or `$v -eq `"--runner`")) {",
1711+
" if (`$p+1 -lt `$pars.Length) {",
1712+
" `$runner=`$pars[`$p+1];",
1713+
" `$p++;",
1714+
" continue;",
1715+
" } Else {",
1716+
" throw `"Run-Remote-Script Arguments Error: -r|--runner requires a value`"",
1717+
" }",
1718+
" }",
1719+
" Else {",
1720+
" if (`$url -eq `$null -or -not `$url) { ",
1721+
" `$url=`$v",
1722+
" } Else {",
1723+
" `$passthrowArgs += `$v",
1724+
" }",
1725+
" }",
1726+
" }",
1727+
"",
1728+
"",
1729+
" `$fileOnly = Get-FileName-by-Uri `$url",
1730+
" `$folder = Combine-Path (Get-PS1-Repo-Downloads-Folder) `"Scripts`"",
1731+
" `$fileFullName = Combine-Path `$folder `$fileOnly",
1732+
" `$runnerParameter=`"`"",
1733+
" if (-not `$runner) {",
1734+
" if (`"`$fileFullName`".ToLower().EndsWith(`".ps1`")) {",
1735+
" `$runner=`"`$([System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName)`"",
1736+
" # or if (([System.IO.Path]::GetFileName(`$runner)) -eq `"powershell.exe`")",
1737+
" if (`$PSVersionTable.PSEdition -and `$PSVersionTable.PSEdition -eq `"Desktop`" -or ([System.IO.Path]::GetFileName(`$runner)) -eq `"powershell.exe`") {",
1738+
" `$runnerParameter=`" -f`"",
1739+
" }",
1740+
" }",
1741+
" elseif (`"`$fileFullName`".ToLower().EndsWith(`".sh`")) {",
1742+
" `$runner=`"bash`"",
1743+
" }",
1744+
" else {",
1745+
" `$err=`"[Run-Remote-Script] Abort. Unable to infer runner for remote script '`$url', file is '`$fileFullName'`"",
1746+
" Write-Line -TextRed `"`$err`"",
1747+
" throw `$err;",
1748+
" }",
1749+
" }",
1750+
" Write-Line `"Invoking `" -TextCyan `"`$runner`$runnerParameter`" `" `" -TextGreen `"`$url`" `" `" -TextGreen `"`$passthrowArgs`" ",
1751+
" # Write-Host `"RUNNER: [`$runner]`"",
1752+
" # Write-Host `"URL: [`$url]`"",
1753+
" # Write-Host `"Arguments (Count = `$(`$passthrowArgs.Length)): `$passthrowArgs`"",
1754+
" # Write-Host `"Local File: `$fileFullName`"",
1755+
" `$okDownload = Download-File-Managed `"`$url`" `"`$fileFullName`"",
1756+
" # Write-Host `"okDownload: `$okDownload`"",
1757+
" if (`$okDownload) {",
1758+
" `$allWorld = `$runner.Split(`" `") + @(`$runnerParameter) + @(`$fileFullName) + `$passthrowArgs;",
1759+
" `$allWorld = @(`$allWorld | ? { `"`$_`" })",
1760+
" & `$allWorld[0] @(`$allWorld | Select-Object -Skip 1) | Out-Host",
1761+
" if (-not `$?) {",
1762+
" Write-Line -TextRed `"Run-Remote-Script FAIL: '`$url'`"",
1763+
" }",
1764+
" }",
1765+
"}",
1766+
"",
16901767
"# Include File: [\Includes\Say.ps1]",
16911768
"function Say { # param( [string] `$message )",
16921769
" if (`$Global:_Say_Stopwatch -eq `$null) { `$Global:_Say_Stopwatch = [System.Diagnostics.Stopwatch]::StartNew(); }",
@@ -4168,6 +4245,7 @@ $ModuleFiles = @(
41684245
"Export-ModuleMember -Function Download-File-FailFree",
41694246
"Export-ModuleMember -Function Download-File-FailFree-and-Cached",
41704247
"Export-ModuleMember -Function Download-File-Managed",
4248+
"Export-ModuleMember -Function Run-Remote-Script",
41714249
"Export-ModuleMember -Function Query-Download-File-Size",
41724250
"Export-ModuleMember -Function Download-Fresh-SQLServer-and-Extract",
41734251
"Export-ModuleMember -Function Download-SQLServer-and-Extract",

SqlServer-Version-Management.ps1

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ function Download-File-Managed([string] $url, [string]$outfile, [bool] $showFile
347347
Troubleshoot-Info "Starting download `"" -Highlight "$url" "`" using aria2c as `"" -Highlight "$outfile" "`""
348348
# "-k", "2M",
349349
$startAt = [System.Diagnostics.Stopwatch]::StartNew()
350-
& aria2c.exe @("--allow-overwrite=true", "--check-certificate=false", "-x", "16", "-j", "16", "-d", "$($dirName)", "-o", "$([System.IO.Path]::GetFileName($outfile))", "$url");
350+
& aria2c.exe @("--allow-overwrite=true", "--check-certificate=false", "-x", "16", "-j", "16", "-d", "$($dirName)", "-o", "$([System.IO.Path]::GetFileName($outfile))", "$url") | out-null;
351351
if ($?) {
352352
<# Write-Host "aria2 rocks ($([System.IO.Path]::GetFileName($outfile)))"; #>
353353
try { $length = (new-object System.IO.FileInfo($outfile)).Length; } catch {}; $milliSeconds = $startAt.ElapsedMilliseconds;
@@ -1554,6 +1554,83 @@ function Reverse-Pipe() { $copy=@($input); for($i = $copy.Length - 1; $i -ge 0;
15541554
# @(42) | Reverse-Pipe
15551555
# @(1,2,3,4,"42") | Reverse-Pipe
15561556

1557+
# Include File: [\Includes\Run-Remote-Script.ps1]
1558+
function Get-FileName-by-Uri([string] $url) {
1559+
$uri = [uri]$url
1560+
$ret = $uri.Segments[-1]
1561+
if ($ret -eq "download" -and $uri.Segments.Count -gt 1) {
1562+
$ret = $uri.Segments[-2]
1563+
$ret="$ret".TrimEnd("/")
1564+
}
1565+
return $ret
1566+
}
1567+
1568+
function Run-Remote-Script() {
1569+
$pars = @($args)
1570+
$url=$null
1571+
$runner=""
1572+
$passthrowArgs=@()
1573+
for($p=0; $p -lt $pars.Length; $p++) {
1574+
$vType="'null'"; if ($v -ne $null) { $vType = "$($v.GetType().Name)" }
1575+
# Write-Host "[DEBUG ARGS] v=[$v], $vType" -ForegroundColor Yellow
1576+
$v = $pars[$p];
1577+
if (($p -le 1) -and ($v -eq "-r" -or $v -eq "--runner")) {
1578+
if ($p+1 -lt $pars.Length) {
1579+
$runner=$pars[$p+1];
1580+
$p++;
1581+
continue;
1582+
} Else {
1583+
throw "Run-Remote-Script Arguments Error: -r|--runner requires a value"
1584+
}
1585+
}
1586+
Else {
1587+
if ($url -eq $null -or -not $url) {
1588+
$url=$v
1589+
} Else {
1590+
$passthrowArgs += $v
1591+
}
1592+
}
1593+
}
1594+
1595+
1596+
$fileOnly = Get-FileName-by-Uri $url
1597+
$folder = Combine-Path (Get-PS1-Repo-Downloads-Folder) "Scripts"
1598+
$fileFullName = Combine-Path $folder $fileOnly
1599+
$runnerParameter=""
1600+
if (-not $runner) {
1601+
if ("$fileFullName".ToLower().EndsWith(".ps1")) {
1602+
$runner="$([System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName)"
1603+
# or if (([System.IO.Path]::GetFileName($runner)) -eq "powershell.exe")
1604+
if ($PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq "Desktop" -or ([System.IO.Path]::GetFileName($runner)) -eq "powershell.exe") {
1605+
$runnerParameter=" -f"
1606+
}
1607+
}
1608+
elseif ("$fileFullName".ToLower().EndsWith(".sh")) {
1609+
$runner="bash"
1610+
}
1611+
else {
1612+
$err="[Run-Remote-Script] Abort. Unable to infer runner for remote script '$url', file is '$fileFullName'"
1613+
Write-Line -TextRed "$err"
1614+
throw $err;
1615+
}
1616+
}
1617+
Write-Line "Invoking " -TextCyan "$runner$runnerParameter" " " -TextGreen "$url" " " -TextGreen "$passthrowArgs"
1618+
# Write-Host "RUNNER: [$runner]"
1619+
# Write-Host "URL: [$url]"
1620+
# Write-Host "Arguments (Count = $($passthrowArgs.Length)): $passthrowArgs"
1621+
# Write-Host "Local File: $fileFullName"
1622+
$okDownload = Download-File-Managed "$url" "$fileFullName"
1623+
# Write-Host "okDownload: $okDownload"
1624+
if ($okDownload) {
1625+
$allWorld = $runner.Split(" ") + @($runnerParameter) + @($fileFullName) + $passthrowArgs;
1626+
$allWorld = @($allWorld | ? { "$_" })
1627+
& $allWorld[0] @($allWorld | Select-Object -Skip 1) | Out-Host
1628+
if (-not $?) {
1629+
Write-Line -TextRed "Run-Remote-Script FAIL: '$url'"
1630+
}
1631+
}
1632+
}
1633+
15571634
# Include File: [\Includes\Say.ps1]
15581635
function Say { # param( [string] $message )
15591636
if ($Global:_Say_Stopwatch -eq $null) { $Global:_Say_Stopwatch = [System.Diagnostics.Stopwatch]::StartNew(); }

0 commit comments

Comments
 (0)