Azure VM运行PowerShell脚本未输出

2024-06-09 11:01:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的Windows10Azure虚拟机中运行python脚本。我已成功连接到VM并从自动化runbook运行脚本,但在runbook完成后,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..."

名为VMRunTest的Azure 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
        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

对于documentation,它还要求我打开VM上的输出端口,以允许带有AzureCloud标记的443端口。在following image中,您可以了解我的设置

当我执行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分钟内完成

我想我很接近了,只是遗漏了一些小细节。任何帮助都将不胜感激


Tags: test脚本outputserviceusernameazureuserswrite
1条回答
网友
1楼 · 发布于 2024-06-09 11:01:52

我可以复制你的问题,实际上它是有效的

换线

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,在您的情况下,它也应该可以工作)

enter image description here

相关问题 更多 >