2017-02-01 40 views
1

我需要在Azure自動化Runbook的虛擬機上執行PowerShell,類似於WinRm執行/ PowerShell遠程處理。Azure Runbooks - 缺少PowerShell Cmdlet或未對虛擬機執行

我已經通過Azure自動化GUI創建了一個Azure Runbook,並且試圖運行一個完全針對物理和虛擬機的腳本來獲取關鍵系統信息和端口。我可以在Azure中進行身份驗證,並且似乎可以通過Azure Runbook執行腳本的某些方面(除非它僅針對Azure自動化工作者運行),例如使用如下方式獲取已安裝的目標VM的PowerShell版本:$PSVersionTable.PSVersion從我所知道的情況來看,我沒有安全/訪問方面的問題。

但是,其他幾個組件失敗如下,我不知道是否需要將模塊導入到Azure自動化,如果是,哪些是。或者,如果這是失敗的,因爲它對工人而不是虛擬機運行。

下面是一些代碼片段我運行:

$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模塊,我需要上傳,但我不確定在哪裏發現這些OR是這種情況,我沒有正確定位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.

如果搭配不當針對虛擬機的問題,我需要一些指導。我懷疑我的目標是運行Runbook的Worker,而不是實際的虛擬機。我正在使用RunAs帳戶/新的Azure自動化安全方法(不是經典的),所以我不相信證書會起作用。這裏是我正在努力的目標虛擬機(我懷疑是不正確/應該改變):

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

更新1:

現在我們已經確定,我不是針對VM正確地,我嘗試了喬的建議,但是當我嘗試運行以下時,WinRm上出現錯誤。我發現Connect-AzureVM.ps1,但我不確定這是舊的還是與我正在使用的較新的RunAs Connection對齊。這是我當前的腳本,它試圖連接到VM和Invoke 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上導入/創建WinRM證書,但不確定Connect-AzureVM.ps1是否是正確的腳本,或者是否有另一個/更新的方法用於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

+1

「來獲得關鍵的系統信息和端口「 - 你爲什麼試圖獲得有關運行你的Runbook的工作機器的信息?你可以包含你正在運行的腳本,或者它的簡化版本來顯示問題? – Joe

+0

也許問題在於我沒有針對Azure虛擬機,而是運行實際Runbook的工作人員。任何有關如何定位虛擬機的好例子 – Kode

回答

1

運行在高溫提示

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

從你的運行手冊裏面做什麼你通常要連接它,但創建一些會話選項來傳遞你的invoke-command。

$ SessionOption = NEW-PSSessionOption首-SkipCACheck -SkipCNCheck

調用命令-ComputerName $虛擬機名-Credential $憑據-UseSSL -SessionOption $ SessionOption -ScriptBlock {}

+1

在$中的這些引號前應該有反標記命令行。我猜想單個刻度線是代碼標記,對不起 –

+0

https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-windows-winrm –

1

您需要將要運行這些腳本的VM添加爲Azure Automation混合工作人員,以便您可以將腳本作爲目標運行於其上,或者需要從在Azure Automation自己的工作人員上運行的Runbook中運行腳本,遠程連接到每個虛擬機,並從遠程處理塊內運行命令。

對於前者,請參閱:https://docs.microsoft.com/en-us/azure/automation/automation-hybrid-runbook-worker

爲階梯:這裏面的VM

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 
} 
+0

這很有道理。兩者之間是否有偏好,因爲我的虛擬機在Azure中(與我的Azure自動化具有相同的資源組)我將測試並通知您結果 – Kode

+0

在調用該命令之前,是否需要添加WinRM證書或其他訪問? – Kode

+1

是的,請參閱https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-windows-winrm。確保您可以使用其他機器上的WinRM遠程訪問機器 - 從Azure Automation開始,這是確保它可以在常規機器上運行的第一個重要工具 – Joe