/// <summary> /// Opens a remote file, fetching its contents if necessary. /// </summary> /// <param name="remoteFilePath"> /// The remote file path to be opened. /// </param> /// <param name="runspaceDetails"> /// The runspace from which where the remote file will be fetched. /// </param> /// <returns> /// The local file path where the remote file's contents have been stored. /// </returns> public async Task <string> FetchRemoteFileAsync( string remoteFilePath, RunspaceDetails runspaceDetails) { string localFilePath = null; if (!string.IsNullOrEmpty(remoteFilePath)) { try { RemotePathMappings pathMappings = this.GetPathMappings(runspaceDetails); localFilePath = this.GetMappedPath(remoteFilePath, runspaceDetails); if (!pathMappings.IsRemotePathOpened(remoteFilePath)) { // Does the local file already exist? if (!File.Exists(localFilePath)) { // Load the file contents from the remote machine and create the buffer PSCommand command = new PSCommand(); command.AddCommand("Microsoft.PowerShell.Management\\Get-Content"); command.AddParameter("Path", remoteFilePath); command.AddParameter("Raw"); command.AddParameter("Encoding", "Byte"); byte[] fileContent = (await this.powerShellContext.ExecuteCommandAsync <byte[]>(command, false, false).ConfigureAwait(false)) .FirstOrDefault(); if (fileContent != null) { RemoteFileManagerService.StoreRemoteFile(localFilePath, fileContent, pathMappings); } else { this.logger.LogWarning( $"Could not load contents of remote file '{remoteFilePath}'"); } } } } catch (IOException e) { this.logger.LogError( $"Caught {e.GetType().Name} while attempting to get remote file at path '{remoteFilePath}'\r\n\r\n{e.ToString()}"); } } return(localFilePath); }
private string StoreRemoteFile( string remoteFilePath, byte[] fileContent, RunspaceDetails runspaceDetails) { RemotePathMappings pathMappings = this.GetPathMappings(runspaceDetails); string localFilePath = pathMappings.GetMappedPath(remoteFilePath); RemoteFileManagerService.StoreRemoteFile( localFilePath, fileContent, pathMappings); return(localFilePath); }