简体   繁体   English

如何在 C# 中运行 powershell 命令

[英]How to run a powershell command in c#

So I am attempting to change the creation date of a text file in C#.所以我试图在 C# 中更改文本文件的创建日期。 The user will type in thr creation date of the file, it will then change the text files creation date to what the user inputted.用户将输入文件的创建日期,然后它将文本文件的创建日期更改为用户输入的日期。 Trouble is it keeps adding a ' for some reason resulting in an error message, it is calling on Powershell to accomplish this:问题是由于某种原因它不断添加一个 ' 导致错误消息,它正在调用 Powershell 来完成此操作:

    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dir = textBox1.Text;

            PowerShell ps = PowerShell.Create();



          ps.AddCommand("Get-ChildItem c:\\encryptedmessagehere.txt | % {$_.CreationTime = '"+ dir + "'}");

            ps.Invoke();
        }
    }
}

Trouble is, after the "'}");, it automatically adds another ', incorrecting the powershell command to change the date.麻烦的是,在 "'}"); 之后,它会自动添加另一个 ',从而错误地使用 powershell 命令更改日期。 Is there a way to stop it from adding the ' at the end?有没有办法阻止它在末尾添加'?

The returned error is:返回的错误是:

System.Management.Automation.CommandNotFoundException HResult=0x80131501 Message=The term 'Get - ChildItem C:\\encryptedmessagehere.txt | System.Management.Automation.CommandNotFoundException HResult=0x80131501 Message=术语'Get - ChildItem C:\\encryptedmessagehere.txt | % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' is not recognized as the name of a cmdlet, function, script file, or operable program. % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

Thank you.谢谢你。

So I am attempting to change the creation date of a text file in C#.所以我试图在 C# 中更改文本文件的创建日期。 The user will type in thr creation date of the file, it will then change the text files creation date to what the user inputted.用户将输入文件的创建日期,然后将文本文件的创建日期更改为用户输入的日期。 Trouble is it keeps adding a ' for some reason resulting in an error message, it is calling on Powershell to accomplish this:问题是它由于某种原因不断添加 ' 导致错误消息,它正在调用 Powershell 来完成此操作:

    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dir = textBox1.Text;

            PowerShell ps = PowerShell.Create();



          ps.AddCommand("Get-ChildItem c:\\encryptedmessagehere.txt | % {$_.CreationTime = '"+ dir + "'}");

            ps.Invoke();
        }
    }
}

Trouble is, after the "'}");, it automatically adds another ', incorrecting the powershell command to change the date.麻烦的是,在"'}"); 之后,它会自动添加另一个',从而错误地更改了powershell 命令以更改日期。 Is there a way to stop it from adding the ' at the end?有没有办法阻止它在末尾添加 '?

The returned error is:返回的错误是:

System.Management.Automation.CommandNotFoundException HResult=0x80131501 Message=The term 'Get - ChildItem C:\\encryptedmessagehere.txt | System.Management.Automation.CommandNotFoundException HResult=0x80131501 Message=术语“Get - ChildItem C:\\encryptedmessagehere.txt | % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' is not recognized as the name of a cmdlet, function, script file, or operable program. % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' 不是 cmdlet、函数、脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

Thank you.谢谢你。

In case anyone wants to know, you need to install the following NuGet packages to use PowerShell in C#:如果有人想知道,您需要安装以下 NuGet 包才能在 C# 中使用 PowerShell:

  • Microsoft.PowerShell.SDK Microsoft.PowerShell.SDK
  • System.Management.Automation系统管理自动化

Thanks how I was able to get Jack J Jun's answer to work.感谢我如何能够得到 Jack J Jun 的工作答案。

Can't comment due to points so can only reply.由于积分无法发表评论,因此只能回复。 For Jack's answer, great info.对于杰克的回答,很好的信息。 Two items:两项:

  1. Is there a way to get a return value if say Powershell would be returning True or False?如果说 Powershell 将返回 True 或 False,有没有办法获得返回值?

  2. If I wanted to do the below that is in a ps1 file now (prefer not to have to call the file), how would I tweak the above for that?如果我现在想在 ps1 文件中执行以下操作(宁愿不必调用该文件),我将如何为此调整上述内容? So passes a username field to the script and then checks if they have ActiveSync enabled.因此,将用户名字段传递给脚本,然后检查它们是否启用了 ActiveSync。 Returns True of False.返回真假。

 param ($username) Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 (Get-CASMailbox -Identity enhnet\\$username).ActiveSyncEnabled

I did previously append我以前确实附加过

 | out-file -Encoding ASCII c:\inetpub\wwwroot\results.txt

With ASP 2.0 in the PS1 file then read the file and give the result but that was kind of clugy.在 PS1 文件中使用 ASP 2.0 然后读取文件并给出结果,但这有点麻烦。

For reference command I ran with old ASP code was:对于我使用旧 ASP 代码运行的参考命令是:

strCMD="powershell -command ""& {c:\temp\activesync.ps1 " & username & "} "" "
objShell.Run strCMD,0,true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM