Azure Automation RunBook Download File from Blob To VM - azure

Is there a way to use Azure Automation to download a file from azure storage? I can currently connect to the VM using templates from the gallery to create files/folder but how would I download a file from storage?
I am currently trying to use Get-AzureStorageBlob command from Invoke-command -ScriptBlock

If you are trying to use the powershell cmdlets, you need to remember to login to Azure prior to executing them. See the documentation. You would need to login in on the remote computer (ie: inside the Script Block).
An alternative is to have azcopy accessible, and simply pass in the key information via Automation Credentials.

If you want to do this, based on my experience, you need do the following steps.
1.Install Azure PowerShell on your target VM.
2.Enable Winrm on your VM, you need open port 5986 on Windows Firewall and Azure NSG. You also need configuration certificate on your VM. You could check this blog that step by step to enable winrm on Azure VM.
Note: You should enable winrm listening on https, if you enable it on http, you could not winrm your VM on runbook script.
3.Login to your Azure subscription in runbook, you could refer to this link about this.
4.Use New-PSSession to login your VM in runbook and execute your PowerShell cmlet. You could check my answer about this.

Related

Job Suspended Run Login-AzureRmAccount to login using Azure AutomationAccounts System Managed Identity

I am trying to shutdown the VM using Azure Automation Account System Managed identity option.
However I am ending up with below error.
As per the other articles it is mentioned to upgrade the module Update-ModulesInAutomationToLatestVersion but I could not update due to below error. I am not sure what is the issue in the script. Same script works with AzureRunAsConnection option without issues ( script ).I even checked with simple login with System Managed Identity it successfully login and fetches the resource group names.
I have tested the above shared script in my automation account. Below are
the couple of observations:
You need to use Connect-AzureRMAccount -Identity cmdlet instead of 'connect-AzAccount` to connect to your subscription because the rest of the script that you have written using Azure RM cmdlets.
If we use AzureRM cmdlets in your run book script the job is getting suspended stating that Azure RM is going to retired and suggesting us to use Az Module in your workflow.
You can refer to this documentation on how to migrate your PowerShell scripts automatically from AzureRM to AZ modules.
If you want to perform start/stop on your virtual Machines you can leverage the Azure Automation start/stop during the off hours feature.
According to the MICROSOFT DOCUMENTATION and looking at your script the Azure Rm module is not supported and it has been updated to the latest version of Az module.
For more information please refer the below links:-
MICROSOFT DOCUMENT|Using a system-assigned managed identity for an Azure Automation account & Troubleshoot runbook issue.

Install Powershell for Azure without an Azure Account

I want to use Get-AzureStorageBlob in a powershell script so my client can download files from my Azure Blob storage ( I use Devops to put them there )
The keys are in the script so the client does not need an Azure account.
He does however need to install Azure Powershell
And the instructions ask him to log in to Azure.
Is there an alternative?
If you just operates with azure storage, then you can ignore the "Connect-AzAccount" cmdlet.
After installing azure powershell module, since you have account_name and account_key of storage account, directly use them to download the blob.
But if you want to operate other resources like vm etc.,then you need to the cmdlet "Connect-AzAccount".
When I click Show Details in the right Commands panel I get an error message
cannot be loaded because running scripts is disabled on this system.

Azure Automation DSC - add a VM from a different subscription as a DSC node

I'm trying to onboard a VM to Azure Automation DSC. Can we add a VM from a different subscription as a DSC node ?
You won't be able to use the Register-AzureRMAutomationDSC to setup VMs cross subscription.
Following the instructions located here there are steps to run the Get-AzureRMAutomationRegistrationInfo cmdlet to get the registration key required (of the target Azure Automation account) and then steps to configure the DSC Extension on the target VM. Please note the instructions are for ASM VMs, but you can use Set-AzureRMVMExtension for ASR VMs.
Alternatively, for a more manual approach, follow the instructions here to obtain the registration url and the registration key, and then configure the Local DSC manager using the Set-DscLocalConfigurationManager cmdlet.

How to run a script on Azure VM from a remote machine

I'm looking for a way to run a script, on a single instance of a cloud service (which has several instances) from a remote machine.
I've tried DSC but that doesn't seem to support running the script only on one machine, as it would run it on all machines of the cloud service.
Note I'm using classic Azure cloud services.
Update: I specifically have an issue with Get-AzureVM which is part of the script example.
How do I get the instance VM which is part of my cloud service.
For instance I tried:
Get-AzureVM -ServiceName myCloudServiceName -Name instanceVMName
And that didn't return anything (I made sure to Add-AzureAccount first).
I’m assuming it returns nothing because Get-AzureVM cannot be used for cloud services, and only for VMs.
I’m able to get the instance by running
$dep = Get-AzureDeployment -ServiceName myCloudServiceName -Slot Production
$dep.RoleInstanceList[0]
Which returns role instance type which is different from the expected VM type.
Any ideas?
Powershell Remoting in Azure Cloud Services
As you are using a single instance the above should do the trick, you will need to create the user as part of a startup script, otherwise it will disappear each time it recreates the VM. It will not work if you every scale out to more than one instance of a cloud service.
You could use Custom Script extension for Windows virtual machines. It supports run on a single VM.
With the Custom Script extension for Windows, you can run PowerShell scripts on a remote VM without signing in to it. You can run the scripts after provisioning the VM, or at any time during the lifecycle of the VM without opening any additional ports. The most common use cases for running Custom Script extension include running, installing, and configuring additional software on the VM after it's provisioned.
More information please refer to this link.

run powershell script without add publishsettings file

I am using powershell script, using this i create/setup vm in azure. I want to run powershell script without azure credentials (right now i am using as below but I don't want to my.publishsettings or publishsettings details in powershell script).
create_vm.ps1
...
azure account import D:\my.publishsettings
...
Is there any want to do same. please suggest me.
There are two ways by which you can connect to and manage your Azure Subscription - One is using X509 Certificate (which is what you're doing when you use publishsettings file) and the other is using Azure AD.
Please see this link for detailed instructions on how you can use Azure AD to manage your Azure Subscriptions: https://azure.microsoft.com/en-in/documentation/articles/powershell-install-configure/. Scroll down to section titled How to: Connect to your subscription.

Resources