Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca2418d

Browse files
committed
i Use-RunAsTool
See SYNOPSIS/DESCRIPTION
1 parent c62dc9c commit ca2418d

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
function Use-RunAsTool {
2+
<#
3+
.SYNOPSIS
4+
Downloads, extracts, and manages the RunAsTool application.
5+
6+
.DESCRIPTION
7+
This function downloads and extracts RunAsTool.exe. After extraction, it supports importing settings via command-line arguments.
8+
9+
.EXAMPLE
10+
Use-RunAsTool -StartApplication
11+
Use-RunAsTool -ImportFile "C:\Path\To\Import.rnt" -Admin "AdminAccount" -Password "AdminPassword"
12+
Use-RunAsTool -StartApplication -ImportFile "C:\Path\To\Import.rnt" -Admin "AdminAccount" -Password "AdminPassword"
13+
14+
.NOTES
15+
v0.0.1
16+
#>
17+
[CmdletBinding()]
18+
param (
19+
[Parameter(Mandatory = $false, HelpMessage = "URL for downloading the RunAsTool")]
20+
[uri]$RunAsToolDownloadUrl = "https://www.sordum.org/files/download/runastool/RunAsTool.zip",
21+
22+
[Parameter(Mandatory = $false, HelpMessage = "Path to the directory where the RunAsTool will be downloaded and extracted")]
23+
[string]$DownloadPath = "$env:TEMP\RunAsTool",
24+
25+
[Parameter(Mandatory = $false, HelpMessage = "Remove the temporary folder after the operation")]
26+
[switch]$RemoveRunAsTool,
27+
28+
[Parameter(Mandatory = $false, HelpMessage = "Start the RunAsTool application after extraction")]
29+
[switch]$StartApplication,
30+
31+
[Parameter(Mandatory = $false, HelpMessage = "Path to the import file")]
32+
[string]$ImportFile,
33+
34+
[Parameter(Mandatory = $false, HelpMessage = "Admin account name for importing settings")]
35+
[string]$Admin,
36+
37+
[Parameter(Mandatory = $false, HelpMessage = "Admin password for importing settings")]
38+
[string]$Pass,
39+
40+
[Parameter(Mandatory = $false, HelpMessage = "Reset previous list during import")]
41+
[switch]$Reset
42+
)
43+
$RunAsToolZipPath = Join-Path $DownloadPath "RunAsTool.zip"
44+
$RunAsToolExtractPath = Join-Path $DownloadPath "RunAsTool"
45+
try {
46+
Write-Host "Creating download directory..." -ForegroundColor Green
47+
New-Item -Path $DownloadPath -ItemType Directory -Force | Out-Null
48+
if (!(Test-Path -Path $RunAsToolZipPath)) {
49+
Write-Host "Downloading RunAsTool..." -ForegroundColor Green
50+
Invoke-WebRequest -Uri $RunAsToolDownloadUrl -OutFile $RunAsToolZipPath -UseBasicParsing -Verbose
51+
if ((Get-Item $RunAsToolZipPath).Length -eq 0) {
52+
throw "The downloaded ZIP file is empty or corrupt."
53+
}
54+
}
55+
Write-Host "Extracting RunAsTool..." -ForegroundColor Green
56+
if (Test-Path -Path $RunAsToolExtractPath) {
57+
Remove-Item -Path $RunAsToolExtractPath -Recurse -Force
58+
}
59+
try {
60+
[System.IO.Compression.ZipFile]::ExtractToDirectory($RunAsToolZipPath, $DownloadPath)
61+
}
62+
catch {
63+
throw "Failed to extract the ZIP file. It may be corrupt or incomplete."
64+
}
65+
$RunAsToolExecutable = Get-ChildItem -Path $RunAsToolExtractPath -Recurse -Filter "RunAsTool.exe" | Select-Object -First 1
66+
if (-Not $RunAsToolExecutable) {
67+
throw "RunAsTool.exe not found in $RunAsToolExtractPath"
68+
}
69+
Write-Verbose -Message "RunAsTool extracted to: $($RunAsToolExecutable.FullName)"
70+
if ($ImportFile) {
71+
$Arguments = "/U=$Admin /P=$Pass /I=$ImportFile"
72+
if ($Reset) {
73+
$Arguments += " /R"
74+
}
75+
Write-Verbose -Message "Importing settings with arguments: $Arguments"
76+
Start-Process -FilePath $RunAsToolExecutable.FullName -ArgumentList $Arguments -Wait
77+
}
78+
if ($StartApplication) {
79+
Write-Host "Starting RunAsTool..." -ForegroundColor Green
80+
Start-Process -FilePath $RunAsToolExecutable.FullName
81+
}
82+
}
83+
catch {
84+
Write-Error -Message "An error occurred: $_"
85+
}
86+
finally {
87+
Write-Host "RunAsTool operation completed." -ForegroundColor Cyan
88+
if ($RemoveRunAsTool) {
89+
Write-Warning -Message "Cleaning up, removing the temporary folder..."
90+
Remove-Item -Path $DownloadPath -Force -Recurse -Verbose
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)