阻止C#服务创建用户个人资料

时间:2018-08-01 17:59:02

标签: c#

我创建了一个Windows服务,该服务使用WINRM在远程系统上运行powershell脚本。

问题在于,当它连接到远程计算机时,它正在创建用户配置文件,就像用户正在交互式登录一样。它在注册表列表中创建了一个新的配置文件,并创建了用户配置文件文件夹,文档,桌面等。

如何防止创建此配置文件?

这是我的代码:

 WSManConnectionInfo connectionInfo = new WSManConnectionInfo()
   {
      ComputerName = this.HostName
   };

 using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
    {
    using (PowerShell ps = PowerShell.Create(RunspaceMode.NewRunspace))
    {
        ps.Runspace = runspace;

        var cmdText = File.ReadAllText($"{pathToScripts}\\{script.name}.ps1");

            if (ps.Runspace.RunspaceStateInfo.State != RunspaceState.Broken)
            {
                ps.AddScript(cmdText);
                PSDataCollection<PSObject> outputCollection =
                    new PowerShellHelper(ps, this.Logger, this.HostName)
                        .ExecuteAsynchronously(new TimeSpan(0, 2, 0));
                var dt = ConvertToDataTable(outputCollection, script.name);
                if (dt.Rows.Count > 0)
                {
                    new DataBaseUpdater(this.Logger, this.Zone, this.ConString,
                        this.HostName).Update(script.name, dt);
                }
            }
            else
            {

                this.Logger.Error(
                    $"[{System.Reflection.MethodBase.GetCurrentMethod()}]:[{this.HostName}]: Runspace State:{runspace.RunspaceStateInfo.State.ToString()}");
            }

        else
        {
            this.Logger.Error(
                $"Output for {script.name} does not contain rec_num field. Ensure that your powershell script is outputing this field.");
            break;
            //statement.Break();
        }
        if (ps.Runspace.RunspaceStateInfo.State != RunspaceState.Closed)
        {
            ps.Runspace.CloseAsync();
            ps.Runspace.Close();
            ps.Runspace.Dispose();
     }
 }}}

1 个答案:

答案 0 :(得分:0)

在创建运行空间之前,在connectionInfo对象上设置NoMachineProfile property

connectionInfo.NoMachineProfile = true;