【问题标题】:Installing Azure powershell in an azure Virtual Machine在 Azure 虚拟机中安装 Azure Powershell
【发布时间】:2015-09-04 11:14:11
【问题描述】:

我需要编写一个 powershell 工作流来创建一个 Azure 虚拟机并在该 Azure 虚拟机中执行一些 azure cmdlet。但是新创建的 VM 中没有安装 azure powershell 模块。我的代码是这样的

    New-AzureQuickVM -Windows -ServiceName $serviceName -Name $vmname -ImageName $VMImage  -Password $password -AdminUserName $username -InstanceSize "ExtraSmall" -WaitForBoot

    $WinRmUri = Get-AzureWinRMUri -ServiceName $serviceName -Name $vmname
    $Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

    Invoke-Command -ConnectionUri $WinRmUri -Credential $Cred -ScriptBlock {
         Add-AzureAccount ......  ## These cmdlets need Azure Powershell Module 
         Set-AzureSubscription........
         New-AzureStorageAccount......
    }

我不应该手动获取该 VM 的 rdp 并打开它来安装 Azure Powershell 模块,而是使用 powershell cmdlet 动态创建一个 VM 并使用 powershell 本身在该 vm 中安装 azure 模块。

【问题讨论】:

    标签: powershell azure azure-virtual-machine azure-powershell


    【解决方案1】:

    您可以使用 Azure 自动化服务将您的 Powershell 代码实现到运行手册中。

    http://azure.microsoft.com/en-us/documentation/services/automation/

    【讨论】:

    • 对不起,我应该清楚地解释我的必要性。我不认为我可以使用 Runbooks。我需要一个虚拟机。整个过程就像使用 ISCSI 连接 Azure 虚拟设备和 VM,然后访问卷中的数据并进行一些与 azure 相关的操作。
    【解决方案2】:

    这可以通过 ARM(Azure 资源管理器)模板轻松完成。这是一个 JSON 模板,用于定义要部署的对象。在您的情况下,您可能希望部署具有自定义脚本扩展的 VM。预配 VM 后,Azure 资源管理器将获取提供的文件并运行您的自定义 powershell。请参阅下面的示例,并将行 https://<YOUR-BLOB-HERE>.blob.core.windows.net/resources/CUSTOM-POWERSHELL-SCRIPT.ps1 替换为您的 blob 和 powershell 脚本。要运行脚本,您可以使用 Azure powershell,如下所述:https://azure.microsoft.com/en-us/documentation/articles/powershell-azure-resource-manager/

    用于您的关键 cmdlet 是 New-AzureResourceGroup。调用将类似于:

    Switch-AzureMode -Name AzureResourceManager
    New-AzureResourceGroup -Name TestRG1 -Location "West US" -TemplateFile <YOUR-JSON-ARM-TEMPLATE>.json
    

    在此处查看 ARM 模板列表以供参考:https://github.com/Azure/azure-quickstart-templates。修改示例模板以运行自定义代码/安装 Azure powershell。

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "newStorageAccountName": {
                "type": "string",
                "metadata": {
                    "description": "Unique DNS Name for the Storage Account where the Virtual Machine's disks will be placed."
                }
            },
            "adminUsername": {
                "type": "string",
                "metadata": {
                    "description": "Username for the Virtual Machine."
                }
            },
            "adminPassword": {
                "type": "securestring",
                "metadata": {
                    "description": "Password for the Virtual Machine."
                }
            },
            "dnsNameForPublicIP": {
                "type": "string",
                "metadata": {
                    "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
                }
            },
            "windowsOSVersion": {
                "type": "string",
                "defaultValue": "2012-R2-Datacenter",
                "allowedValues": [
                    "2008-R2-SP1",
                    "2012-Datacenter",
                    "2012-R2-Datacenter"
                ],
                "metadata": {
                    "description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version. Allowed values: 2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter."
                }
            }
        },
        "variables": {
            "location": "West US",
            "imagePublisher": "MicrosoftWindowsServer",
            "imageOffer": "WindowsServer",
            "OSDiskName": "osdiskforwindowssimple",
            "nicName": "myVMNic",
            "addressPrefix": "10.0.0.0/16",
            "subnetName": "Subnet",
            "subnetPrefix": "10.0.0.0/24",
            "storageAccountType": "Standard_LRS",
            "publicIPAddressName": "myPublicIP",
            "publicIPAddressType": "Dynamic",
            "vmStorageAccountContainerName": "vhds",
            "vmName": "MyWindowsVM",
            "vmSize": "Standard_A2",
            "virtualNetworkName": "MyVNET",
            "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
            "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]"
        },
        "resources": [
            {
                "type": "Microsoft.Storage/storageAccounts",
                "name": "[parameters('newStorageAccountName')]",
                "apiVersion": "2015-05-01-preview",
                "location": "[variables('location')]",
                "properties": {
                    "accountType": "[variables('storageAccountType')]"
                }
            },
            {
                "apiVersion": "2015-05-01-preview",
                "type": "Microsoft.Network/publicIPAddresses",
                "name": "[variables('publicIPAddressName')]",
                "location": "[variables('location')]",
                "properties": {
                    "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
                    "dnsSettings": {
                        "domainNameLabel": "[parameters('dnsNameForPublicIP')]"
                    }
                }
            },
            {
                "apiVersion": "2015-05-01-preview",
                "type": "Microsoft.Network/virtualNetworks",
                "name": "[variables('virtualNetworkName')]",
                "location": "[variables('location')]",
                "properties": {
                    "addressSpace": {
                        "addressPrefixes": [
                            "[variables('addressPrefix')]"
                        ]
                    },
                    "subnets": [
                        {
                            "name": "[variables('subnetName')]",
                            "properties": {
                                "addressPrefix": "[variables('subnetPrefix')]"
                            }
                        }
                    ]
                }
            },
            {
                "apiVersion": "2015-05-01-preview",
                "type": "Microsoft.Network/networkInterfaces",
                "name": "[variables('nicName')]",
                "location": "[variables('location')]",
                "dependsOn": [
                    "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
                    "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
                ],
                "properties": {
                    "ipConfigurations": [
                        {
                            "name": "ipconfig1",
                            "properties": {
                                "privateIPAllocationMethod": "Dynamic",
                                "publicIPAddress": {
                                    "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
                                },
                                "subnet": {
                                    "id": "[variables('subnetRef')]"
                                }
                            }
                        }
                    ]
                },            
            },
            {
                "apiVersion": "2015-05-01-preview",
                "type": "Microsoft.Compute/virtualMachines",
                "name": "[variables('vmName')]",
                "location": "[variables('location')]",
                "dependsOn": [
                    "[concat('Microsoft.Storage/storageAccounts/', parameters('newStorageAccountName'))]",
                    "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
                ],
                "properties": {
                    "hardwareProfile": {
                        "vmSize": "[variables('vmSize')]"
                    },
                    "osProfile": {
                        "computername": "[variables('vmName')]",
                        "adminUsername": "[parameters('adminUsername')]",
                        "adminPassword": "[parameters('adminPassword')]"
                    },
                    "storageProfile": {
                        "imageReference": {
                            "publisher": "[variables('imagePublisher')]",
                            "offer": "[variables('imageOffer')]",
                            "sku" : "[parameters('windowsOSVersion')]",
                            "version":"latest"
                        },
                        "osDisk" : {
                            "name": "osdisk",
                            "vhd": {
                                "uri": "[concat('http://',parameters('newStorageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
                            },
                            "caching": "ReadWrite",
                            "createOption": "FromImage"
                        }
                    },
                    "networkProfile": {
                        "networkInterfaces": [
                            {
                                "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
                            }
                        ]
                    }
                },
                "resources": [
                    {
                        "name": "CustomScript",
                        "type": "extensions",
                        "location": "[variables('location')]",
                        "apiVersion": "2015-05-01-preview",
                        "dependsOn": [
                            "[concat('Microsoft.Compute/virtualMachines/', variables('vmName')]"
                        ],                    
                        "properties": {
                            "publisher": "Microsoft.Compute",
                            "type": "CustomScriptExtension",
                            "typeHandlerVersion": "[variables('customScriptExtensionVersion')]",
                            "settings": {
                                "fileUris": [
                                    "https://<YOUR-BLOB-HERE>.blob.core.windows.net/resources/CUSTOM-POWERSHELL-SCRIPT.ps1",
                                    "http://go.microsoft.com/?linkid=9811175&clcid=0x409"
                                ],
                                "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\CUSTOM-POWERSHELL-SCRIPT.ps1 -Argument1 argument1')]"
                            }
                        }
                    }
                ]
            }
        ]
    }
    

    【讨论】:

    【解决方案3】:

    虽然不是一种直截了当的方法,但我实现了满足我需求的这个想法。

    1. 从 Portal 创建一个新的 azure VM 并通过 RDP https://azure.microsoft.com/en-in/documentation/articles/virtual-machines-windows-tutorial/ 连接它
    2. 现在在您的机器上下载 azure powershell msi(在 AzureVM 中下载被阻止)http://az635501.vo.msecnd.net/azcopy-3-1-0/MicrosoftAzureStorageTools.msi
    3. 手动将 msi 文件复制到虚拟机并安装到该虚拟机中

    4. 现在捕获该 VM 的映像并将其上传到 Azure 我的映像 https://azure.microsoft.com/en-in/documentation/articles/virtual-machines-capture-image-windows-server/

    5. 当我编写自动化脚本来创建 VM 时,我使用了这个新创建的 customVM 映像,其中已经安装了 AzurePowershell

    【讨论】:

      【解决方案4】:

      如果您的 VM 具有 PowerShell 5.0,那么您可以使用 PowerShell 库来安装您的模块。您不需要其他答案中提到的任何步骤。您需要做的就是像往常一样编写 PowerShell 脚本。只需使用一个 cmdlet 从 PowerShell 库添加模块。

      您可以使用 Install-Module 从库中安装模块,也可以使用 Install-Script 从 PowerShell 公共库安装示例脚本。

      您甚至可以将自己的模块放入库中并从那里安装。

      参考:Get Started with the PowerShell Gallery

      【讨论】:

        猜你喜欢
        • 2017-11-29
        • 2017-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-03
        • 2021-05-26
        • 2017-01-02
        • 1970-01-01
        相关资源
        最近更新 更多