Wie führe ich in C#ein PowerShell -Skript aus und erfasse die Ausgabe (keine Eingabeaufforderungen/Passwort).C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Wie führe ich in C#ein PowerShell -Skript aus und erfasse die Ausgabe (keine Eingabeaufforderungen/Passwort).

Post by Anonymous »

Ich schreibe ein Tool für mein Team, um eine Liste von Aufgaben in einer JSON -Datei auszuführen. Nur funktioniert es, wenn es erhöht ist. /> CLIWRAP funktioniert jedoch nicht mit erhöhten Prozessen.- command: Powershell
- arguments: -ExecutionPolicy Bypass "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1"'
- verb: RunAs"
< /code>
Das gleiche wie oben, aber hinzufügen "-wait" zu den Argumenten. < /p>
- command: Powershell
- arguments: -ExecutionPolicy Bypass "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1" -Wait'
- verb: RunAs"
< /code>
Umleitung der Ausgabe in eine Datei, damit ich sie lesen und ihre Inhalte zurückgeben kann. < /p>
- command: Powershell
- arguments: -ExecutionPolicy Bypass "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1" -Wait' > Output.log 2>1
- verb: RunAs"
< /code>
all dies, während die Runas auf einen sekundären Aufruf von PowerShell verschoben werden < /p>
- command: Powershell
- arguments: -ExecutionPolicy Bypass -Command "Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1"' -Verb RunAs"
< /code>
mit dem -wait < /p>
- command: Powershell
- arguments: -ExecutionPolicy Bypass -Command "Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1"' -Verb RunAs -Wait"
< /code>
mit Umleitung < /p>
- command: Powershell
- arguments: -ExecutionPolicy Bypass -Command "Start-Process powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1"' -Verb RunAs -Wait > "C:\Users\dennisl\AppData\Local\Temp\powershell_output_75b92c48-6f18-4e0d-ad92-e2259b5f154e.txt" 2>&1"
< /code>
Ich habe versucht, eine Stapeldatei zu erstellen und sie auszuführen < /p>
@echo off
powershell -ExecutionPolicy Bypass "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1" -Wait > Output.log 2>1
exit /b %ERRORLEVEL%
< /code>
- command: cmd.exe
- arguments: BatchFile.cmd
- verb: runAs
< /code>
With embedded runAs
@echo off
powershell -ExecutionPolicy Bypass "C:\Program Files\TaskRx\Scripts\InstallWinget.ps1" -Verb runAs -Wait > Output.log 2>1
exit /b %ERRORLEVEL%
< /code>
- command: cmd.exe
- arguments: BatchFile.cmd
< /code>
With two levels of Powershell
@echo off
powershell.exe -ExecutionPolicy Bypass -Command "Start-Process powershell.exe -ArgumentList \"-ExecutionPolicy Bypass -File 'C:\Program Files\TaskRx\Scripts\InstallWinget.ps1' -Verb RunAs -Wait\" > Output.log 2>&1"
exit /b %ERRORLEVEL%
< /code>
- command: cmd.exe
- arguments: BatchFile.cmd
< /code>
For all of these I see:
  • The elevated PowerShell screen flashes on the screen momentarily and then vanishes.
  • The return code from the process is 0
  • The output is NULL
I'm sure the elevated process is not running because I've added delays and input requests into my InstallWinget.ps1 script that I never saw.
Also, with redirection, the output file is not created.
BTW, here is the InstallWinget.ps1 script. Note that it provides output right away regarding whether elevated or not.
# Ensure running as Administrator
if ([System.Security.Principal.WindowsIdentity]::GetCurrent().IsSystem) {
Write-Host "Running as SYSTEM (admin)"
} else {
Write-Host "Running as normal user"
}
$adminCheck = [System.Security.Principal.WindowsPrincipal] [System.Security.Principal.WindowsIdentity]::GetCurrent()
if (-not $adminCheck.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Output "This script must be run as Administrator. Exiting..."
exit 1
}

Write-Output "Running as Administrator ... Check!"

# Define the download path in the user's Downloads folder
$downloadPath = [System.IO.Path]::Combine([System.Environment]::GetFolderPath("UserProfile"), "Downloads", "AppInstaller.msixbundle")

# Check if Winget is installed
$wingetInstalled = $false
try {
$installedVersion = winget --version
Write-Output "Installed Winget version: $installedVersion"
$wingetInstalled = $true
Write-Output "Winget version $installedVersion installed!"
} catch {
$installedVersion = "0.0.0" # Default if not installed
Write-Output "Winget not installed!"
}

# If Winget is already installed, check for the latest version
$updateRequired = $false
if ($wingetInstalled) {
try {
$latestVersion = (Invoke-RestMethod -Uri "https://api.github.com/repos/microsoft/ ... ses/latest").tag_name -replace "[^0-9\.]"
Write-Output "Latest available Winget version: $latestVersion"

if ($installedVersion -lt $latestVersion) {
Write-Output "A newer version of Winget is available."
$updateRequired = $true
} else {
Write-Output "Winget is already up to date!"
exit 0
}
} catch {
Write-Output "Failed to fetch the latest Winget version. Skipping update check."
exit 2
}
}

# Download and install/update Winget (App Installer)
Write-Output "Downloading Winget installer to: $downloadPath"
Invoke-WebRequest -Uri "https://aka.ms/getwinget" -OutFile $downloadPath -UseBasicParsing
Add-AppxPackage -Path $downloadPath -ForceUpdate

# Display a clear final message
if (-not $wingetInstalled) {
Write-Output "Winget has been successfully installed!"
} elseif ($updateRequired) {
Write-Output "Winget has been successfully updated to version $latestVersion!"
} else {
Write-Output "Winget was already up to date. No changes were made."
}
< /code>
Does anyone know how to do this? I would be very appreciative of a solution.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post