How to reach a script file in a VM from Powershell runbook

E.C

I have a script file in a VM that I want to reach from the azure portal in a Powershell runbook but it can't find the file. The file is manually saved in c:\ of the VM.

Code snippet in azure runbook:

IF ($VmAction -eq "Shutdown") {
     try
    {
         Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VmName -CommandId 'RunPowerShellScript' -ScriptPath 'C:\\stopservice.ps1' -ErrorVariable result
         Stop-AzVM -Name $VmName -ResourceGroupName $ResourceGroupName -Force
    }
    catch
    {
        throw "Error: $($_.Exception.Message)"
    }

The command "Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VmName -CommandId 'RunPowerShellScript' -ScriptPath 'C:\stopservice.ps1' -ErrorVariable result" can be used on powershell on my computer so I think the issue is somewhere in Azure possibly.

script file (c:\stopservice.ps1):

  try
    {
        Stop-Service -Name SSASTELEMETRY
    }

    catch
    {
        throw "Error: $($_.Exception.Message)"
    }

(ps. the service name is for testing purposes, will stop different service when this works)

KrishnaG-MSFT

Have your Azure Automation runbook something like shown below. It should accomplish your requirement.

$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzAccount -ServicePrincipal -TenantId $ServicePrincipalConnection.TenantId -ApplicationId $ServicePrincipalConnection.ApplicationId -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
$rgname ="rrrrrrrrrrrrrr"
$vmname ="vvvvvvvvvvvvvv"
$ScriptToRun = "c:\test.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1 
Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Remove-Item -Path ScriptToRun.ps1

Note: Before you run your runbook, make sure you update "rrrrrrrrrrrrrr" with your resource group name and "vvvvvvvvvvvvvv" with your VM name and also make sure you have the PowerShell named test.ps1 in C:\ drive of the VM.

Hope this helps! Cheers!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Azure Runbook - Create from gallery using a Powershell script

Azure runbook - run Powershell on remote VM

How to run a PowerShell script from a batch file

How to assign variables to a Powershell script from a file?

How to run powershell script file from JScript?

Deploy an Azrue VM from an ARM template via Powershell Runbook without downloading template

how to reach the parent from vagrant/libvirt/qemu vm?

Terraform on Azure: executing powershell script for Windows VM from local file with variables as arguments

How to fix psexec not running PowerShell script on VM?

Use Azure file share in PowerShell Runbook

How to execute a powershell script from robot framework file?

How do I run a PowerShell script on a file from the context menu?

How to generate comprehensive log file from PowerShell script

How to invoke PowerShell script from command prompt that updates XML file?

How to run powershell script from .ps1 file?

How to pass variable from Batch File to a Powershell Script

how to reach the end of file

How to create automation schedule for Azure Automation Runbook from PowerShell to be run on Hybrid Worker?

How to transfer a html file from Azure VM via Azure powershell or Azure CLI to a local machine

Run Azure VM Extension Powershell script from git repository

powershell script for connecting HyperV VMM and create a VM from specific template

how to copy a lines from a file a file after matched string in a file using powershell script

How Connect to a Azure Windows VM and run a remote script with PowerShell?

How to get ResourceId of VM script is running on in Azure with PowerShell?

Azure Powershell Tagging VM's from CSV file

Call .bat file from powershell script synchronously

Passing parameters to powershell script from batch file

Calling a PowerShell script from a batch file

Powershell script to test zip passwords from file