Azure - 通过 Azure PowerShell 运行手册和输入命令连接到远程 Linux VM

Azure - Connect to a remote Linux VM via Azure PowerShell runbooks and input commands

我正在尝试找到一种方法,通过我们的 Azure 自动化帐户 (PowerShell 运行book) 以编程方式 ssh 进入 Linux VM 并输入命令。这可能吗?我只是在查找有关如何在远程虚拟机上 运行 其他 PowerShell 命令的信息,但在这种情况下,它必须是 Linux 命令。
谢谢!

如果您想 运行 Azure linux VM 上的 shell 脚本,您可以使用功能 Custom Script extension。但它有一个限制。如果要使用 方式,则需要将命令存储在 public 位置(例如 Azure blob),然后提供位置。详情请参考here.

例如

$context=New-AzStorageContext -StorageAccountName "andyprivate" -StorageAccountKey ""


$container=Get-AzStorageContainer -Name "input" -Context $context

$stream=$container.CloudBlobContainer.GetBlockBlobReference("my.sh").OpenWrite()
$content = [system.Text.Encoding]::UTF8.GetBytes('apt-get -y update && apt-get install -y apache2')
$stream.write($content,0,$content.Length)
$stream.Flush()
$stream.close()


$sas=New-AzStorageBlobSASToken -Container "input" -Blob "my.sh"  -Permission r -Context $context
$url=$container.CloudBlobContainer.GetBlockBlobReference("my.sh").StorageUri.PrimaryUri.ToString() + $sas


$protectedSettings = @{"fileUris" = @($url); "commandToExecute" = "sh my.sh"};
Set-AzVMExtension  -ResourceGroupName 'testdata' -Name 'test' -VMName "testdocker"  -Location "eastasia" `
   -Publisher "Microsoft.Azure.Extensions" -ExtensionType "CustomScript" -ProtectedSettings $protectedSettings -TypeHandlerVersion 2.1