简体   繁体   中英

How to run a powershell command in c#

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.

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.

In case anyone wants to know, you need to install the following NuGet packages to use PowerShell in C#:

  • Microsoft.PowerShell.SDK
  • System.Management.Automation

Thanks how I was able to get Jack J Jun's answer to work.

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?

  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? So passes a username field to the script and then checks if they have ActiveSync enabled. 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.

For reference command I ran with old ASP code was:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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