如何解决为什么我的 Azure Runbook 不能通过 Powershell 执行我的 Python 脚本
当我远程进入我的虚拟机执行这个 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 脚本的基本输出应该在中间,但是我什么也没得到。同样,如果我在本地运行它,它工作正常。
感谢您的帮助
解决方法
尝试将您的脚本迁移到新的 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。