gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:48:59 25 4
gpt4 key购买 nike

我需要从 Azure 自动化 Runbook 在虚拟机上执行 PowerShell,类似于 WinRm 执行/PowerShell 远程处理。

我已经通过 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 模块而导致的,但我不确定在哪里可以找到这些错误消息,或者是我没有正确定位虚拟机而是运行此错误消息对抗 AZ 主机? (如果是这样,请提供有关如何定位单个虚拟机的任何好示例):

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 的工作人员,而不是实际的虚拟机。我正在使用 RunAs 帐户/新的 Azure 自动化安全方法(非经典),因此我不相信证书会发挥作用。以下是我尝试定位虚拟机的方式(我怀疑这是不正确的/应该更改):

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

更新1:

现在我们已经确定我没有正确定位虚拟机,我尝试了 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
}

这是脚本产生的错误。我怀疑它是因为我需要在我的目标虚拟机上导入/创建 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

最佳答案

在虚拟机内以提升的提示符运行此命令

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

从 Runbook 内部执行通常的操作来连接它,但创建一些 session 选项来传递调用命令。

$SessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck

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

关于powershell - Azure Runbook - 缺少 PowerShell Cmdlet 或未针对 VM 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41984133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com