使用C#中的powershell脚本访问主机文件

本文关键字:访问 主机 文件 脚本 powershell 中的 使用 | 更新日期: 2024-10-21 08:01:00

我有一个ASP.NETWEbForms项目,我想执行powershell脚本来更新hosts文件。

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)
        {
        }
    }
}

有一个脚本:

 $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";

我尝试了以下操作:Invoke(Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted);

则将CCD_ 3粘贴在脚本的开头。使用此Set-ExecutionPolicy Unrestricted-相同且相同的错误。拒绝访问路径"C:''Windows''System32''drivers''etc''hosts"

如果我运行控制台应用程序,脚本将完美运行。

更新:我正在以管理员身份运行Visual Studio。

更新2:好的,现在我正在使用ImpersonatedUser,但发生了另一个异常。"不允许请求的注册表访问。"

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)

在System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection 1 input, PSDataCollection 1输出,

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;
        }
    }
}

使用C#中的powershell脚本访问主机文件

您的ASP.NET使用应用程序池的工作进程的凭据执行PowerShell脚本,该进程可能不是管理帐户(除非您更改了它)。

修改主机文件仅限于管理帐户,在更改工作进程的凭据之前,您应该仔细考虑。

如果您想进行此更改,请按照此处的说明进行操作:https://technet.microsoft.com/en-us/library/cc771170(v=ws.10).aspx

同样,此更改可能会使您的应用程序更容易受到安全漏洞攻击(因为在应用程序中发现的任何漏洞都可以使用管理权限)。

如果UAC(用户帐户控制)打开,您可能还需要关闭它。

另一种方法是使用模拟来临时提升您的特权。你可以在这里看到一个类的示例,它允许你这样做(包装每件事):https://blogs.msdn.microsoft.com/joncole/2009/09/21/impersonation-code-in-c/

希望这能有所帮助。

我找到了其他解决方案,但可能不是最好的。在IIS服务器中,只需添加一个新的应用程序池。在高级设置中,将标识更改为自定义帐户,并在窗口中输入凭据。将新的应用程序池用于站点。