0

I want to Invoke a PowerShell script stored in Azure storage account to execute commands in the VM scale sets. I can able to invoke via cloudshell but with rubook it says Could not find file


Invoke-AzVmssVMRunCommand -ResourceGroupName $ResourceGroupName -VMScaleSetName $VirtualMachineScaleSetName -InstanceId $vminstances -CommandId 'RunPowerShellScript' -ScriptPath 'script.ps1'


script.ps1 contains the code which I want to run inside the VMS

script.ps1 is present in the $Home location in my storage account

I tried that solution

"Could not find file" Invoke-AzureRmVMRunCommand but got the same error

UDIT JOSHI
  • 1,298
  • 12
  • 26
  • 1
    How did it work via cloudshell ? Can you add that command? There does not seem to be any reference to a storage account. – Blue Clouds Nov 08 '21 at 19:08
  • How the runbook access to the storage account? – MoonHorse Nov 08 '21 at 19:42
  • @Blue Clouds I opened a cloud shell, created ps script and run the above command and give the path of the script and it executed inside VM – UDIT JOSHI Nov 09 '21 at 10:48
  • Could you check if wrapping the cmdlet call within an `InlineScript {}` block solves the problem. You can try something like this `$ValidationResult = InlineScript { $result = Invoke-AzVmssVMRunCommand -ResourceGroupName $USING:VM.ResourceGroupName -VMName $USING:VM.Name -CommandId "RunPowerShellScript" -ScriptPath $USING:ValidationScript }` add include the vmscaleset property and the instance id property in the mentioned code. As a workaround you can also use **Get-AzStorageFileContent** cmdlet to download a copy of the file and perform the same operation. Let me know if it helps. – SauravDas-MT Nov 09 '21 at 11:23

1 Answers1

1

Try the first approach provided by me in this question i.e.,

$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$ConnectToAzAccount = Add-AzAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

$StorageAccountName = "xxxxxxxxxxxxx"
$StorageAccountKey = "xxxxxxxxxxxxxx=="
$ContainerName = "xxxxxxxxxxxxxxx"
$BlobName_Windows = "samplescript.ps1"
$RG_VM = "xxxxxxxxxxxxxxxxxx"
$VM_Name_Windows = "xxxxxxxxx"
$InvokeCmd_Id_Windows = "RunPowerShellScript"

$AzStorage = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$AzStorageContext = $AzStorage.Context

$GetBlobContent_Windows = Get-AzStorageBlobContent -Container $ContainerName -Blob $BlobName_Windows -Destination ($Env:temp+"/samplescript.ps1") -Context $AzStorageContext -Force
$InvokeRunCmdOutput_Windows = Invoke-AzVMRunCommand -ResourceGroupName $RG_VM -VMName $VM_Name_Windows -CommandId $InvokeCmd_Id_Windows -ScriptPath ($Env:temp+"/samplescript.ps1")
$SampleScript_Output_Windows = $InvokeRunCmdOutput_Windows.Value[0].Message
Write-Output $SampleScript_Output_Windows
KrishnaG
  • 3,340
  • 2
  • 6
  • 16