I have 2 scenarios
- Execute test.ps1 powershell present inside azure VM – Works fine
- Execute test.ps1 powershell directly from azure devops repository – Error
Contents of testfile.ps1
Invoke-Sqlcmd -Query "SELECT * FROM table" -ServerInstance "instance" -Database "Dbname"
Scenario 1: To execute a powershell on azure windows machine below command works perfectly
- task: PowerShellOnTargetMachines@3
inputs:
Machines: 'servername'
UserName: 'username'
UserPassword: 'password'
InlineScript: |
$Username = "username"
$Password = ConvertTo-SecureString -String "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($Username, $Password)
Invoke-Command -ComputerName servername -Credential $cred -ConfigurationName configname -ScriptBlock {D:test.ps1}
CommunicationProtocol: 'Http'
Scenrio 2: To execute powershell directly from repository
- task: AzureCLI@2
displayName: 'testrun'
inputs:
azureSubscription: 'subname'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
az vm run-command invoke --command-id RunPowerShellScript --name servername-g RGname --scripts "@testfile.ps1"
For scenario 2 facing below error
Invoke-Sqlcmd : Cannot open database "DBname" requested by the login. The login failed.nLogin failed for user 'NT AUTHORITY\SYSTEM'
In Scenario 1 to avoid NTAUTHORITY authority (double-hop) issue we were using parameter ConfigurationName with stored credential in invoke-command
Invoke-Command -ComputerName servername -Credential $cred -ConfigurationName configname -ScriptBlock {D:test.ps1}
I am not sure how to use this ConfigurationName parameter in az vm run-command.
Could someone please help