So I'm trying to run a Powershell script from C#
string text = System.IO.File.ReadAllText(@"C:\Users\nameofuser\Desktop\script.ps1");
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript(text);
PowerShellInstance.Invoke();
if (PowerShellInstance.Streams.Error.Count > 0)
{
Console.Write("Error");
}
Console.ReadKey();
This is the Script File
$username = "xxx"
$password = "xxx"
$server = "xxx"
$database = "xxx"
$currentuser = "xxx"
$homepath = "C:\Users\$currentuser\Desktop"
mkdir "$homepath\csvs"
mkdir "$homepath\csvs\$database"
$AttachmentPath = "$homepath\csvs\$database\name.csv"
$QueryFmt = "SELECT * FROM TEST"
Invoke-Sqlcmd -User $username -Password $password -ServerInstance $server -
Database $database -Query $QueryFmt | Export-CSV $AttachmentPath
However, the script only runs up through to line mkdir "$homepath\csvs\$database" and then stops working. In other words, the query inside of the script is not executing.
Any help would be greatly appreciated!