Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to execute PowerShell 2.0 script from a .NET installer's custom action
    text
    copied!<p>I've been working on this issue for a couple days and have read several posts here, but I can't get my implementation to work. I am calling the powershell script during the Commit custom action. When I get to the pipeline.Invoke() method, I get an exception that says the entire script "is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."</p> <p>Here is my script:</p> <pre><code>Param( [parameter(Mandatory=$true,ValueFromPipeline=$true)] [string]$installPath ); schtasks /create /TN MyTask /RU domain\account /RP password /xml $installPath\MyTaskSchedule.xml; </code></pre> <p>I've tried it with and without the trailing semi-colons, with and without a wrapping function. I've verified that the C# code is passing the correct install path and that the xml file exists in the directory before this step is hit. I can run this from PowerShell itself and it works just fine.</p> <p>Here is my code:</p> <pre><code>public override void Commit( System.Collections.IDictionary savedState ) { base.Commit( savedState ); String targetDirectory = this.Context.Parameters["TDir"].ToString(); String script = System.IO.File.ReadAllText( targetDirectory + "TaskScheduler.ps1" ); RunspaceConfiguration c = RunspaceConfiguration.Create(); using ( Runspace runspace = RunspaceFactory.CreateRunspace() ) { runspace.Open(); using ( Pipeline pipeline = runspace.CreatePipeline() ) { Command myCommand = new Command( script ); CommandParameter param = new CommandParameter( "installPath", targetDirectory.Replace("\\\\", "\\") ); myCommand.Parameters.Add( param ); pipeline.Commands.Add( myCommand ); try { pipeline.Invoke(); } catch ( Exception e ) { MessageBox.Show( e.Message ); } } } } </code></pre> <p>When the exception is caught at pipeline.Invoke, the entire script is displayed (with the $installPath instead of the actual path) as a string before the error message detailed above. I've tried several checks within the script itself, but I get the same results no matter what, which tells me that the runspace just doesn't like the script itself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload