I have a bash
file with 50 lines of command that needs to be executed on Azure Linux VM. I am using azure library Microsoft.Azure.Management.Compute
for.Net and found IVirtualMachine.RunShellScript(List<string> commandLines, List<RunCommandInputParameter>)
. But it seems the method accepts only list of string commands. I understand I can split the bash file commands into multiple strings, but is there a better way to handle this?
If you want to run the bash scripts on Azure Linux VM, you have two choices: Run Command and Custom Script extension . According to the situation, you can use custom script extension
For example
upload the bash file to Azure blob
Create a service principal and assign a role to sp
az login
az account set --subscription "<your subscription id>"
# the sp will have Azure Contributor role
az ad sp create-for-rbac -n "readMetric"
# use msal get token
var app = ConfidentialClientApplicationBuilder.Create(<sp app id>)
.WithAuthority(AzureCloudInstance.AzurePublic, "<sp tenantID>")
.WithClientSecret(<sp password>)
.Build();
string[] scopes = new string[] { "https://management.azure.com/.default" };
var result = await app.AcquireTokenForClient(scopes)
.ExecuteAsync();
TokenCredentials tokenCredentials = new TokenCredentials(result.AccessToken,"Bearer");
var client = new ComputeManagementClient(tokenCredentials);
client .SubscriptionId = "<>";
var resourceGroupName = "";
var vmName = "";
var extensionName = "";
VirtualMachineExtensionInner para = new VirtualMachineExtensionInner {
AutoUpgradeMinorVersion=true,
Location="",
TypeHandlerVersion="2.1",
VirtualMachineExtensionType= "CustomScript",
Publisher = "Microsoft.Azure.Extensions",
Settings = new {
FileUris = new List<string>
{
"Azure blob url"
}
},
ProtectedSettings = new {
CommandToExecute = $"sh <bash file name>",
StorageAccountName = "<AccountName>",
StorageAccountKey = "<account key>"
}
};
var result =await client.VirtualMachineExtensions.CreateOrUpdateAsync(resourceGroupName, vmName, extensionName, para);
Console.WriteLine(result.Id);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.