【问题标题】:Azure Powershell how to get running services of a VM via Runbook?Azure Powershell 如何通过 Runbook 获取 VM 的运行服务?
【发布时间】:2017-05-02 14:00:47
【问题描述】:

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

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

# 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...

列出所有服务...

虚拟机:demo-0

列出替代方法...

已完成列表...

【问题讨论】:

  • 您使用的是混合工作人员吗?还是您使用的是默认的 Azure 队列?
  • 我会说默认的 Azure 队列

标签: powershell azure azure-powershell


【解决方案1】:

嗯,首先,启动虚拟机是异步的,所以您需要等待虚拟机实际启动,而Get-Service 无论如何也不起作用,因为要从您需要验证的虚拟机获取服务那个虚拟机,所以要么是用户 PSsessions 要么是调用命令,类似的东西。只需看看如何使用 powershell 远程访问服务器或如何向远程 PC 发出 powershell 命令。这个案子没有什么不同。它与 Azure 自动化的工作方式无关。

【讨论】:

  • 啊,我明白了。有没有办法以某种方式授予自动化帐户权限,这样我就不必在 powershell 中存储用户/密码?
  • 不,没有办法做到这一点,除非您使用混合工作人员,否则您可以使其“在内部”工作,因此使用域身份验证。
【解决方案2】:

运行 Azure 自动化 Runbook 时,您可以选择使用 azure 队列或创建混合工作线程。 azure 队列适用于许多进程,但它无法直接访问 VM 以运行 get-service 等命令。

要扩展@4c74356b41 答案,您将需要使用远程powershell 来执行命令(使用New-PSSession),但您还必须确保这些命令在Azure Automation Hybrid Worker 上运行

在下面的 cmets 中,您询问了凭据。您可以在 Azure 自动化帐户中设置凭据,然后在创建新会话时让脚本使用它们。见this link

【讨论】:

  • 这不是一个很好的解释,不够清楚,不可利用
【解决方案3】:

您可以尝试使用以下 cmdlet。它对我有用。

# Try and enumerate the VM's services 
foreach ($VM in $VMs) {
        Write-Output "Listing all services..."
        Write-Output ("VM: {0}" -f $VM.Name)
        $ResourceGroupName=$VM.ResourceGroupName
        $Name=$VM.Name
        $status=(Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $Name -Status).Statuses[1].code
        if($status -like "PowerState/deallocated")
        {
            Write-Output "VM is stopped, starting VM"
            if (Start-AzureRmVm -ResourceGroupName $ResourceGroupName -Name $Name)
            {
                Write-Output "Start VM successfuly"
            }else
            {
                Write-Output "Start VM failed"
                break
            }

        }
        ##get VM's Public IP
        $nicName = ($VM.NetworkInterfaceIDs[0] -split '/')[-1]
        $pip = (Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $ResourceGroupName).IpConfigurations.publicipaddress.id
        $PublicIP=(Get-AzureRmPublicIpAddress -ResourceGroupName shui -Name ($pip -split '/')[-1]).IpAddress

        $Uri="http://$($PublicIP):5986"
        Write-Output "Get ConnectionUri $Uri"

        ##get Credential from assets
        $shui=Get-AutomationPSCredential -Name 'shui' 

        $session=New-PSSession -ConnectionUri $Uri -Credential $shui -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

        $result=Invoke-Command -Session $session -ScriptBlock {ipconfig}
        Write-Output "$result"
        Write-Output "Finished listing..."
}

在使用此脚本之前。首先,你应该在你的VM的防火墙和NSG上打开5896端口,你可以检查如下link。请确保您可以在本地 PC 上telnet IP 5986

  1. 您应该将AzureRM.Network 模块导入您的自动化帐户。更多关于如何导入 Modules 的信息请参考link

3.将你的虚拟机密码保存到Runbook,你可以参考这个link。当你想使用凭证时,你可以使用下面的脚本:

##get Credential from assets
$shui=Get-AutomationPSCredential -Name 'shui' 

$session=New-PSSession -ConnectionUri $Uri -Credential $shui -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

【讨论】:

  • 我对你投了赞成票,这都是很好的信息!我真的需要深入研究一下。
猜你喜欢
  • 1970-01-01
  • 2019-06-11
  • 2018-03-21
  • 2017-08-31
  • 1970-01-01
  • 2019-05-05
  • 2021-07-17
  • 2021-02-18
  • 1970-01-01
相关资源
最近更新 更多