How can I use PowerShell or VBScript scripts in Hyper-V?

Source: Internet
Author: User
Tags powergui

To effectively manage the virtualization platform, you need powerful script tools. This allows several levels of configuration and shortens the configuration time. Microsoft hypervisor's script options include using VBScript and enhanced PowerShell In the Hyper-V script environment. In this article, TechTarget China's special virtualization expert Rick Vanover will introduce how Hyper-V script works, and give several examples.

Hyper-V's script environment is usually called Windows Management InstrumentationWMI). We will focus on Virtualized namespaces. On the WMI interface, the administrator can use Hyper-V to complete a large number of tasks. Use the PowerShell Management Library to make Management easier.

Determine which script to use depends on what is needed and what is available. I will pay attention to Hyper-V servers that cannot be centrally managed by System Center Virtual Machine ManagerSCVMM, because Microsoft provides additional script options for SCVMM implementation. Check the TechNet page to view the SCVMM script overview.

Microsoft is better at writing scripts.
 
My career is to repeat around the required scripts. Microsoft has better scripts than you or myself. Fortunately, many resources are available. The following is a list of online resources used to start using the Hyper-V script:

Network logs of Virtual PC Guy. Ben Armstrong's Microsoft Developer Network (MSDN) blog has simple script resources from all Microsoft virtualization systems.

Taylor Brown's blog. This is another powerful resource. Brown includes several use cases, including a case about SCVMM.

Blog address of Microsoft virtualization team. This blog includes everything related to Hyper-V and other virtualization resources.

PowerGUI.org Hyper-V PowerPack. This is equivalent to VMware's VI Toolkit. features from PowerGUI are required. Make sure to check the Toolkit tips written by Eric Seibert on TechTarget.

Cases of obtaining virtual machine information

Now that we have provided an overview of the script options, let's take a look at the instance. The PowerShell script used in Hyper-V starts with a query script for almost every resource, especially the script that includes decisive operations. This is only a single-row way of interacting with the virtual namespace of Hyper-V, and obtaining virtual machine information is like it exists in the namespace. Many scripts want to query this to obtain the current status, such as the value of OperationalStatus, which indicates the running status of the virtual machine. The following simple command can obtain information from a virtual machine named "TESTVM1:

Get-WmiObject-Namespace root \ virtualization-Query "Select * From Msvm_ComputerSystem Where ElementName = 'testvm1 '"

If you want to run a virtual machine in your environment, its name is the only one that needs to be changed. When running in PowerShell, the result is shown in 1:

 

Click to zoom in

Note that the OperationalStatus value of the virtual machine is displayed as "2", meaning it is running.

Use VBScript to stop or start a VM

Starting and stopping basic tasks such as virtual machines is a good start for learning scripts. Unlike PowerShell, Hyper-V machines can interact with each other through VBScript and fully access the WMI virtual namespace. The following script starts a virtual machine that is disabled by VBScript:

Option Explicit

Dim CallWMI

Dim InventoryVMs

Dim YourVM

YourVM = "TESTVM1"

Set CallWMI = GetObject ("winmgmts: \. \ root \ virtualization ")

Set InventoryVMs = CallWMI. ExecQuery ("SELECT * FROM Msvm_ComputerSystem

WHERE ElementName = '"& YourVM &"'")

InventoryVMs. ItemIndex (0). RequestStateChange (2)

For example, PowerShell, the only place in the script that needs to be changed is the online value of "YourVM", VBScript. Save the text as the. VBS file on the local file system and allow it to be executed locally.

Now we can run the close command on the remote Hyper-V server. The previous example was to run scripts locally on servers with Hyper-V and specific virtual machines. There are multiple Hyper-V servers and SCVMM is not executed, which is favorable for the environment. This script will be remotely disabled and forcibly disabled on Server55) TESTVM1 virtual machine.

Option Explicit

Dim CallWMI

Dim InventoryVMs

Dim YourVM

YourVM = "TESTVM1"

Set CallWMI = GetObject ("winmgmts: \ SERVER55 \ root \ virtualization ")

Set InventoryVMs = CallWMI. ExecQuery ("SELECT * FROM Msvm_ComputerSystem

WHERE ElementName = '"& YourVM &"'")

InventoryVMs. ItemIndex (0). RequestStateChange (3)

Note that the name of Server55 is entered in Row 6, which is the Hyper-V server of the host TESTVM1. It can run remotely from the system. Pay attention to the WMI namespace, such as another Windows Server 2008 Server. Force disable sending code 3 in the last line, as shown in the preceding example. Other popular virtual machine codes include the following:

Reboot (10): the code name is used to perform a hard start on the VM.
Pause (32768): This code pauses the virtual machine.

Test Hyper-V script

The Hyper-V script should be executed in the test environment. As the advertisement said, PowerShell is a very powerful shell environment. Scripts written into VBScript are at the same risk because they do what you tell, just as they will do what you tell. However, when a command is sent, the Undo button is not displayed or the command is returned. Simply put, this command assumes that you know what to do. Permissions are also a concern for remote Hyper-V servers. For more information, refer to the tip: Hyper-V permission mode.

Summary

To automate the Hyper-V Environment, administrators have a lot of options. With experience and patience, you can coordinate Hyper-V with your created PowerShell or VBScripts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.