python - Azure VM 运行 PowerShell 脚本不输出
问题描述
我正在尝试在我的 Windows 10 Azure VM 中运行 python 脚本。我已设法连接到 VM 并从自动化运行手册运行脚本,但运行手册完成后似乎没有输出 powershell 脚本中的任何内容。
我的 python 脚本存储在C:\Users\username\Desktop\test.py
:
print("Hello World.")
我的 powershell 脚本存储在C:\Users\username\Desktop\test.ps1
:
Write-Output "Starting Script..."
C:\Users\username\Python\python.exe C:\Users\username\Desktop\test.py
Write-Output "Shutting Down..."
我的 Azure Runbook 名为 VMRunTest:
$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
throw $_.Exception
}
}
$rgname ="ParcelTracking"
$vmname ="ParcelTracking-Scraper"
$ScriptToRun = "C:\Users\username\Desktop\test.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Remove-Item -Path ScriptToRun.ps1
根据文档,它还要求我打开 VM 上的输出端口以允许443
带有AzureCloud
标签的端口。在下图中,您的设置是什么。
当我执行 Azure Runbook 时,我没有收到任何错误、警告和异常。这是以下输出:
Logging in to Azure...
Environments
------------
{[AzureChinaCloud, AzureChinaCloud], [AzureCloud, AzureCloud], [AzureGermanCloud, AzureGermanCloud], [AzureUSGovernme...
Value : {Microsoft.Azure.Management.Compute.Models.InstanceViewStatus,
Microsoft.Azure.Management.Compute.Models.InstanceViewStatus}
Name :
StartTime :
EndTime :
Status : Succeeded
Error :
Output :
Capacity : 0
Count : 0
Item :
因此,它似乎已经成功,但是我没有看到任何提及该Hello World.
语句或来自 powershell 脚本的任何输出语句。所以,我只能假设 python 脚本没有执行。我也通过在 python 脚本上尝试这个过程来知道这一点,这个过程大约需要 15 分钟才能运行,并且它会在 1 分钟内完成。
我想我已经接近了,只是在某处遗漏了一些小细节。任何帮助将不胜感激。
解决方案
我可以重现您的问题,实际上它有效。
换行
Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
至
$run = Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Write-Output $run.Value[0]
然后您将能够看到输出(在我的 vm 中,我没有安装 python,为了快速测试,我只使用 powershell,在您的情况下,它也应该可以工作)。
推荐阅读
- javascript - 如何判断 IndexedDB 何时手动关闭?
- cmd - 如何退出 anaconda 提示并返回到 cmd 级别?
- java - 如何使用创建导航抽屉
- jquery - 删除点击延迟
- powershell - 在 Powershell 中读取 zip 文件(openread)
- javascript - 在编辑之前更改文本框以具有其他文本框的值
- string - 如何在熊猫系列中的特定行上进行字符串替换
- javascript - children().hasClass () 找不到类
- flutter - 如何从 Dart 中的列表中删除空列表?
- django - 设置 django 项目时出现 Gunicorn 错误 203