I have a linux Cent OS vm and am trying to add a custom script to it. If i do it manually from the portal i am able to install the custom script extension for linux. But i am not able to install the custom script via powershell code that i added below. Need some help on it. I am storing the custom script file in a storage account and calling it from there when installing the custom script extension on Linux.
Error i get is : Enable failed: processing file downloads failed: failed to download file[0]: failed to download file: unexpected status code: actual=404 expected=200
if($vmname.OSType -eq "Linux"){
$resourcegroup=$vmname.ResourceGroupName
$location=$vmname.Location
$vm=$vmname.Name
$TheURI = "https://storageaccount.blob.core.windows.net/container/cstmscript.sh"
$ScriptSettings = #{"fileUris" = #($TheURI); "commandToExecute" = "sh cstmscript.sh";}
Set-AzVMExtension -ResourceGroupName $resourcegroup -VMName $vm -Name "Custom Script" -Publisher "Microsoft.Azure.Extensions" -TypeHandlerVersion 2.0 -ExtensionType "CustomScript" -Location $location -Settings $ScriptSettings
}
Cant use Set-AzVMCustomScriptExtension as that's for only Windows VM's
Thanks Satya It did work Btw . I needed to add the access keys.
here is the code
if($vmname.OSType -eq "Linux"){
$resourcegroup=$vmname.ResourceGroupName
$location=$vmname.Location
$vm=$vmname.Name
$TheURI = "https://storage.blob.core.windows.net/container/cstmscript.sh"
$ScriptSettings = #{"fileUris" = #($TheURI); "commandToExecute" = " sh cstmscript.sh";}
$ProtectedSettings = #{"storageAccountName"="storage";"storageAccountKey" = ""};
Set-AzVMExtension -ResourceGroupName $resourcegroup -VMName $vm -Name "Custom Script" -Publisher "Microsoft.Azure.Extensions" -TypeHandlerVersion 2.0 -ExtensionType "CustomScript" -Location $location -Settings $ScriptSettings -ProtectedSettings $ProtectedSettings
}
I had tried hitting the above the url
https://storageaccount.blob.core.windows.net/container/cstmscript.sh
Which is more or less 404 error as encountered below :
Enable failed: processing file downloads failed: failed to download file[0]: failed to download file: unexpected status code: actual=404 expected=200
Looks like you re not able to access the cstmsscript.sh & encountering a 404 - you will have to host the file anonymously accessible or alternatively use means to grant access ( like SAS )
Related
I am trying to import API using swagger.json file using Azure Pipeline. I have added a Azure CLI Task Version 2. Selected Script Type as Powershell Core.
In the inline script I have used below commands
$apiMgmtContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
$api = Get-AzApiManagementApi -Context $apiMgmtContext -Name $ApiName
The first one is working fine. But the second line of code is throwing error.
Below is the error I get:
I have searched this error Status Code : CloseError But no solid information on the same. I tried to Install few powershell modules for Azure Api , still it gives the same error.
I have tried with the below mentioned Azure APIM PowerShell Commands to get the API details from the Azure APIM Instance and got expected results:
$ApimResource = New-AzApiManagement -Name "pravusapim1205" -ResourceGroupName "pravutestrg" -Location "West US" -Organization "Contoso" -AdminEmail "admin#contoso.com"
$ApimResourceDetails = Get-AzApiManagement -Name "pravusapim1205" -ResourceGroupName "pravutestrg"
$ApimResourceDetails
$ApiMgmtContext = New-AzApiManagementContext -ResourceGroupName "pravutestrg" -ServiceName "pravusapim1205"
$ApiName = "Echo API"
$api = Get-AzApiManagementApi -Context $apiMgmtContext -Name $ApiName
$api
Output:
Reference: GitHub Doc on APIM PS Commands.
I referred to below link and succeeded to reset password on VM scale set with extension created
Reset password of a virtual machine scale set
Now, I want to reset the password again. However, if I perform the same as above, it will give me an error during Update-AzVmss
Update-AzVmss: On resource 'VMSS1', extension name 'VMAccessAgent' cannot be used for more than one extension.
ErrorCode: BadRequest
ErrorMessage: On resource 'VMSS1', extension name 'VMAccessAgent' cannot be used for more than one extension.
ErrorTarget:
StatusCode: 400
ReasonPhrase: Bad Request
OperationID : 53c2fea8-bf5a-47fe-a5e9-8e98eea1bb7b
How should I reset the password again? Does it mean I have to remove the extension and run the Powershell script again?
The error you are referring(On VM Scale Set 'VMSS1', extension name 'VMAccessAgent' cannot be used for more than one extension), I suspect this could be related to this one:"Multiple VMExtensions per handler not supported for OS type '{0}'. VMExtension '{1}' with handler '{2}' already added or specified in input."
Understand common error messages when you manage virtual machines in Azure
If the issue still persist, remove the extension and Process password update through script:
$vmss = ""
$vmssResourceGroup = ""
$publicConfig = #{"UserName" = ""}
$privateConfig = #{"Password" = ""}
$extName = "VMAccessAgent"
$publisher = "Microsoft.Compute"
$vmss = Get-AzVmss -ResourceGroupName $vmssResourceGroup
-VMScaleSetName $vmssName
$vmss = Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name
$extName -Publisher $publisher -Setting $publicConfig
-ProtectedSetting $privateConfig -Type $extName -TypeHandlerVersion "2.0" -AutoUpgradeMinorVersion $true
Update-AzVmss -ResourceGroupName $vmssResourceGroup -Name $vmssName
-VirtualMachineScaleSet $vmss
I was unable to remove the extension using any PowerShell commands. Ultimately I just deleted it in the portal and then was able to set the extension.
I'm setting up some virtual machines to run my service. There may be several, so I'm trying to automate the process. I've got a PowerShell script that successfully build the virtual machine, but now I want to install the dependent software that my .NET Core Web Application requires in the same script.
The first dependency I want to install is .NET 5.0 Runtime. I've done this many times from the browser, but now I want to commit this to a script that runs after the VM has been built.
Test locally or on a test VM by installing using the dotnet-install-script and finalize the parameters. Then use Set-AzVMExtension to install that script using custom script extension. The code would look like this (not tested)
$Command = "&powershell -NoProfile -ExecutionPolicy unrestricted -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; &([scriptblock]::Create((Invoke-WebRequest -UseBasicParsing 'https://dot.net/v1/dotnet-install.ps1'))) <additional install-script args>"
$Settings = #{"commandToExecute" = "Powershell $Command";};
Set-AzVMExtension `
-ResourceGroupName "ResourceGroupName" `
-Location "Location" `
-VMName "VirtualMachineName" `
-Name "ExtensionName" `
-Publisher "Contoso.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion "1.1" `
-Settings $Settings
Full details and schema of settings custom-script-windows
You can also use Set-AzureVMCustomScriptExtension for running custom scripts.
As #amit_g recommended, you can use Azure VM run command functionality to run install .net with scripts by PowerShell directly to meet your requirement.
This seems to work pretty well:
Invoke-AzureRmVMRunCommand -ResourceGroupName "$resourceGroupName" -Name "$machineName" -CommandId "RunPowerShellScript" -ScriptPath "configureMachine.ps1" -Parameter #{"machineName" = "$machineName"}
The contents of the Powershell script look something like this:
# The name of the VM is passed in as the first parameter.
param ($machineName)
if ($machineName -eq $null)
{
Write-Host "Usage: configureMachine -machineName <machineName>";
Exit;
}
# Download the agent installation files.
$agentZip="agent.zip";
Invoke-WebRequest -Uri "https://vstsagentpackage.azureedge.net/agent/2.181.1/vsts-agent-win-x64-2.181.1.zip" -OutFile $agentZip
# Unpack them.
$agentDirectory="$env:SystemDrive\azagent";
Add-Type -AssemblyName System.IO.Compression.FileSystem;
[System.IO.Compression.ZipFile]::ExtractToDirectory($agentZip, $agentDirectory);
# Configure the machine to work as a DevOps Agent.
&"$agentDirectory\config.cmd" --unattended --deploymentgroup --deploymentgroupname "Production" --agent "$machineName" --runasservice --work "_work" --url "https://dev.azure.com/theta-rex/" --projectname "openbook" --auth PAT --token te64yuv36tina2rvc2lsvwcsvctpwomiewz5fxihcubbdzaasoka
# Remove the Agent Zip files when installation is complete.
Remove-Item $agentZip;
# Download and install .NET 5.0
Invoke-WebRequest -Uri "https://dot.net/v1/dotnet-install.ps1" -OutFile "dotnet-install.ps1"
&"./dotnet-install.ps1" -Channel 5.0 -Runtime aspnetcore -InstallDir "C:\Program Files\dotnet"
The end result is a machine configured to participate in Azure DevOps and ASP.NET 5.0.
I'm looking for a way to run a script that creates a simple windows vm with rdp port and installs a software on it in order give our clients a simple sand boxed server for testing purposes. Is there a simple way to do that as I've been struggling for a while now.
You should use custom script extension for Azure VM. Depending on how you create vms you can use powershell\cli\arm templates\rest api\sdk to use that extension.
sample powershell:
$ProtectedSettings = #{"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File \\filesvr\build\serverUpdate1.ps1"};
Set-AzureRmVMExtension -ResourceGroupName myRG
-Location myLocation `
-VMName myVM `
-Name "serverUpdate"
-Publisher "Microsoft.Compute" `
-ExtensionType "CustomScriptExtension" `
-TypeHandlerVersion "1.9" `
-ProtectedSettings $ProtectedSettings
ARM Template sample: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-custom-script-windows
AZ cli sample: https://blogs.technet.microsoft.com/paulomarques/2017/02/13/executing-custom-script-extension-using-azure-cli-2-0-on-an-azure-linux-virtual-machine/
To do this with a script that is publicly available this is no Problem using:
$publicSettings = #{
"fileUris" = (,"$uri");
"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File azure_cse_vm_initial_script.ps1 $argument"
}
Write-Host " ==> Add-AzureRmVmssExtension"
Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss `
-Name "customScript" `
-Publisher "Microsoft.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-Setting $publicSettings
But how to do in case I use a storage account with a blob container? Can the access key be added to the Settings object? But how? And what to use for the URL.
The script I want to run should not be public accessible because it is the Installation script of my application.
Thanks,
Daniel
I would create a shared access signatur for that script (see Using shared access signatures). Then you can simple add the SAS token to the URI. E. g:
https://myaccount.blob.core.windows.net/sascontainer/sasblob.txt?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D
You could also use storage account name and storage account key to download the script: https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows#extension-schema