繁体   English   中英

Azure VM 运行 PowerShell 脚本不 Output

[英]Azure VM Run PowerShell Script doesn't Output

我正在尝试在我的 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 运行手册名为 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 上打开 output 端口,以允许带有AzureCloud标签的443端口。 在下图中,您的设置是什么。

当我执行 Azure Runbook 时,我没有收到任何错误、警告和异常。 这是下面的 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      : 

所以,它似乎是成功的,但是我没有看到任何提到Hello World. 语句或 powershell 脚本中的 output 语句。 所以,我只能假设 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]

然后您将能够看到输出(在我的虚拟机中,我没有安装 python,为了快速测试,我只使用 powershell,在您的情况下,它应该也可以工作)。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM