New to PowerShell,
Context: Trying to execute a ShellScript
file reside inside an AzureVM
(OS Type Linux) from Azure Pipeline using PowerShell.
Script Sample:
function getConnectionToRemoteMachine ($AzuresecretValue,$AzureApplicationID,$AzureObjectID,$AzureDirectoryID,$AzureUserName,$AzureSubscriptionID) {
$password = ConvertTo-SecureString $AzuresecretValue -AsPlainText -Force;
$VMResourceGroup = "npd-test-rg01";
#Connecting to the Azure VM using the Service Principle
$pscredential = New-Object -TypeName System.Management.Automation.PSCredential($AzureApplicationID, $password);
Connect-AzAccount -ServicePrincipal -Tenant $AzureDirectoryID -Credential $pscredential | Out-null;
$VMList = Get-AzVm -ResourceGroupName $VMResourceGroup -Status;
ForEach($VM in $VMList) {
if ($VM.Name -eq "npd-test-app01"){
Write-Host "Test Machine traced!!!"
}
else {
continue;
}
}
}
function triggerShellScript () {
$AzuresecretValue = "<AzureSecret>";
$AzureApplicationID = "<AzureApplicationID>";
$AzureObjectID = "<AzureObjectID>";
$AzureDirectoryID = "<AzureDirectoryID>";
$AzureUserName = "SVCUSER";
$AzureSubscriptionID = "<AzureSubscriptionID>";
getConnectionToRemoteMachine $AzuresecretValue $AzureApplicationID $AzureObjectID $AzureDirectoryID $AzureUserName $AzureSubscriptionID
}
triggerShellScript
Able to trace the VM, but don't know how to connect to the machine and trigger the shell script under /home/TestRun/TriggerTest.sh
CodePudding user response:
You can run the Shell Script in OS Linux using the Azure Cloud Shell.
Step 1: Created a file script.sh
using command code script.sh
and pasted the command and save and close editor.
Step 2: use the below command to run the above Script in your VM.
Invoke-AzVMRunCommand -ResourceGroupName '<myResourceGroup>' -Name '<myVMName>' -CommandId 'RunShellScript' -ScriptPath '<pathToScript>'
Refernce : https://docs.microsoft.com/en-us/azure/virtual-machines/linux/run-command