Вот что мой код выглядит так:
Код: Выделить всё
public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)
{
_logger.LogInformation("Invoking remote executable on Windows VM using RunCommand");
// Azure authentication and setup
string tenantId = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
string clientId = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
string clientSecret = Environment.GetEnvironmentVariable("AZURE_CLIENT_SECRET");
string subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
string resourceGroupName = "YOUR_RESOURCE_GROUP";
string vmName = "YOUR_VM_NAME";
// Use DefaultAzureCredential for authentication
var credential = new DefaultAzureCredential();
// Initialize Azure resource manager client
var armClient = new ArmClient(credential, subscriptionId);
// Get the specific VM resource
var subscription = armClient.GetSubscriptionResource(new ResourceIdentifier($"/subscriptions/{subscriptionId}"));
ResourceGroupResource resourceGroup = subscription.GetResourceGroup(resourceGroupName);
VirtualMachineResource vmResource = resourceGroup.GetVirtualMachine(vmName);
try
{
// Use Azure Compute's RunCommand to execute a PowerShell script on the VM
var runCommandInput = new RunCommandInput("RunPowerShellScript");
//{
// Script = new List { commandToExecute },
// Parameters = new List {}
//};
_logger.LogInformation("Running command on VM...");
// Execute the command
var runCommandResult = await vmResource.RunCommandAsync(WaitUntil.Completed, runCommandInput);
// Log the result
//_logger.LogInformation($"Command executed successfully. Status: {runCommandResult.Value.Status}");
}
catch (RequestFailedException ex)
{
_logger.LogError($"Failed to execute command on VM: {ex.Message}");
}
return new OkObjectResult("OK");
}
Существует конструктор, который устанавливает эти поля, но это внутренний конструктор, поэтому я не могу его вызвать.
Здесь — это ссылка на исходный код этого класса: https://github.com/Azure/azure-sdk-for- ... mmandInput .cs
Может кто-нибудь объяснить, как мне использовать класс RunCommandInput, поскольку в Интернете очень мало информации или вообще ее нет.
Кроме того, действительно ли мое решение работоспособно или его невозможно достичь с помощью ARM SDK?
Подробнее здесь: https://stackoverflow.com/questions/791 ... nction-app