powershell 从函数App调用AzVMRunCommand-参数为空

0qx6xfy6  于 2023-03-23  发布在  Shell
关注(0)|答案(2)|浏览(137)

我有一个队列触发的PowerShell函数应用程序,它试图通过Invoke-AzVMRunCommand运行位于Azure VM上的PowerShell脚本。我能够启动脚本,但参数为空!下面是队列触发的函数应用程序,非常简单:
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
}

下面是虚拟机上的脚本,如果您没有猜到的话,我正在使用azCopy将文件从存储帐户下载到此虚拟机:

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
}

我试过了

-Parameter @{ PDB = $PDB; BlobURI = $BlobURI }
-Parameter @{ "PDB" = $PDB; "BlobURI" = $BlobURI }
-Parameter @{ PDB = "$PDB"; BlobURI = "$BlobURI" }
-Parameter @{ "PDB" = "$PDB"; "BlobURI" = "$BlobURI" }
-Parameter $params
4uqofj5v

4uqofj5v1#

我面临着类似的问题,你我有一个脚本如下,我按照Microsoft-DocumentDoc 2

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

如果我执行,我不会得到包含在输出中的参数,如下所示:

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

作为替代方案,要获取我包含的参数,建议您使用Azure Cli命令,如下所示:

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

Reference用于在Function App中使用azure cli。

wr98u20j

wr98u20j2#

最后找到了如何做到这一点的帮助下,这篇文章here.
因此,技巧是不要使用-Parameter选项并使用参数设置脚本对象:

$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

相关问题