简体   繁体   English

Azure VM 运行 PowerShell 脚本不 Output

[英]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.

相关问题 通过SSH进入我的Vagrant虚拟机以运行python脚本…但是除非我在VM本身中,否则python脚本不起作用 - SSHed into my Vagrant virtual machine to run a python script…but the python script doesn't work unless I'm in the VM itself 部署Azure虚拟机启动时运行自定义脚本 - Run custom script when deployed Azure vm boots 如果脚本未正确运行,似乎无法将错误写入输出文件 - Can't seem to write error to output file if a script doesn't run correctly 无法通过 SSH 在远程 VM 上运行 Python 脚本 - Can't run Python Script on remote VM via SSH 当我重定向输出时,Python程序未在Docker容器的Shell脚本中运行 - Python program doesn't run in shell script in docker container when i redirect the output Azure python SDK 在虚拟机内部运行 powershell 脚本 - Azure python SDK run powershell script inside a Virtual Machine PowerShell:从Azure Blob存储运行Python脚本 - PowerShell: run Python script from Azure blob storage Azure VM 运行命令使用 Python SDK 执行但脚本不运行 - Azure VM Run Command using Python SDK executing but script doesnt run 自动脚本没有 output 数据 - Automatic script doesn't output data Django Python脚本不输出文件 - Django Python Script Doesn't Output Files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM