Ich habe ein ASP .NET WEB Forms-Projekt, und ich möchte Power-Shell-Skript zur Aktualisierung hosts
Datei ausführen.Zugriff auf Hosts-Datei mit Powershell-Skript von C#
private void ExecutePowerShellScript(string scriptToExecute)
{
using (PowerShell powershelInstance = PowerShell.Create())
{
var authManger = powershelInstance.Runspace.RunspaceConfiguration.AuthorizationManager;
powershelInstance.AddScript(scriptToExecute);
Collection<PSObject> results = powershelInstance.Invoke();
if (powershelInstance.Streams.Error.Count > 0)
{
throw powershelInstance.Streams.Error[0].Exception;
}
foreach (var result in results)
{
}
}
}
Es ist das Skript:
$hostsPath = "$env:windir\System32\drivers\etc\hosts";
$hosts = get-content $hostsPath;
[System.Collections.ArrayList]$arr = $hosts;
$arr.Add(someValueHere);
$arr | Out-File $hostsPath -enc ascii;
# returns results;
$arr;
# end of the script";
ich dieses versucht: Invoke (Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
);
dann diese Paste Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
am Anfang des Skripts. Verwenden Sie diese Set-ExecutionPolicy Unrestricted
- gleichen und gleichen Fehler. Zugriff auf den Pfad C: \ Windows \ System32 \ drivers \ etc \ hosts 'wird verweigert.
Das Skript funktioniert einwandfrei, wenn ich in der Konsolenanwendung lief.
Update: Ich führe Visual Studio als Administrator.
Update 2: OK, jetzt benutze ich die ImpersonatedUser
, aber eine weitere Ausnahme auftreten. "Der angeforderte Registrierungszugriff ist nicht zulässig."
Stacktrace:
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
at System.Environment.GetEnvironmentVariable(String variable, EnvironmentVariableTarget target)
at System.Management.Automation.ModuleIntrinsics.SetModulePath()
at System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine, PSHost hostInterface)
at System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss)
at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper()
at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall)
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
bei System.Management.Automation.PowerShell.CoreInvoke [TInput, TOutput] (PSDataCollection 1 input, PSDataCollection
1 Ausgang,
using (ImpersonatedUser impersonatedUser = new ImpersonatedUser(username, domain, password))
{
using (PowerShell powershelInstance = PowerShell.Create())
{
powershelInstance.AddScript(scriptToExecute);
//When the .Invoke() method is called, an exception with message "Requested registry access is not allowed." was thrown.
Collection<PSObject> results = powershelInstance.Invoke();
if (powershelInstance.Streams.Error.Count > 0)
{
throw powershelInstance.Streams.Error[0].Exception;
}
}
}
Es sollte eine andere Problemumgehung haben ... – mihkov
Sie können Identitätswechsel verwenden, aber die Risiken sind immer noch die gleichen. Ich habe es meiner Antwort hinzugefügt. –
Ich habe den Beitrag aktualisiert, bitte überprüfen Sie es – mihkov