I am trying to setup Azure VM's using Powershell but i wish to do it all remotely with no logging into the machine. I have been able to spin up the VM using commands like New-AzVM but now i need to run specific powershell commands e.g. Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
I can see you can run remote scripts using the following but as this is a brand new server there are no scripts on that box to execute.
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -Name $VmName -CommandId 'RunPowerShellScript' -ScriptPath '<pathToScript>' -Parameter #{"arg1" = "var1";"arg2" = "var2"}
How can i copy scripts to the server remotely so i can use the above command, or run commands without the need for a script file.
You don't need to copy the scripts remotely to the Azure VM. You can run it locally.
Save the PowerShell scripts to a format .ps1 file, then run it on local PowerShell with referencing the path of the script via parameter -ScriptPath.
Enable-WindowsOptionalFeature -Online -FeatureName IIS-ManagementConsole
You also could copy that scripts on the Virtual machine---Run command---RunPowerShellScript on the Azure portal, refer to this.
What you need is to look into the direction of PowerShell Desired State Configuration (DSC)
Here you can find general explanation for this technology
Get started manual for DSC
Related
I'm trying to run the command
Invoke-Command -ComputerName AzMachine1 -Credential $Cred -ScriptBlock{Test-Path $path}
on an Azure machine. However, it returns the following error "Connecting to remote server AzMachine1 failed with the following error message : The WinRM client received an HTTP
status code of 504 from the remote WS-Management service". I've checked if the service WinRM is running and working correctly and everything seams fine.
I'm not sure if the command Invoke-Command works on Azure machines. Can someone confirm this?
I also looked into the module Az.Compute and the command
Invoke-AzVMRunCommand
Which of these commands is the best one to run the command Test-Path remotely?
Running PowerShell remote commands on an Azure VM is possible, but there is some configuration required.
See Microsoft's instructions: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/winrm
I am trying to create a PowerShell Script inside a Function App that checks if a Docker Container is running on an Azure VM. So far I have managed to check wether the VM is running with the following script:
$provisioningState = (Get-AzVM -resourcegroupname $rsgName -name $vmName -Status).Statuses[1].Code
$condition = ($provisioningState -eq "PowerState/running")
However, I am not able to check the Docker status. How can I check from a Function App whether Docker is running on the VM using PowerShell?
Thank you for your help.
If you're wanting to run the command on the VM to check docker then you could use the Run Command and Invoke-AzVMRunCommand.
As this requires the script to be local to where the command is being run you may have to store the script inside the function app or write a wrapper function/module to make use of it.
Docs on that are here:
https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command#powershell
How to call a file where the File Path resides in Azure VM Using the Powershell (ISE Locally).
Using Powershell ISE with local computer and trying to call a Ps file which is placed in the Azure Vm.
If you mean to run an .ps1 file inside the VM, then you can use the PowerShell command Invoke-AzVMRunCommand. You can create a script in your local machine and put the command that you will execute to run the .ps1 inside the VM to the script. For example:
the script script.ps1 in your local machine:
powershell.exe -f 'C:\script_inside_VM.ps1' # this is the command that will run inside the VM
Use the PowerShell command:
Invoke-AzVMRunCommand -ResourceGroupName groupName -VMName vmName -CommandId 'RunPowerShellScript' -ScriptPath '\path\script.ps1'
I am trying to download and install an exe during the provisioning of a Windows VM in Azure cloud. I do not want to use Custom Script Extension but instead I want to use "Custom Data". I cannot find any solid examples on Azure documentation.
In AWS, I found enough resources and I could develop the below PowerShell script and add it to the User Data but that doesn't work on Azure, I tried different variations but with no luck. Has anyone done that before? Is there any clear documentation on that? I read Azure uses Cloud-init but again, no clear examples on how to do that with Cloud-init for a Windows machine, all examples are for Linux.
<powershell>
start-transcript
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest 'https://www.domain-name.com/filename.exe' -OutFile C:\filename.exe
C:\filename.exe --quiet
</powershell>
Any help would be appreciated.
You can inject data into a Windows virtual machine on Azure, but you can't execute it using custom data or cloud init unfrotunately. Execution of the custom data using cloud init is only supported in Ubuntu images.
Source: https://azure.microsoft.com/es-es/blog/custom-data-and-cloud-init-on-windows-azure/
To achieve an execution of a script post provisioning, it depends on how you're provisioning the VM.
In ARM templates you can use custom script extensions: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows
Using Azure CLI you can execute a script using az vm run-command like this:
az vm run-command invoke --command-id RunPowerShellScript --name win-vm -g my-resource-group \
--scripts 'param([string]$arg1)' \
'Write-Host Hello' \
--parameters 'arg1=kemety'
Sourced from here: https://learn.microsoft.com/en-us/cli/azure/vm/run-command?view=azure-cli-latest
I have a few Windows Server 2019 Core VMs in Azure all running the same windows service.
I would like to run a powershell script from my computer that loops through all the VMs and restarts the service on each.
I have a PS script on each VM which does this so how do I invoke them remotely? Is this possible without using Cloud Shell or Automation, with just a few lines of code? I have Azure powershell tools installed on my computer too.
Please provide answers with a working example.
Thank you
For your requirement, I think the simplest way is to use the Azure PowerShell command to execute the PowerShell script inside the VM, the example command here:
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter #{param1 = "var1"; param2 = "var2"}
To execute the script in each VM, you need to make a loop with PowerShell yourself. By the way, you can also use the Custom Extension for Windows.