我需要在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
「來獲得關鍵的系統信息和端口「 - 你爲什麼試圖獲得有關運行你的Runbook的工作機器的信息?你可以包含你正在運行的腳本,或者它的簡化版本來顯示問題? – Joe
也許問題在於我沒有針對Azure虛擬機,而是運行實際Runbook的工作人員。任何有關如何定位虛擬機的好例子 – Kode