Hi all I’d like to copy a file to a computer and remote into said machine
and run a single command then gather the output file of that command. I’ve
been tasked to use Powershell to copy and remote into the machines.
I use this page to help start me out:
http://www.codeproject.com/Articles/18229/How-to-run-PowerShell-scripts-from-C
The program I’m using is .net 4 and has to stay that way. I first installed the windows sdk 3.5 then when the code didnt work I installed sdk 4.0 but I still get
the same error in my answer string: “Mixed mode assembly is built against version ‘v2.0.50727’ of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.\r\n”
Thanks for the help
Casey
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Import-Module BitsTransfer" + Environment.NewLine + "Start-BitsTransfer -Source " + filename + " -Destination \\\\" + host + "\\" + uploadtodir + "\\"+Environment.NewLine);
// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = new Collection<PSObject>();
try
{
results = pipeline.Invoke();
}
catch (Exception ex)
{
results.Add(new PSObject((object)ex.Message));
}
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
string answer= stringBuilder.ToString();
In the App.config file of the project you need to add this:
If you don’t have an App.config file just create one.
Hope this helps you.