@@ -22,6 +22,7 @@ namespace Microsoft.PowerShell.EditorServices
22
22
using System . Management . Automation . Host ;
23
23
using System . Management . Automation . Runspaces ;
24
24
using Microsoft . PowerShell . EditorServices . Session . Capabilities ;
25
+ using System . IO ;
25
26
26
27
/// <summary>
27
28
/// Manages the lifetime and usage of a PowerShell session.
@@ -665,21 +666,48 @@ await this.ExecuteCommand<object>(
665
666
/// <summary>
666
667
/// Executes a script file at the specified path.
667
668
/// </summary>
668
- /// <param name="scriptPath ">The path to the script file to execute.</param>
669
+ /// <param name="script ">The script execute.</param>
669
670
/// <param name="arguments">Arguments to pass to the script.</param>
670
671
/// <returns>A Task that can be awaited for completion.</returns>
671
- public async Task ExecuteScriptAtPath ( string scriptPath , string arguments = null )
672
- {
673
- // If we don't escape wildcard characters in the script path, the script can
674
- // fail to execute if say the script name was foo][.ps1.
675
- // Related to issue #123.
676
- string escapedScriptPath = EscapePath ( scriptPath , escapeSpaces : true ) ;
677
-
678
- await this . ExecuteScriptString (
679
- $ "{ escapedScriptPath } { arguments } ",
680
- true ,
681
- true ,
682
- false ) ;
672
+ public async Task ExecuteScriptWithArgs ( string script , string arguments = null )
673
+ {
674
+ PSCommand command = new PSCommand ( ) ;
675
+
676
+ if ( arguments != null )
677
+ {
678
+ // Need to determine If the script string is a path to a script file.
679
+ string scriptAbsPath = string . Empty ;
680
+ try
681
+ {
682
+ // Assume we can only debug scripts from the FileSystem provider
683
+ string workingDir =
684
+ this . CurrentRunspace . Runspace . SessionStateProxy . Path . CurrentFileSystemLocation . ProviderPath ;
685
+ workingDir = workingDir . TrimEnd ( Path . DirectorySeparatorChar ) ;
686
+ scriptAbsPath = workingDir + Path . DirectorySeparatorChar + script ;
687
+ }
688
+ catch ( System . Management . Automation . DriveNotFoundException e )
689
+ {
690
+ Logger . Write (
691
+ LogLevel . Error ,
692
+ "Could not determine current filesystem location:\r \n \r \n " + e . ToString ( ) ) ;
693
+ }
694
+
695
+ // If we don't escape wildcard characters in a path to a script file, the script can
696
+ // fail to execute if say the script filename was foo][.ps1.
697
+ // Related to issue #123.
698
+ if ( File . Exists ( script ) || File . Exists ( scriptAbsPath ) )
699
+ {
700
+ script = EscapePath ( script , escapeSpaces : true ) ;
701
+ }
702
+ string scriptWithArgs = script + " " + arguments ;
703
+ command . AddScript ( scriptWithArgs ) ;
704
+ }
705
+ else
706
+ {
707
+ command . AddCommand ( script ) ;
708
+ }
709
+
710
+ await this . ExecuteCommand < object > ( command , true ) ;
683
711
}
684
712
685
713
internal static TResult ExecuteScriptAndGetItem < TResult > ( string scriptToExecute , Runspace runspace , TResult defaultValue = default ( TResult ) )
0 commit comments