[英]Azure VM Run PowerShell Script doesn't Output
I'm attempting to run a python script inside of my Windows 10 Azure VM.我正在尝试在我的 Windows 10 Azure VM 内运行 python 脚本。 I've managed to connect to the VM and run the script from an Automation runbook but nothing from the powershell script seems to be outputted after the runbook completes.我已设法连接到 VM 并从自动化运行手册运行脚本,但运行手册完成后似乎没有输出 powershell 脚本中的任何内容。
My python script stored at C:\Users\username\Desktop\test.py
:我的 python 脚本存储在C:\Users\username\Desktop\test.py
:
print("Hello World.")
My powershell script stored at C:\Users\username\Desktop\test.ps1
:我的 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..."
My Azure runbook named VMRunTest:我的 Azure 运行手册名为 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
Per the documentation it also requires that I open the output port on the VM to allow the 443
port with the AzureCloud
tag.根据文档,它还要求我在 VM 上打开 output 端口,以允许带有AzureCloud
标签的443
端口。 In the following image you what my setting are for that.在下图中,您的设置是什么。
When I execute the Azure Runbook, I get no errors, warnings, and no exceptions.当我执行 Azure Runbook 时,我没有收到任何错误、警告和异常。 This is the output that follows:这是下面的 output:
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 :
So, it appears to have been successful, however I don't see any mention of the Hello World.
所以,它似乎是成功的,但是我没有看到任何提到Hello World.
statement or either output statements from the powershell script.语句或 powershell 脚本中的 output 语句。 So, I can only assume that the python script is not executing.所以,我只能假设 python 脚本没有执行。 I also know this from trying this process on a python script that should take roughly ~15minutes to run and it comes back as completed within 1 minute.我也通过在 python 脚本上尝试这个过程来知道这一点,该脚本应该需要大约 15 分钟才能运行,并且它会在 1 分钟内完成。
I think I'm close, just missing a few minor details somewhere.我想我已经接近了,只是在某处遗漏了一些小细节。 Any help would be greatly appreciated.任何帮助将不胜感激。
I can reproduce your issue, actually it works.我可以重现您的问题,实际上它有效。
Change the line换行
Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
to至
$run = Invoke-AzureRmVMRunCommand -ResourceGroupName $rgname -Name $vmname -CommandId 'RunPowerShellScript' -ScriptPath ScriptToRun.ps1
Write-Output $run.Value[0]
Then you will be able to see the output(in my vm, I didn't install python, for a quick test, I just use powershell, in your case, it should also work).然后您将能够看到输出(在我的虚拟机中,我没有安装 python,为了快速测试,我只使用 powershell,在您的情况下,它应该也可以工作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.