I need to execute a simple PS script that contains a Invoke-Sqlcmd cmdlet from a C# app. When the script is executed through the PS window it works fine. In the C# app nothing happens.
I've tried other scripts from the C# app and got results, but with this specific script something went wrong.
using (var powerShell = PowerShell.Create())
{
powerShell.AddScript(psScript);
powerShell.AddParameter("Username", "user");
powerShell.AddParameter("Password", "password");
powerShell.AddParameter("Server", server);
powerShell.AddParameter("Script", script);
var result = powerShell.Invoke();
}
PS script:
param ([String]$Username, [String]$Password, [String]$Server, [String]$Script)
Import-Module SqlPs
Invoke-Sqlcmd -ServerInstance $Server -Username $Username -Password $Password -Query $Script -QueryTimeout 750 -ConnectionTimeout 600
Does anyone know how to solve the problem?