Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用模拟时不允许请求的注册表访问_C#_Powershell_Impersonation - Fatal编程技术网

C# 使用模拟时不允许请求的注册表访问

C# 使用模拟时不允许请求的注册表访问,c#,powershell,impersonation,C#,Powershell,Impersonation,我正在编辑一个用于在文件服务器上创建项目文件夹的网站。 由于没有人使用C#那么多,我们希望进行更改,以便它只执行一个PowerShell脚本,以便在将来需要更改任何内容时对我们更简单 起初,我在执行时遇到了这个错误 不允许请求的注册表访问 然后我读了一遍,然后像上面说的那样添加了运行空间。这消除了这个问题,当我在我的普通用户帐户已经可以通过资源管理器访问的地方创建文件夹时,它开始工作 但当我尝试在只有我用于模拟的帐户有权创建文件夹的位置创建文件夹时,它不会创建文件夹 代码如下: protecte

我正在编辑一个用于在文件服务器上创建项目文件夹的网站。 由于没有人使用C#那么多,我们希望进行更改,以便它只执行一个PowerShell脚本,以便在将来需要更改任何内容时对我们更简单

起初,我在执行时遇到了这个错误

不允许请求的注册表访问

然后我读了一遍,然后像上面说的那样添加了运行空间。这消除了这个问题,当我在我的普通用户帐户已经可以通过资源管理器访问的地方创建文件夹时,它开始工作

但当我尝试在只有我用于模拟的帐户有权创建文件夹的位置创建文件夹时,它不会创建文件夹

代码如下:

protected void Timer1_Tick(object sender, EventArgs e)
{
    try
    {
        //Impersonator imp = new Impersonator("username", "domain", "Password");
        string server = DropDownListSite.SelectedValue + @"FIL001";
        string company = DropDownListCompanies.SelectedValue;
        string site = DropDownListSite.SelectedValue;
        string PX = TextBoxPX.Text;
        string description = TextBoxDescription.Text;

        string vartDescription = "'" + TextBoxDescription.Text + "'";
        // Initialize PowerShell engine
        var shell = PowerShell.Create();
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();
        shell.Runspace = runspace;

        // Add the script to the PowerShell object
        Impersonator imp = new Impersonator("username", "domain", "Password");
        shell.Commands.AddScript("C:\\inetpub\\wwwroot\\ITAdmin\\Scripts\\SkapaUppdrag.ps1 -Bolag " + DropDownListCompanies.Text + " -Site " + DropDownListSite.Text + " -PX " + TextBoxPX.Text + " -Description " + vartDescription);

        // Execute the script
        var results = shell.Invoke();

        // display results, with BaseObject converted to string
        // Note : use |out-string for console-like output
        if (results.Count > 0)
        {
            // We use a string builder ton create our result text
            var builder = new StringBuilder();

            foreach (var psObject in results)
            {
                // Convert the Base Object to a string and append it to the string builder.
                // Add \r\n for line breaks
                builder.Append(psObject.BaseObject.ToString() + "\r\n");
            }

            // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
            LabelStatus.Text = Server.HtmlEncode(builder.ToString());
        }
    }
    catch (Exception ex)
protectedvoid Timer1\u Tick(对象发送方,事件参数e)
{
尝试
{
//模拟者imp=新模拟者(“用户名”、“域”、“密码”);
string server=DropDownListSite.SelectedValue+@“FIL001”;
string company=dropdownlistcompanys.SelectedValue;
字符串site=DropDownListSite.SelectedValue;
字符串PX=TextBoxPX.Text;
字符串描述=TextBoxDescription.Text;
字符串vartDescription=“”+TextBoxDescription.Text+“”;
//初始化PowerShell引擎
var shell=PowerShell.Create();
Runspace Runspace=RunspaceFactory.CreateRunspace();
Open();
shell.Runspace=运行空间;
//将脚本添加到PowerShell对象
模拟者imp=新模拟者(“用户名”、“域”、“密码”);
shell.Commands.AddScript(“C:\\inetpub\\wwwroot\\ITAdmin\\Scripts\\SkapaUppdrag.ps1-Bolag”+dropdownlistcompanys.Text+“-Site”+DropDownListSite.Text+“-PX”+TextBoxPX.Text+“-Description”+vartDescription);
//执行脚本
var results=shell.Invoke();
//显示结果,将BaseObject转换为字符串
//注意:对于类似控制台的输出,使用| out字符串
如果(results.Count>0)
{
//我们使用字符串生成器来创建结果文本
var builder=新的StringBuilder();
foreach(结果中的var psObject)
{
//将基础对象转换为字符串并将其附加到字符串生成器。
//为换行符添加\r\n
Append(psObject.BaseObject.ToString()+“\r\n”);
}
//用HTML编码字符串(防止出现“危险”字符的安全问题,如<>
LabelStatus.Text=Server.HtmlEncode(builder.ToString());
}
}
捕获(例外情况除外)

“它不起作用”这不是一个问题描述。到底是什么不起作用?如果你想知道到底出了什么问题,我建议你去掉try/catch块。我不会在文件服务器上创建文件夹,因为我的登录用户无权创建文件夹。模拟用户AllThrough拥有该访问权限,因此它不会使用Impersonated user来创建文件夹。我已将$env:Username | Out字符串添加到powershell脚本中,以查看谁在运行该脚本,并显示与我一起登录到计算机的用户,因此它不会以模拟用户的身份运行该脚本,对吗?假设是这样,我希望在具有不同帐户的共享上创建文件夹(服务帐户)这是唯一允许在共享上创建文件夹的人,我希望c#使用此服务帐户的凭据运行powershell脚本,以便我可以轻松添加它将来应该做的其他事情,正常帐户不允许做,但此服务帐户是。