Invoke-AsAdmin-Module.psm1
function Invoke-AsAdmin { param( [Parameter()] [ScriptBlock]$Script, [Parameter()] [System.Management.Automation.PSCredential]$Credential ) # Pfad zur temporären Skriptdatei $tempScriptPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName() + ".ps1") # Pfad zur Ausgabe-Textdatei $outputFile = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName() + ".txt") # Inhalt des temporären Skripts $scriptContent = @" Start-Transcript -Path '$outputFile' $($Script.ToString()) Stop-Transcript "@ # Schreiben des Skriptinhalts in die temporäre Datei $scriptContent | Out-File -FilePath $tempScriptPath -Encoding UTF8 # Erstellen des Prozessstart-Info-Objekts $startInfo = New-Object System.Diagnostics.ProcessStartInfo $startInfo.FileName = "powershell.exe" $startInfo.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$tempScriptPath`"" $startInfo.UseShellExecute = $false $startInfo.CreateNoWindow = $true if ($Credential) { $startInfo.UserName = $Credential.UserName $startInfo.Password = $Credential.Password $startInfo.Domain = $Credential.GetNetworkCredential().Domain } $process = [System.Diagnostics.Process]::Start($startInfo) $process.WaitForExit() # Überprüfen, ob die Ausgabedatei erstellt und befüllt wurde if (Test-Path $outputFile) { Write-Host "Ausgabedatei wurde erstellt:" Get-Content $outputFile } else { Write-Host "Ausgabedatei wurde nicht erstellt." } # Löschen des temporären Skripts Remove-Item -Path $tempScriptPath -Force # Löschen der Ausgabedatei, wenn sie erstellt wurde (optional) Remove-Item -Path $outputFile -Force } |