Azure Powershell如何通过Runbook获取VM的运行服务? [英] Azure Powershell how to get running services of a VM via Runbook?

查看:87
本文介绍了Azure Powershell如何通过Runbook获取VM的运行服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写将启动VM的Azure Powershell Runbook,然后检查VM上的Windows服务是否正在运行并启动它.

I'm trying to write an Azure Powershell Runbook that will start a VM, and then check if a windows service on the VM is running or not and start it.

我可以启动VM,但是无法枚举服务.我是Azure Runbook的全新用户,所以我可能做错了什么.我将以下代码限制为仅Get-Service位,而不是VM启动.

I can get the VM started, but enumerating the services isn't working. I'm brand new on Azure Runbooks so I could be doing something wrong. I limited the below code to only the Get-Service bit and not the VM starting.

# Returns strings with status messages
[OutputType([String])]

param (
    [Parameter(Mandatory=$false)] 
    [String]  $AzureConnectionAssetName = "AzureRunAsConnection",

    [Parameter(Mandatory=$false)] 
    [String] $ResourceGroupName = ""
)

try {
    # Connect to Azure using service principal auth
    $ServicePrincipalConnection = Get-AutomationConnection -Name $AzureConnectionAssetName         

    Write-Output "Logging in to Azure..."

    $Null = Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $ServicePrincipalConnection.TenantId `
        -ApplicationId $ServicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint 
}
catch {
    if(!$ServicePrincipalConnection) {
        throw "Connection $AzureConnectionAssetName not found."
    }
    else {
        throw $_.Exception
    }
}

# If there is a specific resource group, then get all VMs in the resource group,
# otherwise get all VMs in the subscription.
if ($ResourceGroupName) { 
    $VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName
}
else { 
    $VMs = Get-AzureRmVM
}

# Try and enumerate the VM's services 
foreach ($VM in $VMs) {
        Write-Output "Listing all services..."
        Write-Output ("VM: {0}" -f $VM.Name)
        $VM | Get-Service | Format-Table -AutoSize

        Write-Output "Listing alternative method..."
        Get-Service -ComputerName $VM.Name | Format-Table -AutoSize
        Write-Output "Finished listing..."
}

输出为:

登录到Azure ...

Logging in to Azure...

列出所有服务...

VM:演示-0

列出替代方法...

商品详情...

推荐答案

在运行Azure Automation Runbook时,可以选择azure队列或创建混合工作程序.天蓝色队列对于许多进程而言都是很好的选择,但是它无法直接访问VM以运行诸如get-service之类的命令.

When you are running Azure Automation runbooks, you have the choice of the azure queue or creating a hybrid worker. The azure queue is good for many processes, but it will not have access to the VMs directly to run commands such as get-service.

要在@ 4c74356b41答案上进行扩展,您将需要使用远程Powershell执行命令(使用

To expand on @4c74356b41 answer, you will need to use remote powershell to execute the command (using New-PSSession) but you will also have to ensure that those commands are running on an Azure Automation Hybrid Worker

在下面的评论中,您询问了凭据.您可以在Azure自动化帐户中设置凭据,然后在创建新会话时让脚本使用它们.请参见此链接

In the comments below, you asked about credentials. You can set credentials in the Azure Automation account and then have them used by your script when creating a new session. See this link

这篇关于Azure Powershell如何通过Runbook获取VM的运行服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆