AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1047653
Accepted
A X
A X
Asked: 2020-12-27 21:33:36 +0800 CST2020-12-27 21:33:36 +0800 CST 2020-12-27 21:33:36 +0800 CST

使用 PowerShell 从 VM 内部获取当前 Azure VM 的详细信息

  • 772

假设我在 Azure 中预配了一台运行 Windows Server 2016 标准 Azure 映像的虚拟机。

无需在 VM 上安装任何东西,如何使用 PowerShell 从 VM 内部检索当前 VM 的详细信息?

我想检索的一些属性是:

  • 虚拟机名称
  • 虚拟机 IP 地址
  • 订阅号
  • 虚拟机位置

我相信这是可能的,因为 Azure 中的 Windows 桌面映像会自动显示上述大部分信息。那么如何通过 PowerShell 以编程方式获取这些信息呢?

(像往常一样,微软文档辜负了他们毫无价值的声誉)

任何帮助将不胜感激。

powershell virtual-machines azure windows-server-2016
  • 1 1 个回答
  • 410 Views

1 个回答

  • Voted
  1. Best Answer
    Taguada
    2020-12-28T05:01:34+08:002020-12-28T05:01:34+08:00

    我建议您使用Azure Instance Metadata Service

    Invoke-RestMethod -Headers @{"Metadata"="true"} -Method GET -NoProxy -Uri http://169.254.169.254/metadata/instance?api-version=2020-09-01 | ConvertTo-Json
    

    {
        "compute": {
            "azEnvironment": "AZUREPUBLICCLOUD",
            "isHostCompatibilityLayerVm": "true",
            "licenseType":  "Windows_Client",
            "location": "westus",
            "name": "examplevmname",
            "offer": "Windows",
            "osProfile": {
                "adminUsername": "admin",
                "computerName": "examplevmname",
                "disablePasswordAuthentication": "true"
            },
            "osType": "linux",
            "placementGroupId": "f67c14ab-e92c-408c-ae2d-da15866ec79a",
            "plan": {
                "name": "planName",
                "product": "planProduct",
                "publisher": "planPublisher"
            },
            "platformFaultDomain": "36",
            "platformUpdateDomain": "42",
            "publicKeys": [{
                    "keyData": "ssh-rsa 0",
                    "path": "/home/user/.ssh/authorized_keys0"
                },
                {
                    "keyData": "ssh-rsa 1",
                    "path": "/home/user/.ssh/authorized_keys1"
                }
            ],
            "publisher": "RDFE-Test-Microsoft-Windows-Server-Group",
            "resourceGroupName": "macikgo-test-may-23",
            "resourceId": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/virtualMachines/examplevmname",
            "securityProfile": {
                "secureBootEnabled": "true",
                "virtualTpmEnabled": "false"
            },
            "sku": "Windows-Server-2012-R2-Datacenter",
            "storageProfile": {
                "dataDisks": [{
                    "caching": "None",
                    "createOption": "Empty",
                    "diskSizeGB": "1024",
                    "image": {
                        "uri": ""
                    },
                    "lun": "0",
                    "managedDisk": {
                        "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampledatadiskname",
                        "storageAccountType": "Standard_LRS"
                    },
                    "name": "exampledatadiskname",
                    "vhd": {
                        "uri": ""
                    },
                    "writeAcceleratorEnabled": "false"
                }],
                "imageReference": {
                    "id": "",
                    "offer": "UbuntuServer",
                    "publisher": "Canonical",
                    "sku": "16.04.0-LTS",
                    "version": "latest"
                },
                "osDisk": {
                    "caching": "ReadWrite",
                    "createOption": "FromImage",
                    "diskSizeGB": "30",
                    "diffDiskSettings": {
                        "option": "Local"
                    },
                    "encryptionSettings": {
                        "enabled": "false"
                    },
                    "image": {
                        "uri": ""
                    },
                    "managedDisk": {
                        "id": "/subscriptions/xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx/resourceGroups/macikgo-test-may-23/providers/Microsoft.Compute/disks/exampleosdiskname",
                        "storageAccountType": "Standard_LRS"
                    },
                    "name": "exampleosdiskname",
                    "osType": "Linux",
                    "vhd": {
                        "uri": ""
                    },
                    "writeAcceleratorEnabled": "false"
                }
            },
            "subscriptionId": "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx",
            "tags": "baz:bash;foo:bar",
            "version": "15.05.22",
            "vmId": "02aab8a4-74ef-476e-8182-f6d2ba4166a6",
            "vmScaleSetName": "crpteste9vflji9",
            "vmSize": "Standard_A3",
            "zone": ""
        },
        "network": {
            "interface": [{
                "ipv4": {
                   "ipAddress": [{
                        "privateIpAddress": "10.144.133.132",
                        "publicIpAddress": ""
                    }],
                    "subnet": [{
                        "address": "10.144.133.128",
                        "prefix": "26"
                    }]
                },
                "ipv6": {
                    "ipAddress": [
                     ]
                },
                "macAddress": "0011AAFFBB22"
            }]
        }
    }
    

    或资源图

    Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Compute/virtualMachines' | limit 1" | ConvertTo-Json -Depth 100
    

    [
      {
        "id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/virtualMachines/ContosoVM1",
        "kind": "",
        "location": "westus2",
        "managedBy": "",
        "name": "ContosoVM1",
        "plan": {},
        "properties": {
          "hardwareProfile": {
            "vmSize": "Standard_B2s"
          },
          "networkProfile": {
            "networkInterfaces": [
              {
                "id": "/subscriptions/<subscriptionId>/MyResourceGroup/providers/Microsoft.Network/networkInterfaces/contosovm1535",
                "resourceGroup": "MyResourceGroup"
              }
            ]
          },
          "osProfile": {
            "adminUsername": "localAdmin",
            "computerName": "ContosoVM1",
            "secrets": [],
            "windowsConfiguration": {
              "enableAutomaticUpdates": true,
              "provisionVMAgent": true
            }
          },
          "provisioningState": "Succeeded",
          "storageProfile": {
            "dataDisks": [],
            "imageReference": {
              "offer": "WindowsServer",
              "publisher": "MicrosoftWindowsServer",
              "sku": "2016-Datacenter",
              "version": "latest"
            },
            "osDisk": {
              "caching": "ReadWrite",
              "createOption": "FromImage",
              "diskSizeGB": 127,
              "managedDisk": {
                "id": "/subscriptions/<subscriptionId>/resourceGroups/MyResourceGroup/providers/Microsoft.Compute/disks/ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166",
                "resourceGroup": "MyResourceGroup",
                "storageAccountType": "Premium_LRS"
              },
              "name": "ContosoVM1_OsDisk_1_9676b7e1b3c44e2cb672338ebe6f5166",
              "osType": "Windows"
            }
          },
          "vmId": "bbb9b451-6dc7-4117-bec5-c971eb1118c6"
        },
        "resourceGroup": "MyResourceGroup",
        "sku": {},
        "subscriptionId": "<subscriptionId>",
        "tags": {},
        "type": "microsoft.compute/virtualmachines"
      }
    ]
    

    使用 Run Command 在您的 Windows VM 中运行 PowerShell 脚本,此处有更多详细信息

    az vm run-command invoke  --command-id RunPowerShellScript --name win-vm -g my-resource-group \
        --scripts @script.ps1 --parameters "arg1=somefoo" "arg2=somebar"
    
    • 2

相关问题

  • 资源锁和电源外壳

  • 脚本 - 如何断开远程桌面会话?

  • 如何限制向通讯组发送到外部地址?

  • Powershell对值与数组的作用不同?

  • Windows Powershell Vim 键绑定

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve