如何在 C# 中运行 powershell 命令 [英] How to run a powershell command in c#

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

问题描述

所以我试图在 C# 中更改文本文件的创建日期.用户将输入文件的创建日期,然后将文本文件的创建日期更改为用户输入的日期.问题是它由于某种原因不断添加 ' 导致错误消息,它正在调用 Powershell 来完成此操作:

 公共部分类 Form6 : Form{公共 Form6(){初始化组件();}private void textBox1_TextChanged(对象发送者,EventArgs e){}private void button1_Click(object sender, EventArgs e){字符串目录 = textBox1.Text;PowerShell ps = PowerShell.Create();ps.AddCommand("Get-ChildItem c:\\encryptedmessagehere.txt | % {$_.CreationTime = '"+ dir + "'}");ps.调用();}}}

问题是,在'}"); 之后,它会自动添加另一个 ',从而错误地更改了 powershell 命令以更改日期.有没有办法阻止它在末尾添加 '?

返回的错误是:

System.Management.Automation.CommandNotFoundExceptionHResult=0x80131501Message=术语'Get - ChildItem C:\encryptedmessagehere.txt |% {$_.CreationTime = '06/12/12 09:27:03 AM'} ' 不是 cmdlet、函数、脚本文件或可运行程序的名称.检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试.

谢谢.

解决方案

首先,您需要使用 Powersheel.AddScript 方法而不是 Powersheel.AddCommand 方法.>

其次,可以试试下面的Powershell代码来改变文件的创建时间.

(Get-ChildItem d:\Test\new.txt).CreationTime = '2020/09/23'

最后可以试试下面的c#代码示例,在c#中调用powershell代码.

private void button1_Click(object sender, EventArgs e){字符串日期时间= textBox1.Text;PowerShell ps = PowerShell.Create();string script = string.Format("(Get-ChildItem d:\\New.txt).CreationTime = '{0}'", datetime);ps.AddScript(脚本);ps.调用();MessageBox.Show(测试");}

So I am attempting to change the creation date of a text file in 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:

    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. 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 | % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Thank you.

解决方案

First, you need to use Powersheel.AddScript method instead of Powersheel.AddCommand method.

Second, you can try the following Powershell code to change the creation time of the file.

(Get-ChildItem d:\Test\new.txt).CreationTime =  '2020/09/23'

Finally, you can try the following c# code example to call powershell code in c#.

private void button1_Click(object sender, EventArgs e)
        {
            string datetime= textBox1.Text;
            PowerShell ps = PowerShell.Create();
            string script = string.Format("(Get-ChildItem d:\\New.txt).CreationTime = '{0}'", datetime);
            ps.AddScript(script);
            ps.Invoke();
            MessageBox.Show("Test");
        }

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

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