【发布时间】: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