简体   繁体   English

如何对Azure Data Factory V2中的现有Azure VM运行远程命令(PowerShell / Bash)?

[英]How to run a remote command (powershell/bash) against an existing Azure VM in Azure Data Factory V2?

I've been trying to find a way to run a simple command against one of my existing Azure VMs using Azure Data Factory V2. 我一直在尝试找到一种使用Azure Data Factory V2针对我现有的Azure虚拟机运行简单命令的方法。

Options so far: 到目前为止的选项:

  • Custom Activity/Azure Batch won't let me add existing VMs to the pool 自定义活动/ Azure批处理不允许我将现有VM添加到池中
  • Azure Functions - I have not played with this but I have not found any documentation on this using AZ Functions. Azure Functions-我还没有玩过,但是我没有使用AZ Functions找到关于此的任何文档。
  • Azure Cloud Shell - I've tried this using the browser UI and it works, however I cannot find a way of doing this via ADF V2 Azure Cloud Shell-我已经使用浏览器UI尝试过方法,并且它可以工作,但是我找不到通过ADF V2做到这一点的方法

The use case is the following: 用例如下:

There are a few tasks that are running locally (Azure VM) in task scheduler that I'd like to orchestrate using ADF as everything else is in ADF, these tasks are usually python applications that restore a SQL Backup and or purge some folders. 我想在任务计划程序中本地运行一些任务(Azure VM),就像其他所有在ADF中一样,我希望使用ADF进行编排,这些任务通常是可恢复SQL Backup和/或清除某些文件夹的python应用程序。

ie sqdb-restore -r myDatabase sqdb-restore -r myDatabase

where sqldb-restore is a command that is recognized locally after installing my local python library. 其中sqldb-restore是安装我的本地python库后在本地识别的命令。 Unfortunately the python app needs to live locally in the VM. 不幸的是,python应用程序需要在VM中本地运行。

Any suggestions? 有什么建议么? Thanks. 谢谢。

Thanks to @martin-esteban-zurita, his answer helped me to get to what I needed and this was a beautiful and fun experiment. 感谢@ martin-esteban-zurita,他的回答帮助我找到了所需的东西,这是一个美丽而有趣的实验。

It is important to understand that Azure Automation is used for many things regarding resource orchestration in Azure (VMs, Services, DevOps), this automation can be done with Powershell and/or Python. 重要的是要了解, Azure自动化用于与Azure中的资源编排有关的许多事情(VM,服务,DevOps),该自动化可以通过Powershell和/或Python来完成。

In this particular case I did not need to modify/maintain/orchestrate any Azure resource, I needed to actually run a Bash/Powershell command remotely into one of my existing VMs where I have multiple Powershell/Bash commands running recurrently in "Task Scheduler". 在这种特殊情况下,我不需要修改/维护/编排任何Azure资源,而是需要在我的现有VM之一中远程运行Bash / Powershell命令,而我在其中的多个“任务计划程序”中经常运行多个Powershell / Bash命令。 "Task Scheduler" was adding unnecessary overhead to my data pipelines because it was unable to talk to ADF. “任务计划程序”为我的数据管道增加了不必要的开销,因为它无法与ADF进行通信。

In addition, Azure Automation natively only runs Powershell/Python commands in Azure Cloud Shell which is very useful to orchestrate resources like turning on/off Azure VMs, adding/removing permissions from other Azure services, running maintenance or purge processes, etc, but I was still unable to run commands locally in an existing VM. 此外,Azure Automation本机仅在Azure Cloud Shell中运行Powershell / Python命令,这对于协调资源(例如打开/关闭Azure VM,从其他Azure服务添加/删除权限,运行维护或清除过程等)非常有用,但是我仍然无法在现有VM中本地运行命令。 This is where the Hybrid Runbook Worker came into to picture. 这是Hybrid Runbook Worker的图片。 A Hybrid worker group 混合工人团体

These are the steps to accomplish this use case. 这些是完成此用例的步骤。

1. Create an Azure Automation Account 1.创建一个Azure自动化帐户

2. Install the Windows Hybrid Worker in my existing VM . 2.在现有VM中安装 Windows Hybrid Worker。 In my case it was tricky because my proxy was giving me some errors. 就我而言,这很棘手,因为我的代理给我一些错误。 I ended up downloading the Nuget Package and manually installing it. 我最终下载了Nuget软件包并手动安装。

.\New-OnPremiseHybridWorker.ps1 -AutomationAccountName <NameofAutomationAccount> -AAResourceGroupName <NameofResourceGroup>
-OMSResourceGroupName <NameofOResourceGroup> -HybridGroupName <NameofHRWGroup>
-SubscriptionId <AzureSubscriptionId> -WorkspaceName <NameOfLogAnalyticsWorkspace>

Keep in mind that in the above code, you will need to find your own parameter values, the only parameter that does not have to be found and will be created is HybridGroupName this will define the name of the Hybrid Group 请记住,在上面的代码中,您将需要找到自己的参数值,唯一不需要找到并将创建的参数是HybridGroupName它将定义混合组的名称。

3. Create a PowerShell Runbook 3.创建一个PowerShell Runbook

[CmdletBinding()]
Param
([object]$WebhookData) #this parameter name needs to be called WebHookData otherwise the webhook does not work as expected.
$VerbosePreference = 'continue'

#region Verify if Runbook is started from Webhook.

# If runbook was called from Webhook, WebhookData will not be null.
if ($WebHookData){

    # Collect properties of WebhookData
    $WebhookName     =     $WebHookData.WebhookName
    # $WebhookHeaders  =     $WebHookData.RequestHeader
    $WebhookBody     =     $WebHookData.RequestBody

    # Collect individual headers. Input converted from JSON.
    $Input = (ConvertFrom-Json -InputObject $WebhookBody)
    # Write-Verbose "WebhookBody: $Input"
    #Write-Output -InputObject ('Runbook started from webhook {0} by {1}.' -f $WebhookName, $From)
}
else
{
   Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
}
#endregion

# This is where I run the commands that were in task scheduler

$callBackUri = $Input.callBackUri

 # This is extremely important for ADF
 Invoke-WebRequest -Uri $callBackUri -Method POST

4. Create a Runbook Webhook pointing to the Hybrid Worker's VM 4.创建一个指向混合工作者的VM的Runbook Webhook

在此处输入图片说明

在此处输入图片说明

4. Create a webhook activity in ADF where the above PowerShell runbook script will be called via a POST Method 4.在ADF中创建一个webhook活动 ,将通过POST方法调用上述PowerShell Runbook脚本

Important Note: When I created the webhook activity it was timing out after 10 minutes (default), so I noticed in the Azure Automation Account that I was actually getting INPUT data (WEBHOOKDATA) that contained a JSON structure with the following elements: 重要说明:当我创建webhook活动时,它会在10分钟后超时(默认),因此我在Azure自动化帐户中注意到我实际上正在获取包含具有以下元素的JSON结构的INPUT数据(WEBHOOKDATA):

  • WebhookName WebhookName
  • RequestBody (This one contains whatever you add in the Body plus a default element called callBackUri) RequestBody(此内容包含您在Body中添加的内容以及一个名为callBackUri的默认元素)

All I had to do was to invoke the callBackUri from Azure Automation. 我要做的就是从Azure自动化调用callBackUri And this is why in the PowerShell runbook code I added Invoke-WebRequest -Uri $callBackUri -Method POST . 这就是为什么在PowerShell Runbook代码中我添加了Invoke-WebRequest -Uri $callBackUri -Method POST With this, ADF was succeeding/failing instead of timing out. 因此,ADF成功/失败而不是超时。

There are many other details that I struggled with when installing the hybrid worker in my VM but those are more specific to your environment/company. 在VM中安装混合工作程序时,还有许多其他细节需要解决,但这些细节更特定于您的环境/公司。

This looks like a use case that is supported with Azure Automation, using a hybrid worker. 这看起来像使用混合工作程序的Azure自动化支持的用例。 Try reading here: https://docs.microsoft.com/en-us/azure/automation/automation-hybrid-runbook-worker 尝试在这里阅读: https : //docs.microsoft.com/en-us/azure/automation/automation-hybrid-runbook-worker

You can call runbooks with webhooks in ADFv2, using the web activity. 您可以使用网络活动在ADFv2中调用带有网络钩子的Runbook。

Hope this helped! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM