从C#运行Powershell [英] Run Powershell from C#

查看:225
本文介绍了从C#运行Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从C#启动Powershell脚本,并在管道上获取PSSSecurityException.Invoke()

I need to start a powershell script from C# and get the PSSSecurityException on pipeline.Invoke()

AuthorizationManager检查失败.

AuthorizationManager check failed.

我的代码:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    Command scriptCommand = new Command(scriptfile);
    pipeline.Commands.Add(scriptCommand);
    pipeline.Invoke();
}

问题

  1. 我怀疑需要设置PSCredential.但是我无法对此进行提示,因此我必须在代码中进行处理.可以安全的方式进行吗? (情况并非如此)

推荐答案

问题是脚本已放置在共享上.要在此共享上运行脚本,我需要将该共享添加到受信任的站点".

The problem was that the script was placed on a share. To run scripts on this share I needed to add the share to Trusted Sites.

这篇关于从C#运行Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆