Calling Powershell from MATLAB: AWS Example

3 vues (au cours des 30 derniers jours)
Matlab2010 le 14 Nov 2014
Réponse apportée : Greg le 9 Déc 2014
Running 2014A and Windows Server 2012 R2. I wish to call Powershell from inside matlab. Below is my attempt,
%%Get The required Data
[~, instance_ID] = system('powershell 2*10; echo "GET-EC2-Instance"');
[~, region_id] = system('powershell 2*10; echo "GET-AWSRegion"');
Terminate_param = true; % stop or terminate?
%terminate the instance
[status,cmdout] = system(['powershell 2*10; echo "Stop-EC2Instance -Instance' instance_ID ' -Region' region_id '-Terminate' '-Force"']);
%stop the instance
[status,cmdout] = system(['powershell 2*10; echo "Stop-EC2Instance -Instance' instance_ID ' -Region' region_id '-Force"']);
As you can see what I am unclear on is how to differentiate between Powershell and AWS Powershell. When I run the above commands I get the below (which is not the response I want),
[~, instance_ID] = system('powershell 2*10; echo "GET-EC2-Instance"')
instance_ID =
When I open the executable "Windows Powershell for AWS" (as per AWS Powershell) and type the command lines, i get the below (Note I have already entered my access key, secret key and region).
PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell> Get-AWSRegion
Region Name IsShellDefault
------ ---- --------------
us-east-1 US East (Virginia) True
us-west-1 US West (N. California) False
us-west-2 US West (Oregon) False
eu-west-1 EU West (Ireland) False
ap-northeast-1 Asia Pacific (Tokyo) False
ap-southeast-1 Asia Pacific (Singapore) False
ap-southeast-2 Asia Pacific (Sydney) False
sa-east-1 South America (Sao Paulo) False
PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell> GET-EC2Instance
GroupNames : {}
Groups : {}
Instances : {my_key_name, my_key_name, my_key_name, my_key_name...}
OwnerId : aws_account_id
RequesterId :
ReservationId : r-be212395
GroupNames : {}
Groups : {}
Instances : {my_key_name}
OwnerId : aws_account_id
RequesterId :
ReservationId : r-bbeac492
GroupNames : {}
Groups : {}
Instances : {my_key_name}
OwnerId : aws_account_id
RequesterId :
ReservationId : r-15efd42f
GroupNames : {}
Groups : {}
Instances : {my_key_name}
OwnerId : aws_account_id
RequesterId :
ReservationId : r-dc3133f2
However, none of these correspond to the instance_id of the machine from which I am making this call.
Then I run the below, getting the instance_id from the AWS management console window,
PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell> Stop-EC2Instance -Instance i-2810d7c6
This then stops the instance as it's supposed to.
Hence I am happy the commands work. I would just like to be able to execute these from inside MATLAB.
Also keen to hear from anyone who was experience with the AWS command line interface (CLI) tool.
thank you.
  2 commentaires
Geoff Hayes
Geoff Hayes le 14 Nov 2014
Try using system or the exclamation point. An example of the latter and powershell seems to be found at How do I execute Powershell scripts in MATLAB.
Matlab2010 le 18 Nov 2014
temp answer: You need to write bat files that speak to aws powershell (which does not have an exe, only a dll). system.m then calls the bat file. to be updated when ive got it working...

Connectez-vous pour commenter.

Réponses (1)

Greg le 9 Déc 2014
I'm not totally clear on what the actual question is. However, I'm taking a decent guess that your problem is the "echo" in your system command. In Windows system calls, "ECHO" is like MATLAB's "DISP". It purely prints the string back to you, there is no command being executed.


En savoir plus sur Install Products dans Help Center et File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by