{"id":587,"date":"2019-11-25T11:31:14","date_gmt":"2019-11-25T19:31:14","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=587"},"modified":"2019-11-25T11:47:39","modified_gmt":"2019-11-25T19:47:39","slug":"setting-up-an-azure-automation-hybrid-worker","status":"publish","type":"post","link":"https:\/\/nillsf.com\/index.php\/2019\/11\/25\/setting-up-an-azure-automation-hybrid-worker\/","title":{"rendered":"Setting up an Azure Automation Hybrid Worker"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I am working with a customer that is going to set up an Azure Automation hybrid worker. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Hybrid Worker in Azure Automation allows you to run automation scripts on your own VMs. The benefit of this is that while the script is running, it can connect to all resources on the network it has access to. This is useful to move data around, or even do PowerShell remoting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Automation Hybrid Worker is part of the Microsoft Monitoring Agent. It&#8217;ll make an outbound connection to Azure Automation to get any information about scripts it has to run.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/azure\/automation\/media\/automation-hybrid-runbook-worker\/automation.png\" alt=\"Hybrid Runbook Worker overview\"\/><figcaption>Architecture of Azure Automation Hybrid worker.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up pre-requisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s a couple of pre-requisites before we can actually create the Hybrid worker. Number 1 is actually having a VM that will run the Hybrid worker. For this demo, I&#8217;ll create a new VM in East US.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"1024\" src=\"\/wp-content\/uploads\/2019\/11\/image-66-880x1024.png\" alt=\"\" class=\"wp-image-588\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-66-880x1024.png 880w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-66-258x300.png 258w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-66-768x894.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-66.png 1176w\" sizes=\"auto, (max-width: 880px) 100vw, 880px\" \/><figcaption>Creating a new VM for this demo.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We also need an Automation Account, which I&#8217;ll also create in East US. The Azure Run As account isn&#8217;t required, but I always like to have it, just in case.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"750\" src=\"\/wp-content\/uploads\/2019\/11\/image-67.png\" alt=\"\" class=\"wp-image-589\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-67.png 466w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-67-186x300.png 186w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><figcaption>Creating a new Automation Account.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And we&#8217;ll also need a Log Analytics workspace. Again, I&#8217;ll create a new one in East US.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"623\" src=\"\/wp-content\/uploads\/2019\/11\/image-68.png\" alt=\"\" class=\"wp-image-590\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-68.png 478w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-68-230x300.png 230w\" sizes=\"auto, (max-width: 478px) 100vw, 478px\" \/><figcaption>Creating a new Log Analytics workspace<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next up, we&#8217;ll need to add the Automation solution to our Log Analytics workspace. To do this, we&#8217;ll run a line of PowerShell, in the Cloud Shell.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$rgname=\"hybrid-worker\"\n$loganal=\"for-hybrid\"\nSet-AzureRmOperationalInsightsIntelligencePack -ResourceGroupName $rgname -WorkspaceName $loganal -IntelligencePackName \"AzureAutomation\" -Enabled $True<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And with the prereqs out of the way, we can go ahead and install the Hybrid worker on our machine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the Hybrid worker<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With all the prereqs done, your VM should now be ready. We&#8217;ll need to download a PowerShell script, and then run that script to setup the hybrid worker process. This script will essentially download the MMA agent and install it, and then connect the Hybrid worker agent on your machine to your Automation account.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Connect to your VM, open an elevated PowerShell prompt and run the following to download our script we need:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$url = \"https:\/\/raw.githubusercontent.com\/azureautomation\/runbooks\/master\/Utility\/ARM\/New-OnPremiseHybridWorker.ps1\"\n$output = \".\\New-OnPremiseHybridWorker.ps1\"\nInvoke-WebRequest -Uri $url -OutFile $output<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will download the script in the working directory. Next, we&#8217;ll need to actually run the script with the right parameters. This should do the job:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$aaname=\"hybrid-worker\"\n$aarg=\"hybrid-worker\"\n$hybridname=\"worker1\"\n$subid=\"d19dddf3-9520-4226-a313-ae8ee08675e5\"\n$loganal=\"for-hybrid\"\n$logrg=\"hybrid-worker\"\n\n\n.\\New-OnPremiseHybridWorker.ps1 -AutomationAccountName $aaname  -AAResourceGroupName $aarg -OMSResourceGroupName $logrg -HybridGroupName $hybridname -SubscriptionId $subid -WorkspaceName $loganal<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This might take a while to run, as this script downloads and installs the complete AzureRM module. After it installed that module, you&#8217;ll need to authenticate to it with your Azure account credentials. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"223\" src=\"\/wp-content\/uploads\/2019\/11\/image-69-1024x223.png\" alt=\"\" class=\"wp-image-591\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-69-1024x223.png 1024w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-69-300x65.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-69-768x167.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-69.png 1342w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And with that done, we can run scripts from within Azure automation on your Hybrid worker. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Test running a script on the Hybrid worker<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To quickly test this out, I&#8217;ll create a nonsense runbook in Azure automation to create a test.txt file on the desktop.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"473\" src=\"\/wp-content\/uploads\/2019\/11\/image-70.png\" alt=\"\" class=\"wp-image-592\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-70.png 466w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-70-296x300.png 296w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-70-60x60.png 60w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><figcaption>Creating a new runbook.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll have one line in our file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"from azure\" | out-file \"C:\\Users\\nilfranadmin\\Desktop\\hybrid.txt\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the editor in Azure Automation, paste in this line (maybe change the destination of the script) and hit save, then hit publish. Then hit start and run it on the Hybrid Worker we created. Not too long after, we should see our file appear:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"412\" height=\"149\" src=\"\/wp-content\/uploads\/2019\/11\/image-71.png\" alt=\"\" class=\"wp-image-593\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-71.png 412w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/image-71-300x108.png 300w\" sizes=\"auto, (max-width: 412px) 100vw, 412px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this quick post, we created a Hybrid Worker and ran a script on it. We ran the hybrid worker on an Azure VM, but you could equally well run this on an on-prem VM, or even on another cloud. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am working with a customer that is going to set up an Azure Automation hybrid worker. A Hybrid Worker in Azure Automation allows you to run automation scripts on your own VMs. The benefit of this is that while the script is running, it can connect to all resources on the network it has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,4],"tags":[25,8,61],"class_list":["post-587","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-management","tag-automation","tag-azure","tag-hybrid"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2019\/11\/automation1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=587"}],"version-history":[{"count":2,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/587\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/587\/revisions\/608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/595"}],"wp:attachment":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}