从C#执行Powershell [英] Executing Powershell from C#

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

问题描述

嗨偷看

首先,我对C#领域是全新的:-)

First of all I am brand new to the world of C# :-)

我试图通过此链接创建一个简单的Web应用程序:

I am trying to create a simple Web application from this link:

http://jeffmurr.com/blog/?p=142

http://jeffmurr.com/blog/?p=142

我可以正常使用它,但是我想对其进行更改,因此它实际上使用我在输入框中输入的文字来调用本地Powershell脚本.

I have no problem getting this to work, but l would like to change it so it actually calls a local Powershell script with the text I write in the input box.

我可以调用本地PS1脚本,但无法解析参数/参数.

I am able to call a local PS1 script, but can't get it to parse the parameter/argument.

我已更改:

shell.Commands.AddScript(Input.Text);

收件人

shell.Commands.AddScript("C:\\Scripts\\Test_ny.ps1");
shell.Commands.AddArgument(Input.Text);

我的PS1做到了

$inputServer = $($args[0])
Invoke-Command -ComputerName $inputServer {Get-ChildItem "C:\Users"} -ArgumentList $args[0] | Out-String

如果我从提示符下手动运行脚本,它将起作用.如果我对$ inputServer = server.domain.local进行硬编码,则可以在Web应用程序中使用.

If I run the script manually from prompt it works. If I hardcode $inputServer = server.domain.local it works from the web application.

希望这是有道理的:-)

Hopefully this makes sense :-)

拉瑟

/Lasse

推荐答案

等等,您想用C#无法完成的PowerShell脚本完成什么任务?如果您可以轻松地用C#编写代码,则无需调用PowerShell脚本.

Wait, what is the task that you want to achieve in PowerShell script the C# cannot achieve? If you can code that in C# easily then don't need to call PowerShell script.

如果您确实需要这样做,请参阅

If you really need to do that then refer to

http://blogs.msdn .com/b/kebab/archive/2014/04/28/executing-powershell-scripts-from-c.aspx

chanmm

chanmm


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

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