Issue
I am writing a program in C# (dotnet 6/Mac) which will issue a PowerShell command to a remote EC2 instance running Windows (2012/PowerShell version 5.x) to download a file from S3.
I am on a Mac and I am able to connect to the EC2 Instance with PowerShell for Mac.
Here is the C# program:
public void DownloadS3FileToRemoteMachine(string host,
string user,
string password,
string bucket,
string s3path,
string localPath)
{
string s3DownloadCommand =
$"aws s3 cp s3://{bucket}{s3path} {localPath}";
var securePass = new SecureString();
foreach (char p in password)
{
securePass.AppendChar(p);
}
var credential = new PSCredential(user, securePass);
var connectionInfo = new WSManConnectionInfo
{
ComputerName = host,
Credential = credential,
NoEncryption = true,
Scheme = WSManConnectionInfo.HttpScheme
};
using Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo);
using PowerShell? ps = PowerShell.Create(rs).AddScript(s3DownloadCommand);
Collection<PSObject>? results;
try
{
rs.Open();
results = ps.Invoke();
if (ps.HadErrors)
{
string errors = string.Join(Environment.NewLine, ps.Streams
.Error
.ReadAll()
.Select(err => err.ErrorDetails.Message));
_logger.LogError("Error while downloading the file from S3 to local path {LocalPath}, " +
"error {ErrorMsg}", localPath, errors);
}
}
catch (Exception e)
{
_logger.LogError(e, "Error copying the file from S3 to remote machine");
throw;
}
string enumerable = string.Join("\n", results.Select(r => r.ToString()));
_logger.LogInformation(enumerable);
}
With this, I get the error:
Connecting to remote server 10.62.166.198 failed with the following error message : Authorization failed For more information, see the about_Remote_Troubleshooting Help topic.
However, I know that the code works because I have access to another Windows machine running Window 10 on my local network and I am able to successfully download the file on that machine.
If I remove the line NoEncryption = true
from the configuration then I get a different message:
Connecting to remote server 10.62.166.198 failed with the following error message : MI_RESULT_FAILED For more information, see the about_Remote_Troubleshooting Help topic.
Any help will be tremendously appreciated.
Solution
Thanks to the amazing people on the PowerShell Discord channel (access via https://www.reddit.com/r/PowerShell) I learnt that the current functionality of PowerShell to remote from Linux to Windows is very limited.
I had to choose OpenSSH option i.e. install OpenSSH on Windows machine, thereafter, I was able to access the service via C# like so:
var info = new ConnectionInfo(host, user, new PasswordAuthenticationMethod(user, password));
using var client = new SshClient(info);
client.HostKeyReceived += (_, e) => e.CanTrust = true;
client.Connect();
string s3DownloadCommand = $"aws s3 cp s3://{bucket}/{source} {destination}";
using SshCommand? cmd = client.CreateCommand(s3DownloadCommand);
string? output = cmd.Execute();
Answered By - Vinay Pandey Answer Checked By - Mary Flores (WPSolving Volunteer)