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

问题描述

我看过很多关于在C#中运行Powershell的帖子,并且在我的帐户下运行时代码运行良好。我想要做的是将我的代码放在监控服务中并使其仍然有效。监视器服务作为本地系统运行,然后各个监视器使用指定的日程运行,这允许我们使用相同的监视器服务来监视跨域。

I have seen a lot of posts on running Powershell in C# and have code that is working great when run under my account. What I'm wanting to do though is put my code in a monitoring service and have it still work. The monitor service run as Local System then the individual monitors run with specified crendials which allows us to use the same monitor service to monitor across domains.

有没有人知道如何在C#中使用备用凭据运行Powershell?

Does anyone here know how I could run Powershell in C# with alternate credentials?

谢谢,

Dave

推荐答案

您只需将您的连接信息作为WSMANConnectionInfo或runspaceconnectioninfo传递:

You just have to pass on your connection information as WSMANConnectionInfo or as runspaceconnectioninfo :

https://msdn.microsoft.com/ en-us / library / system.management.automation.runspaces.runspaceconnectioninfo.credential%28v = vs.85%29.aspx

https://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspaceconnectioninfo.credential%28v=vs.85%29.aspx

PSCredential credential = new PSCredential("username","passowrdassecurestring");
            WSManConnectionInfo Informations = new WSManConnectionInfo(
            new Uri("connectoinurl"),
            "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
            credential);

只需将密码作为securestring传递(将字符串转换为字符):)

Just pass your password as securestring (convert string to chars) :)

问候,

Mounir


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

09-03 04:00