0

当我远程进入我的虚拟机执行这个 Powershell 脚本时,一切正常。

但是,如果我尝试在我的 Azure Runbook 中运行脚本,它将执行,但 Python 脚本不会触发,我不知道为什么:( 它似乎只是跳过它。我错过了什么吗?

这是我的 Runbook 中的代码

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection 
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        "are we here"
        throw $_.Exception

    }
}
$rgname ="MyResourceName"
$vmname ="MyVirtualMachine"
$ScriptToRun = "c:\mypath\myscript.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
$run = Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Write-Output $run.Value[0]
Remove-Item -Path ScriptToRun.ps1

这是 myscript.ps1(Python 的路径设置为环境变量)

Write-Output "Script Started."
Python C:\myscripts\pythonscript.py
Write-Output "Script Ended." 

然后我会看到这个输出

Script Started.
Script Ended.

py 脚本的基本输出应该在中间,但我什么也没得到。同样,如果我在本地运行它,它工作正常。

感谢您的任何帮助

4

1 回答 1

0

尝试将脚本迁移到新Az模块,确保已在自动化帐户中安装Az.Accounts, 。Az.Compute

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection 
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
    "Logging in to Azure..."
    Connect-AzAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        "are we here"
        throw $_.Exception

    }
}
$rgname ="MyResourceName"
$vmname ="MyVirtualMachine"
$ScriptToRun = "c:\mypath\myscript.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
$run = Invoke-AzVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Write-Output $run.Value[0]
Remove-Item -Path ScriptToRun.ps1
于 2021-06-03T02:38:15.383 回答