Azure Runbooks - 缺少 PowerShell Cmdlet 或未针对 VM 执行

Azure Runbooks - Missing PowerShell Cmdlets Or Not Executing Against a VM

我需要从 Azure Automation Runbook 在 VM 上执行 PowerShell,类似于 WinRm execution/PowerShell Remoting。

我已经通过 Azure Automation GUI 创建了一个 Azure Runbook,并且正在尝试 运行 一个完美适用于物理机和虚拟机的脚本,以获取关键系统信息和端口。我能够在 Azure 中进行身份验证,而且我似乎可以通过 Azure Runbook 执行脚本的某些方面(除非它只是 运行ning 针对 Azure Automation Worker),例如获取目标的已安装 PowerShell 版本虚拟机使用:$PSVersionTable.PSVersion 所以据我所知,security/access 没有问题。

然而,其他几个组件失败如下,我不知道是否需要将模块导入 Azure Automation,如果需要,导入哪些。或者,如果这是失败的,因为它是针对 Worker 而不是 VM。运行ning。

以下是我 运行ning:

的一些代码片段
$computerSystem = Get-CimInstance Win32_ComputerSystem
"CPU: " + $computerCPU.Name

Get-WmiObject -Class Win32_LogicalDisk |
    Where-Object {$_.DriveType -ne 5} |
    Sort-Object -Property Name | 
    Select-Object Name, VolumeName, FileSystem, Description, `
        @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
        @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}}, `
        @{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} |
    Format-Table -AutoSize

Get-NetAdapter -Name "*" | Format-Table

Get-NetOffloadGlobalSetting | Format-List

Test-NetConnection -Port 80

这是错误消息,我强烈怀疑这些错误消息要么是由于缺少我需要上传的 PowerShell 模块,但不确定在哪里可以找到这些错误消息,要么是我没有正确定位 VM 而是这种情况运行针对 AZ 主机? (如果是这样,任何关于如何定位单个 VM 的好例子):

Get-CimInstance : The specified service does not exist as an installed service.

Get-WmiObject : The specified service does not exist as an installed service.

Get-NetAdapter : The term 'Get-NetAdapter' is not recognized as the name of a cmdlet, function, script file, or operable program.

Get-NetOffloadGlobalSetting : The term 'Get-NetOffloadGlobalSetting' is not recognized as the name of a cmdlet, function, script file, or operable program.

Test-NetConnection : The term 'Test-NetConnection' is not recognized as the name of a cmdlet, function, script file, or operable program.

如果正确定位 VM 存在问题,我需要一些指导。我怀疑我的目标是 Worker 运行ning Runbook,而不是实际的 VM。我正在使用 RunAs account/the 新的 Azure Automation 安全方法(不是经典的),所以我不相信证书会发挥作用。这是我尝试定位 VM 的方式(我怀疑是 incorrect/should 被更改):

$Resources = Get-AzureRmResource -ResourceType "Microsoft.Compute/virtualMachines" -ResourceGroupName "MyTestGroup" -ResourceName "MyTestVM"
ForEach ($Resource in $Resources)
{
# PowerShell Code from Above here
}

更新 1:

既然我们已经确定我没有正确定位 VM,我尝试了 Joe 的建议,但是当我尝试 运行 以下内容时,我在 WinRm 上遇到错误。我找到了 Connect-AzureVM.ps1,但不确定这是旧的还是与我正在使用的较新的 RunAs 连接对齐。这是我当前尝试连接到 VM 并调用 PowerShell 的脚本。

param(      
        [parameter(Mandatory=$true)][String] 'https://myvmname.eastus.cloudapp.azure.com:5986,
        [parameter(Mandatory=$true)][String] 'MyVMName'        
        )

$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
    }
}

    # Get credentials to Azure VM 
    $Credential = Get-AutomationPSCredential -Name $VMCredentialName

Invoke-Command -ConnectionUri $Uri -Credential $Credential -ScriptBlock { 
# My PowerShell Here
}

这是脚本产生的错误。我怀疑是因为我需要在目标 VM 上 import/create 一个 WinRM 证书,但不确定 Connect-AzureVM.ps1 是否是要使用的正确脚本,或者是否有 another/more 更新的方法要使用对于 WinRM 访问:

[myvmname.eastus.cloudapp.azure.com] Connecting to remote server myvmname.eastus.cloudapp.azure.com failed with the following error message : WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network, and that a firewall exception for the WinRM service is enabled and allows access from this computer. By default, the WinRM firewall exception for public profiles limits access to remote computers within the same local subnet. For more information, see the about_Remote_Troubleshooting Help topic. + CategoryInfo : OpenError: (myvmname.eastus.cloudapp.azure.com:String) [], PSRemotingTransportException + FullyQualifiedErrorId : WinRMOperationTimeout,PSSessionStateBroken

您需要添加您想要 运行 这些脚本的 VM 作为 Azure 自动化混合工作者,以便您可以将脚本定位到 运行 它们上,或者您需要,从你的 运行书中 运行宁 Azure 自动化自己的工作人员,远程进入每个 VM 并从远程处理块内 运行 命令。

前者见:https://docs.microsoft.com/en-us/azure/automation/automation-hybrid-runbook-worker

对于梯子:

Invoke-Command -ConnectionUri $Uri -Credential $Credential -ScriptBlock { 
      $computerSystem = Get-CimInstance Win32_ComputerSystem
      "CPU: " + $computerCPU.Name

      Get-WmiObject -Class Win32_LogicalDisk |
      Where-Object {$_.DriveType -ne 5} |
      Sort-Object -Property Name | 
      Select-Object Name, VolumeName, FileSystem, Description, `
      @{"Label"="DiskSize(GB)";"Expression"={"{0:N}" -f ($_.Size/1GB) -as [float]}}, `
      @{"Label"="FreeSpace(GB)";"Expression"={"{0:N}" -f ($_.FreeSpace/1GB) -as [float]}}, `
      @{"Label"="%Free";"Expression"={"{0:N}" -f ($_.FreeSpace/$_.Size*100) -as [float]}} |
      Format-Table -AutoSize

      Get-NetAdapter -Name "*" | Format-Table

      Get-NetOffloadGlobalSetting | Format-List

      Test-NetConnection -Port 80
}

运行 在提升的提示符下在 VM 中执行此操作

https://gist.github.com/jeffpatton1971/2321f0db8025e48ad8ec13c243153045

在你的 运行 书中做你通常做的任何事情来连接它,但创建一些会话选项来传递你的 invoke-command。

$SessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck

Invoke-Command -ComputerName $VMname -Credential $Credential -UseSSL -SessionOption $SessionOption -ScriptBlock { }