@@ -22,6 +22,7 @@ namespace Microsoft.PowerShell.EditorServices
2222 using System . Management . Automation . Host ;
2323 using System . Management . Automation . Runspaces ;
2424 using Microsoft . PowerShell . EditorServices . Session . Capabilities ;
25+ using System . IO ;
2526
2627 /// <summary>
2728 /// Manages the lifetime and usage of a PowerShell session.
@@ -665,21 +666,48 @@ await this.ExecuteCommand<object>(
665666 /// <summary>
666667 /// Executes a script file at the specified path.
667668 /// </summary>
668- /// <param name="scriptPath ">The path to the script file to execute.</param>
669+ /// <param name="script ">The script execute.</param>
669670 /// <param name="arguments">Arguments to pass to the script.</param>
670671 /// <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 ) ;
683711 }
684712
685713 internal static TResult ExecuteScriptAndGetItem < TResult > ( string scriptToExecute , Runspace runspace , TResult defaultValue = default ( TResult ) )
0 commit comments