Hi - I have an application written in c# that executes PowerShell scripts. It performs well except when I try to write out database query results to an Excel worksheet. I am not sure at all what is preventing this.
if (!fi.Exists) { string smsg = string.Format("Script file {0} does not exist.\r\n\r\n Action can't be completed.",scriptfile);
MessageBox.Show(smsg, Application.ProductName); return; } using (new sb.Impersonator("username", "domain", "password")) { RunspaceConfiguration config = RunspaceConfiguration.Create();
//create powershell runspace Runspace cmdlet = RunspaceFactory.CreateRunspace(config);
cmdlet.ApartmentState = System.Threading.ApartmentState.STA; cmdlet.ThreadOptions = PSThreadOptions.UseCurrentThread;
cmdlet.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(cmdlet);
scriptInvoker.Invoke("Set-ExecutionPolicy RemoteSigned -Scope CurrentUser");
// create a pipeline and load it with command object pipeline = cmdlet.CreatePipeline();
Command cmd = new Command(scriptfile);
if (paramList.Length > 0) { foreach (object prm in paramList) { cmd.Parameters.Add(prm.ToString()); } }
pipeline.Commands.Add(cmd);
pipeline.Commands.Add("Out-String");
// this will format the output IEnumerable<PSObject> output = pipeline.Invoke();
// process each object in the output and append to stringbuilder foreach (PSObject obj in output) { results.AppendLine(obj.ToString()); } }
$app.Visible = $false
$wb = $app.Workbooks.Add()
$ws1 = $wb.WorkSheets.Item(1)$ws1.Name = "File Audit"
$wb.ActiveSheet.Paste($rng, $false)