gpt4 book ai didi

powershell - 运行在虚拟机上执行 PowerShell 命令的 Azure Runbook 时出错

转载 作者:行者123 更新时间:2023-12-03 03:04:15 24 4
gpt4 key购买 nike

我尝试在 Runbook 中执行此代码,使用“Invoke-Command”连接到虚拟机。

$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

# Use the subscription that this Automation account is in
$null = Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionID
Get-AzureRmVM | Select Name
$dcred = Get-AutomationPSCredential -Name 'myvm1creds'
Write-Output $DomainCred
$opts = New-PSSessionOption -SkipCACheck
Invoke-Command -Computername 'myVM1' -Credential $dcred -ScriptBlock {Get-Process} -SessionOption $opts
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}

出现以下错误:

[myVM1] 连接到远程服务器 myVM1 失败,并显示以下错误消息:WinRM 客户端无法处理要求。如果身份验证方案与 Kerberos 不同,或者客户端计算机未加入域,那么必须使用 HTTPS 传输,或者必须将目标计算机添加到 TrustedHosts 配置设置中。使用 winrm.cmd 配置 TrustedHosts。请注意,TrustedHosts 列表中的计算机可能未经过身份验证。你可以通过运行以下命令获取更多信息:winrm help config。欲了解更多信息,请参阅about_Remote_Troubleshooting 帮助主题。 + 类别信息:OpenError:(myVM1:字符串)[],PSRemotingTransportException +FullyQualifiedErrorId:ServerNotTrusted,PSSessionStateBroken

知道需要做什么才能通过 Azure 虚拟机上的 Runbook 运行 powershell 脚本

最佳答案

在 Azure Runbook 中,我们无法使用传输 HTTP 来连接 Azure VM,因为 Azure Runbook 无法添加信任主机,因此我们需要使用 HTTPS 来连接 Azure VM。

这是我的步骤:
1.创建自签名证书。

使用makecert.exe创建它。

2.配置Winrm监听HTTPS,在CMD中运行此脚本:

winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Port="5986" ;Hostname="jasonvm" ;CertificateThumbprint="98941E137CDF9553CCB0C28D5814EB9EDB1AC87D"}

3.在Azure NSG入站规则和Windows防火墙入站规则中添加端口5986。4.我们可以使用此 Runbook 连接 Azure VM:

$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


$null = Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionID
Get-AzureRmVM | Select Name
$dcred = Get-AutomationPSCredential -Name 'jasonvm'
Write-Output $DomainCred
$opts = New-PSSession -ConnectionUri 'https://52.185.148.177:5986' -Credential $dcred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
Invoke-Command -Session $opts -ScriptBlock {Get-Process}

}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}

这是我的结果:

enter image description here

关于powershell - 运行在虚拟机上执行 PowerShell 命令的 Azure Runbook 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45978965/

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