Created
February 16, 2016 20:28
-
-
Save jdforsythe/d2c666037bcd61547cc0 to your computer and use it in GitHub Desktop.
Execute Powershell Cmdlet in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Management.Automation; | |
namespace ExecutePowershellCmdlet { | |
class Program { | |
using (PowerShell pshell = PowerShell.Create()) { | |
DateTime yesterday = DateTime.Today.AddDays(-1); | |
string date_string = "\"" + yesterday.ToString("MM"_ + "/" + yesterday.ToString("dd") + "/" + yesterday.ToString("yyyy") + "\""; // "MM/dd/yyyy" | |
string name_regex = "'^[0-9]{4,4}\\.dw*$'"; | |
pshell.AddScript("dir \"k:\\jobs\\\" -Directory | Where-Object { $_.LastWriteTime -ge " + date_string + " -and $_.Name -match " + name_reges + " } | Copy-Item -Destination c:\\copy_to -recurse"); | |
pshell.Invoke(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment