0

I have a queue-triggered Powershell function app that is trying to run a PowerShell script located on an Azure VM via Invoke-AzVMRunCommand. I'm able to launch the script but the parameters are blank! Here's the Queue triggered Function App, pretty straightforward:

FunctionApp:

# Input bindings are passed in via param block.
param($QueueItem, $TriggerMetadata)
$ErrorActionPreference = "Stop"
try 
{
  $SasToken = $Env:SasToken

  $PDB = $QueueItem['PDB']
  $FileName = $QueueItem['FileName']

  $StorageAccount = 'https://STORAGENAME.blob.core.windows.net/CONTAINER/'
  $BlobURI = "{0}{1}{2}" -f $StorageAccount, $FileName, $sasToken

  $ScriptPath = "F:\azCopy\CopyToDump.ps1"
  $ScriptFile = "CopyToDump.ps1"

  $params = @{
    "PDB" = $PDB;
    "BlobURI" = $BlobURI
  }

  # When using "-ScriptPath $ScriptPath", kept getting errors with path:
  #   "Could not find path F:..." from a thread somewhere on here, they mentioned
  #   to us this Out-File -InputObject to which I have no understanding!!!
  Out-File -InputObject $ScriptPath -FilePath ScriptToRun.ps1

  Invoke-AzVMRunCommand `
    -ResourceGroupName 'RESOURCE_GROUP' `
    -VMName 'VM_NAME' `
    -CommandId 'RunPowerShellScript' `
    -ScriptPath ScriptToRun.ps1 `
    -Parameter @{ PDB = $PDB; BlobURI = $BlobURI }

  Remove-Item -Path ScriptToRun.ps1
  Write-Host "Done"
} catch {
  $_.Exception.Message
}

And here is the script on the VM, and if you hadn't guessed - I'm using azCopy to download a file from a storage account to this VM:

param(
  [string]$PDB,
  [string]$BlobURI
)

$logFile = "F:\azcopy\Info.txt"
Function Write-Log {
    param(
        [Parameter(Mandatory = $true)][string] $message,
        [Parameter(Mandatory = $false)]
        [ValidateSet("INFO", "WARN", "ERROR")]
        [string] $level = "INFO"
    )   
    # Create timestamp
    $timestamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    # Append content to log file
    Add-Content -Path $logFile -Value "$timestamp [$level] - $message"
}

try
{
  $folder = switch ($PDB)
  {
    'QA' {'094D2AA1A909459BA36ABEEA7F7732F6'}
    'DEV' {'0B2305B6305547F389F96C800630CD21'}
  }

  $dpdump = "F:\app\oracle\admin\hs\dpdump\$folder"

  Write-Log -level INFO -message  "---------------------------"
  Write-Log -level INFO -message  "Copying:  $BlobURI"
  Write-Log -level INFO -message  "To folder $dpdump"
  Write-Log -level INFO -message  "---------------------------"
  Write-Log -level INFO -message  "F:\azCopy\AzCopy.exe copy $BlobURI $dpdump --overwrite=ifsourcenewer --check-md5=FailIfDifferent"
  Write-Log -level INFO -message  "---------------------------"

  & F:\azCopy\AzCopy.exe copy $BlobURI $dpdump --overwrite=ifsourcenewer --check-md5=FailIfDifferent
} catch {
  Write-Log -level ERROR -message  "Error running script"
  Write-Log -level ERROR -message  $_.Message
  exit -1
}

I've tried:

-Parameter @{ PDB = $PDB; BlobURI = $BlobURI }
-Parameter @{ "PDB" = $PDB; "BlobURI" = $BlobURI }
-Parameter @{ PDB = "$PDB"; BlobURI = "$BlobURI" }
-Parameter @{ "PDB" = "$PDB"; "BlobURI" = "$BlobURI" }
-Parameter $params
Tom
  • 162
  • 8

2 Answers2

0

I am facing similar issue as you I have a script as below and I followed Microsoft-Document and Doc 2:

param(
     [string]$ar1,
     [string]$ar2
  )
Write-Host This is a sample script with parameters $ar1 and $ar2

If I execute, I don't get parameters included in output as below:

Invoke-AzVMRunCommand  -ResourceGroupName 'RESOURCEGROUP Name'  -VMName 'vm1'   -CommandId 'RunPowerShellScript'   -ScriptPath ScriptToRun.ps1   -Parameter @{arg1 = "rithwik";arg2 = "bojja"} 

enter image description here

As an Alternative, To get Parameters I have Included and would suggest you use Azure Cli commands as below:

az vm run-command invoke  --command-id RunPowerShellScript --name "vm1" -g "RESOURCEGROUP Name" --scripts "C:\test.ps1  rithwik bojja"     

enter image description here

Reference for using azure cli in Function App.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • thanks for the response, I did see that thread and thought there has to be an easier way - I guess not. Can I ask how you ended up getting the Azure CLI installed? Did you just download the azure CLI tar.gz file and extract to kudu? – Tom Feb 23 '23 at 15:25
  • Yeah you are right – RithwikBojja Feb 23 '23 at 15:58
  • great - so I copied the az cli from my local system to the vm via kudu but now it's throwing an az login message. So for testing purposes just to see if the script was getting the arguments I hard-coded my uname and pw and it threw a "you must use multi-factor authentication to access." Does anyone know how to give access to us az in a function app? – Tom Feb 23 '23 at 18:25
0

Finally found out how to do this with the help of this post here.

So the trick is NOT to use the -Parameter option and set the script object with the parameters:

 $ScriptPath = "F:\azCopy\CopyToDump.ps1"

 # Append arguments directly to the script path for a positional argument passing
 $ScriptFile = "$ScriptPath $PDB $FileName"

 # Create a the actual script-to-run as a new PS script
 Out-File -InputObject $ScriptFile -FilePath .\ScriptToRun.ps1
 
 Invoke-AzVMRunCommand `
    -ResourceGroupName $ResourceGroup `
    -VMName $vmName `
    -CommandId 'RunPowerShellScript' `
    -ScriptPath ScriptToRun.ps1
Tom
  • 162
  • 8