Have you ever tried to automate the setup of a complex environment including the base OS, AD, SQL, Hyper-V and other components?

For my demo at Ignite 2016 I did just that. I would like to share a few things I learnedwhile writing a single PowerShell script that builds the demo environment from scratch. The script heavily usesPowerShell Direct and just requires the installation sources put into specific folders.

In this blog post I’d like to provide solutions for two challenges that I came across:

Determining when a virtual machine is ready for customization using PowerShell Direct, and as a variation of that theme Determining when Active Directory is fully up and running in a fully virtualized PoC/demo environment.

Solution #1 Determining when a virtual machine is ready for customization using PowerShell Direct

Some guest OSoperations require multiple restarts. If you’re using a simple approach to automate everything from a single script and check for the guest OS to be ready, things might go wrong. For example, with a nave PowerShell Direct call using Invoke-Command, the script might resume while the virtual machine is restarting multiple times to finish up role installation. This can lead to unpredictable behavior and break scripts.

One solution is using a wrapper function like this:

This wrapper function first makes sure that the virtual machine is running, if not, the VM is started. If the heartbeat integration component is enabled for the VM, it will also wait for a proper heartbeat status this resolves the multiple-reboot issue mentioned above. Afterwards, it waits for a proper PowerShell Direct connection. Both wait operations have time-outs to make sure script execution is not blocked perpetually. Finally, the provided script block is run passing through arguments.

Solution #2 Determining when Active Directory is fully up and running

Whenever a Domain Controller is restarted, it takes some time until the full AD functionality is available. If you use a VMConnect session to look at the machine during this time, you will see the status message “Applying Computer Settings”. Even with the Invoke-CommandWithPSDirect wrapper function above, I noticed some calls, like creating a new user or group, will fail during this time.

In my script, I am therefore waiting for AD to be ready before continuing:

This function leverages the Invoke-CommandWithPSDirect function to ensure the VM is up and running. To make sure that Active Directory works properly, it then requests the local computer’s AD object until this call succeeds.

Using these two functions has saved me quite some headache. For additional tips, you can also take a look at Ben’s tips around variables and functions .

Cheers,

Lars

PS: The full script for building the guarded fabric demo environment for Ignite 2016’s session BRK3124: Dive into Shielded VMs with windows Server 2016 Hyper-V will be shared through our Virtualization Documentation GitHub .

本文系统(windows)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

分页:12
转载请注明
本文标题:Waiting for VMs to restart in a complex configuration script with PowerShell Dir ...
本站链接:http://www.codesec.net/view/481662.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(52)