使用PowerShell监视您的Azure计算机

介绍 (Introduction)

In earlier chapters, I show you how to create your Virtual Machine in Azure using Power Shell.

前面的章节中 ,我向您展示了如何使用Power Shell在Azure中创建虚拟机。

In this new chapter, I will show you how to get information about your VM machine already created. We will use PowerShell cmdlet commands to retrieve the information.

在这一新的章节中,我将向您展示如何获取有关已创建的VM计算机的信息。 我们将使用PowerShell cmdlet命令检索信息。

要求 (Requirements)

I am assuming that you already have a VM in Azure installed.

我假设您已经在Azure中安装了VM

  • An Azure subscription.

    Azure订阅。
  • A VM Machine in Azure with SQL Server already installed (check my article about Azure VMs).

    Azure中已安装SQL Server的VM机器(请参阅有关Azure VM的文章 )。
  • A local machine with Windows installed with PowerShell.

    Windows与PowerShell一起安装的本地计算机。

入门 (Getting started)

  1. In order to start, in the local machine open the Microsoft Azure PowerShell:

    Get-AzureVM

    使用PowerShell监视您的Azure计算机
    Figure 1. Starting the Microsoft Azure PowerShell

    为了启动,请在本地计算机中打开Microsoft Azure PowerShell:

    Get-AzureVM

    图1.启动Microsoft Azure PowerShell
  2. Let’s start with a simple command. The Get-AzureVM:

    使用PowerShell监视您的Azure计算机

    Figure 2. The Get-AzureVM cmdlet.

    让我们从一个简单的命令开始。 Get-AzureVM:

    图2. Get- AzureVM cmdlet
  3. By default, it shows the active VMs, the name and status of Azure VMs. You can verify in the Azure Portal the VM names:

    使用PowerShell监视您的Azure计算机

    Figure 3. The virtual machines in the Azure Portal

    默认情况下,它显示活动的VM,Azure VM的名称和状态。 您可以在Azure门户中验证VM名称:

    图3. Azure门户中的虚拟机
  4. Alternatively, you can run this command to get more information about the VM in Azure:

    Get-AzureVM | select hostname,instancesize,IpAddress,NetworkInterfaces,OperationStatus, PowerSate, ServiceName,Status

    使用PowerShell监视您的Azure计算机
    Figure 4. The Get-AzureVM cmdlet

    或者,您可以运行以下命令以获取有关Azure中VM的更多信息:

    GET-AzureVM | 选择主机名 ,实例大小,IpAddress,网络接口,OperationStatus PowerSate 服务名称 ,S 的ATU

    图4. Get- AzureVM cmdlet
  5. The command shows the InstanceSize, the IP Address, The OperationStatus. The PowerState, ServiceName, Status.

    The instance size is related to a specific price. You can find more information about prices and instance sized here.

    The PowerState and OperationsStatus are very important to check if the VM is OK.

    该命令显示InstanceSize,IP地址,OperationStatus。 PowerState,ServiceName,状态。

    实例大小与特定价格有关。 您可以在此处找到有关价格和实例大小的更多信息

    PowerState和OperationsStatus对检查VM是否正常非常重要。

  6. Another useful cmdlet is the Get-AzureDisk:

    使用PowerShell监视您的Azure计算机
    Figure 5. The Get-AzureDisk cmdlet

    另一个有用的cmdlet是Get-AzureDisk:

    图5. Get- AzureDisk cmdlet
  7. The Get-AzureDisks shows all the disks, the status of them, their location, DiskSize, name and other useful information about the disk:

    使用PowerShell监视您的Azure计算机

    Figure 6. The Azure Disk results.

    Get-AzureDisks显示所有磁盘,它们的状态,它们的位置,DiskSize,名称以及有关该磁盘的其他有用信息:

    图6. Azure磁盘结果。
  8. By default, it shows all the disks, but you can specify the information that you want to check and specify the name of the disk:

    Get-AzureDisk | select label,location,medialink,operationdescription,operationstatus,OS,sourceimagename,diskname| Where-Object {$_.disknameeq "olapsqldan-olapsqldan-8-201505292102250296"}

    使用PowerShell监视您的Azure计算机
    Figure 7. Custom query about the disk information

    The command shows the Disk information of the disk whose name is olapsqldan-olapsqldan-8-201505292102250296.

    默认情况下,它显示所有磁盘,但是您可以指定要检查的信息并指定磁盘名称:

    GET-AzureDisk | 选择标签 ,位置,媒体链接,操作说明,操作状态,操作系统,源映像名,磁盘名 | 哪里对象{$ _。 diskname - EQ “olapsqldan-olapsqldan-8-201505292102250296”}

    图7.关于 磁盘信息的 定制查询

    该命令显示名称为olapsqldan-olapsqldan-8-201505292102250296的磁盘的磁盘信息。

  9. You can verify in the Azure Portal the disk information and compare with the PowerShell results:

    使用PowerShell监视您的Azure计算机

    Figure 8. The Azure VM disks

    您可以在Azure门户中验证磁盘信息并与PowerShell结果进行比较:

    图8. Azure VM磁盘
  10. Another common command is the Get-AZureVMExtension. You can configure and extend the VM capabilities in Azure using the VM extensions.

    Get-AzureVMServiceName "olapsqldan" -Name "olapsqldan" | Get-AzureVMExtension

    使用PowerShell监视您的Azure计算机

    Figure 9. The Get-AzureVM cmdlet

    You can extend the security features, to support Windows System Center, to automate tasks, display and administer your VM, etc.

    另一个常见命令是Get-AZureVMExtension。 您可以使用VM扩展在Azure中配置和扩展VM功能。

    Get- AzureVM ServiceName olapsqldan ” -Name“ olapsqldan ” | GET-AzureVMExtension

    图9. Get- AzureVM cmdlet

    您可以扩展安全功能,以支持Windows System Center,自动执行任务,显示和管理VM等。

  11. You can also get the Roles of a specific Azure Service. The following example shows the roles of the Service olapsqldan:

    Get-AzureRoleServiceName "olapsqldan"

    您还可以获取特定Azure服务的角色。 以下示例显示了服务olapsqldan的角色:

    Get- AzureRole ServiceName olapsqldan

  12. The results shows the Role name, deployment ID, Operation status, etc.

    使用PowerShell监视您的Azure计算机
    Figure 10. The Get-AzureRole cmdlet

    结果显示角色名称,部署ID,操作状态等。

    图10. Get- AzureRole cmdlet
  13. The EndPoint information is also useful. You can verify the ports used and enabled, the Vip used and protocols. The following cmd shows the EndPoints of the Service olpasqldan:

    get-AzureVMServiceNameolapsqldan” | Get-AzureEndpoint | select Localport,name,port,protocol,Vip

    使用PowerShell监视您的Azure计算机

    Figure 11. The Get-AzureEndpoint cmdlet

    EndPoint信息也很有用。 您可以验证使用和启用的端口,使用的Vip和协议。 以下cmd显示了服务olpasqldan的端点:

    get- AzureVM ServiceName olapsqldan ” | GET-AzureEndpoint | 选择 本地端口,名称,端口,协议,Vip

    图11. Get- AzureEndpoint cmdlet
  14. You can verify and compare the information of the PowerShell with the Azure Portal:

    使用PowerShell监视您的Azure计算机
    Figure 12. The list of endpoints.

    您可以将PowerShell的信息与Azure门户进行比较并进行比较:

    图12.端点列表。
  15. Another important command is the Get-AzureLocation. This cmdlet shows all the locations available where you can store your VM:

    Get-AzureLocation | select DisplayName, virtualmachinerolesizes, storageaccounttypes

    使用PowerShell监视您的Azure计算机

    Figure 13. The Get-AzureLocation cmdlet

    The command shows the different locations available like US West, East, Japan East, West, the Machine Sizes (explained in step 5), etc.

    The next cmdlet is the Get-AzureOSDisk. The command lets you check the OS where the disk is, the disk name with the OS, the media link.

    Get-AzureVMServiceNameolapsqldan” | Get-AzureOSDisk

    Figure 14. The Get-AzureOSDisk command

    另一个重要命令是Get-AzureLocation。 此cmdlet显示可用于存储VM的所有可用位置:

    GET-AzureLocation | 选择 Di playName virtualmachinerolesize 存储帐户类型

    使用PowerShell监视您的Azure计算机

    图13. Get- AzureLocation cmdlet

    该命令显示了可用的不同位置,例如美国西部,东部,日本东部,西部,机器尺寸(在步骤5中说明)等。

    下一个cmdlet是Get-AzureOSDisk。 该命令使您可以检查磁盘所在的操作系统,带有操作系统的磁盘名称以及媒体链接。

    GET-AzureVM - 服务名称 “olapsqldan” | GET-AzureOSDisk

    图14. Get- AzureOSDisk 命令
  16. Another useful cmdlet is the Get-AzureOSVersion. It shows all the OSs available in Azure.

    Get-AzureOSVersion

    使用PowerShell监视您的Azure计算机
    Figure 15. The Get-AzureOSVersion cmdlet

    另一个有用的cmdlet是Get-AzureOSVersion。 它显示了Azure中可用的所有操作系统。

    GET-AzureOSVersion

    图15. Get- AzureOSVersion cmdlet
  17. Now, let’s create some queries to retrieve more specific information. For example, to get the list of OSs version 2008, you can use the following query:

    Get-AzureOSVersion | select FamilyLabel | Where-Object {$_.FamilyLabel -like "*2008*"}

    现在,让我们创建一些查询以检索更多特定信息。 例如,要获取2008版操作系统的列表,可以使用以下查询:

    GET-AzureOSVersion | 选择 FamilyLabel | 哪里对象{$ _。 类似于FamilyLabel 的“ * 2008 *”}

  18. You can export your results to a csv file to open the results in an Excel file. The following sample exports the results of a PowerShell query in a file name export.csv:

    get-AzureVMServiceNameolapsqldan” | Get-AzureEndpoint | select Localport,name,port,protocol,Vip| Export-Csv c:\scripts\export.csv

    您可以将结果导出到csv文件中,以在Excel文件中打开结果。 以下示例将PowerShell查询的结果导出到文件名export.csv中:

    get- AzureVM ServiceName olapsqldan ” | GET-AzureEndpoint | 选择 本地端口,名称,端口,协议,Vip | Export-Csv c:\ scripts \ export.csv

  19. If everything is OK, you will be able to open and check the Endpoints information in Excel:

    使用PowerShell监视您的Azure计算机

    Figure 16. The PowerShell results in MS Excel

    如果一切正常,您将可以在Excel中打开并检查“端点”信息:

    图16. PowerShell在MS Excel中的结果
  20. Let’s check if the port 1433 is used by the EndPoints. You can use the following PowerShell Command:

    get-AzureVMServiceNameolapsqldan” | Get-AzureEndpoint | select Localport,name,port,protocol | Where-Object {$_.LocalPorteq "1433"}

    让我们检查一下Endpoints是否使用了端口1433。 您可以使用以下PowerShell命令:

    get- AzureVM ServiceName olapsqldan ” | GET-AzureEndpoint | 选择 本地端口,名称,端口,协议 | 哪里对象{$ _。 LocalPort eq “ 1433”}

  21. You will have a result similar to this one:

    使用PowerShell监视您的Azure计算机

    Figure 17. Verifying port used in EndPoints.

    您将得到与此类似的结果:

    图17.验证 EndPoints中 使用的端口
  22. The following example will create an EndPoint named testendpoint in the Virtual Machine olapsqldan with the port 1112 and the protocol TCP-IP:

    Get-AzureVMServiceName "olapsqldan" | Add-AzureEndpoint -Name "testendpoint" -Protocol "tcp" –PublicPort 1112 –LocalPort 1112 | Update-AzureVM

    以下示例将在虚拟机olapsqldan中使用端口1112和协议TCP-IP创建一个名为testendpoint的端点:

    Get- AzureVM 服务名 olapsqldan ” | Add- AzureEndpoint-名称 testendpoint ” -Protocol“ tcp ” – PublicPort 1112 – LocalPort 1112 | 更新 -AzureVM

  23. The result displayed is the following:

    使用PowerShell监视您的Azure计算机

    Figure 18. The results of the EndPoint Creaton with PowerShell

    显示的结果如下:

    图18. EndPoint 的结果 使用PowerShell创建 Creaton
  24. If everything is OK, you will be able to see the EndPoint in the VM section in the EndPoint Tab:

    使用PowerShell监视您的Azure计算机

    Figure 19. The EndPoint created in the Azure Portal

    如果一切正常,您将可以在“端点”选项卡的“虚拟机”部分中看到端点:

    图19. 在Azure门户中创建 EndPoint
  25. Now, let’s say that I do not want the port 1112 to be enabled. Lets say that I want to erase the endpoint automatically if it uses this port. To do that, we need to store the EndPoint information in a variable named Object using the following commands:

    $Object=get-AzureVMServiceNameolapsqldan” | Get-AzureEndpoint | select Localport,name,port,protocol | Where-Object {$_.LocalPorteq "1112"}

    现在,假设我不希望启用端口1112。 假设我要使用此端口自动擦除端点。 为此,我们需要使用以下命令将EndPoint信息存储在名为Object的变量中:

    $ Object = get- AzureVM ServiceName olapsqldan ” | GET-AzureEndpoint | 选择 Localport ,名称,端口,协议 | 哪里对象{$ _。 LocalPort eq “ 1112”}

  26. The result displayed will be similar to this one:

    使用PowerShell监视您的Azure计算机

    Figure 20. The results of a query stored in a PowerShell variable

    显示的结果将与此类似:

    图20.存储在PowerShell变量中的查询结果
  27. To retrieve the value of a property. In this case the Port number, we can use the following command:

    $Object.port

    检索属性的值。 在这种情况下,端口号,我们可以使用以下命令:

    $ Object.port

  28. The result displayed will be the following:

    使用PowerShell监视您的Azure计算机
    Figure 21. The variable properties

    显示的结果如下:

    图21. 变量属性
  29. Now, if we want to automatically erase an EndPoint according if a specified port is used, we can use this script:

    $Object=get-AzureVMServiceNameolapsqldan” | Get-AzureEndpoint | select Localport,name,port,protocol | Where-Object {$_.LocalPorteq
    "1112"}

    If($Object.porteq "1112")
    {
    Get-AzureVMServiceName "olapsqldan" | Remove-AzureEndpoint –Name "testendpoint" | Update-AzureVM
    }

    现在,如果要根据使用的端口自动擦除EndPoint,可以使用以下脚本:

    $ Object = get- AzureVM ServiceName olapsqldan ” | GET-AzureEndpoint | 选择 Localport ,名称,端口,协议 | 哪里对象{$ _。 LocalPort eq
    “ 1112”}

    If( $ Object.port eq “ 1112”)
    {
    Get- AzureVM 服务名 olapsqldan ” | 删除 -AzureEndpoint –名称为“ testendpoint ” | 更新 -AzureVM
    }

  30. The scripts verify if the port 1112 is used. If that is the case, the EndPoint is removed. In order to run a script save the commands of the step 29 in a script named powershell.ps1

    脚本验证是否使用了端口1112。 在这种情况下,将删除EndPoint。 为了运行脚本,将步骤29的命令保存在名为powershell.ps1的脚本中
  31. Once saved, navigate to the path where the script is stored.

    Cd c:\scripts

    保存后,导航到存储脚本的路径。

    CD C:\脚本

  32. You can finally execute the file

    .\powershell.ps1

    您终于可以执行文件了

    。\ powershell.ps1

  33. You will receive a message similar to this one. The succeeded operation should be displayed.

    使用PowerShell监视您的Azure计算机

    Figure 22. The PowerShell script executed

    您将收到一条与此类似的消息。 应该显示成功的操作。

    图22.执行的PowerShell脚本
  34. You can verify the results using the Azure Portal and verify that the EndPoint created on step 24, was removed by the PowerShell Script.

    使用PowerShell监视您的Azure计算机

    您可以使用Azure门户验证结果,并验证PowerShell脚本已删除在步骤24中创建的EndPoint。

PowerShell是一个非常强大的工具,可以监视和验证来自一个或多个VM的信息。 在本章中,我们学习了如何检索与IP,VM,服务,角色,端点,端口等有关的此工具的信息。

(

PowerShell is a very powerful tool to monitor and verify information from your VM or VMs. In this chapter, we learned how to retrieve information about this tool related to IPs, VM, Services, Roles, Endpoints, Ports, etc.

)

翻译自: https://www.sqlshack.com/using-powershell-to-monitor-your-azure-machine/