UDIT JOSHI
UDIT JOSHI

Reputation: 1378

How to invoke PS script stored in File share with azure run-book

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

Upvotes: 0

Views: 1230

Answers (1)

KrishnaG
KrishnaG

Reputation: 3484

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

Upvotes: 1

Related Questions