-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunAsCommand.ps1
More file actions
74 lines (64 loc) · 2.62 KB
/
RunAsCommand.ps1
File metadata and controls
74 lines (64 loc) · 2.62 KB
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
param(
[string]$Username, # 欲以 runas 執行的使用者帳號,例如 ".\AdminUser"
[string]$Password, # 該帳號的密碼
[string]$Command # 欲執行的 CMD 指令,例如 "dir C:\Windows"
)
# 指定使用目前使用者的臨時資料夾
$sharedTempFolder = $env:TEMP
if (-not (Test-Path $sharedTempFolder)) {
New-Item -Path $sharedTempFolder -ItemType Directory -Force | Out-Null
}
# 產生唯一檔案名稱(避免與其他執行衝突)
$guid = [guid]::NewGuid().ToString()
$outputFile = Join-Path $sharedTempFolder "out_$guid.txt"
$errorFile = Join-Path $sharedTempFolder "err_$guid.txt"
# 確保檔案存在,若不存在則創建
if (-not (Test-Path $outputFile)) {
New-Item -Path $outputFile -ItemType File -Force | Out-Null
}
if (-not (Test-Path $errorFile)) {
New-Item -Path $errorFile -ItemType File -Force | Out-Null
}
# 組合 cmd 執行命令,並將標準輸出與錯誤輸出導向檔案
$cmdCommand = "cmd /c `"$Command`" > `"$outputFile`" 2> `"$errorFile`""
# 判斷是否需要使用指定帳號
if ([string]::IsNullOrEmpty($Username) -or [string]::IsNullOrEmpty($Password)) {
# 使用目前登入的帳號
$process = Start-Process -FilePath "powershell.exe" `
-ArgumentList "-NoProfile", "-Command", "`"$cmdCommand`"" `
-NoNewWindow `
-PassThru
} else {
# 使用指定帳號
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($Username, $securePassword)
$process = Start-Process -FilePath "powershell.exe" `
-Credential $cred `
-ArgumentList "-NoProfile", "-Command", "`"$cmdCommand`"" `
-NoNewWindow `
-PassThru
}
# 手動等待該進程結束(因為 -Wait 會因權限問題失敗)
while (-not $process.HasExited) {
Start-Sleep -Milliseconds 200
}
# 檢查檔案是否存在
if (-Not (Test-Path $outputFile)) {
Write-Host "⚠️ 標準輸出檔案不存在:$outputFile" -ForegroundColor Red
} else {
# 嘗試讀取並顯示標準輸出與錯誤輸出
try {
Write-Host "`n=== 標準輸出 ==="
Get-Content $outputFile -Raw
$errorText = (Get-Content $errorFile -Raw)
if ($errorText) {
Write-Host "`n=== 錯誤輸出 ===" -ForegroundColor Red
Write-Host $errorText.Trim()
}
} catch {
Write-Host "錯誤訊息:" $_.Exception.Message -ForegroundColor Red
Write-Host "錯誤堆疊:" $_.Exception.StackTrace -ForegroundColor Red
}
}
# 清理暫存檔案
Remove-Item $outputFile, $errorFile -Force -ErrorAction SilentlyContinue