FI-B322 - Scripts used during MMS 2012 PowerShell session

Hello everyone,

For all of those attending MMS 2012, hope you are having a great week!

As promised here is the PowerShell session (FI-B322), script used during the session.

#set script variables
$vmNamePrefix = "COMBINEDDEMO"
#set random number
$global:Rand = (new-object random)
$random = $rand.Next(100,999)
#Get Template by name
($t = Get-SCVMtemplate | where {$_.Name -eq "W2K8R2SP1_Template"}) | fl Name, OperatingSystem, VirtualizationPlatform
#create a copy of the template that you will modify
($template = New-SCVMtemplate -VMTemplate $t -Name ("Temporary" + $vmNamePrefix + "Template")) | fl Name, OperatingSystem, VirtualizationPlatform
##########
#set the OU
(Set-SCVMTemplate -VMTemplate $template -DomainJoinOrganizationalUnit "OU=Test1,DC=contoso,DC=lab") | fl Name, DomainJoinOrganizationalUnit
##########
#get runasaccount which will serve as auto logon credential
($raa = Get-SCRunAsAccount -Name "MMS Build Account") | fl Name, Username, Domain
#set auto logon credential and autologoncount (must be set together)
(Set-SCVMTemplate -VMTemplate $template -AutoLogonCredential $raa -AutoLogonCount 10) | fl Name, AutoLogonCredential, AutoLogonCount
#######
#get unattend settings object
($unattend = $template.UnattendSettings) | Out-Null
#add key/value pairs to unattend settings
#Configuration Pass ID mapping
#  windowsPE = 0
#  offlineServicing = 1
#  generalize = 2
#  specialize = 3
#  auditsystem =4
#  audituser = 5
#  oobesystem = 6
#The relevant passes for VM deployment are oobesystem (6) and specialize (3)
$unattend.Add(“3/Microsoft-Windows-International-Core/InputLocale”, “0411:e0010411”)
$unattend.Add(“3/Microsoft-Windows-International-Core/SystemLocale”, “ja-jp”)
$unattend.Add(“3/Microsoft-Windows-International-Core/UILanguage”, “ja-jp”)
$unattend.Add(“3/Microsoft-Windows-International-Core/UILanguageFallback”, “ja-jp”)
$unattend.Add(“3/Microsoft-Windows-International-Core/UserLocale”, “ja-jp”)
$unattend.Add(“6/Microsoft-Windows-International-Core/InputLocale”, “0411:e0010411”)
$unattend.Add(“6/Microsoft-Windows-International-Core/SystemLocale”, “ja-jp”)
$unattend.Add(“6/Microsoft-Windows-International-Core/UILanguage”, “ja-jp”)
$unattend.Add(“6/Microsoft-Windows-International-Core/UILanguageFallback”, “ja-jp”)
$unattend.Add(“6/Microsoft-Windows-International-Core/UserLocale”, “ja-jp”)
$unattend.Add(“6/Microsoft-Windows-Shell-Setup/AutoLogon/Domain”, “contoso”)
$unattend.Add(“3/Microsoft-Windows-OutOfBoxExperience/DoNotOpenInitialConfigurationTasksAtLogon”, “true”)
$unattend.Add(“3/Microsoft-Windows-ServerManager-SvrMgrNc/DoNotOpenServerManagerAtLogon”, “true”)

$unattend | fl Key, Value
#commit unattend to template
(Set-SCVMtemplate -VMtemplate $template -UnattendSettings $unattend) | Out-Null
####### 
#Create new VM config from template
($vmconfig = New-SCVMConfiguration -VMTemplate $template -Name ($vmNamePrefix + "_Config")) | fl Name
#Get VMhost object
($vmhost = Get-SCVMHost -ComputerName "VMMLab1823N2.contoso.lab") | fl Name, VirtualizationPlatform
#Set destination to specific host
(Set-SCVMConfiguration -VMConfiguration $vmconfig -VMHost $vmhost -VMLocation ("C:\ClusterStorage\Volume1\VMs\" + $vmNamePrefix) -ComputerName ($vmNamePrefix + $random)) | fl Name, ComputerName, VMLocation, VMHost
#update VM config object with placement recommendations
Update-SCVMConfiguration -VMConfiguration $vmconfig | Out-Null
#create the differencing disk
$VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername $vmhost.Name
($VHDService.CreateDifferencingVirtualHardDisk(("C:\ClusterStorage\Volume1\VMs\" + $vmNamePrefix + "\" + $vmNamePrefix + ".VHD"),"C:\ClusterStorage\Volume1\BASEVHDs\W2K8R2SP1.VHD")) | fl ReturnValue
#Update VHD information
(Set-SCVirtualHardDiskConfiguration -VHDConfiguration $vmconfig.VirtualHardDiskConfigurations[0] -DestinationLocation ("C:\ClusterStorage\Volume1\VMs\" + $vmNamePrefix) -FileName ($vmNamePrefix + ".VHD") -DeploymentOption "UseExistingVirtualDisk") | fl DeploymentOption, FileName, DestinationLocation
#create VM
($vm = New-SCVirtualMachine -VMConfiguration $vmconfig -Name $vmNamePrefix -StartVM) | fl Name, VirtualizationPlatform, HostGroupPath

#####
#send power state info to VM
(Set-SCGuestInfo -VM $vm -Key "PowerState1" -Value "1. ON")
#####
#Suspend VM and write KVPs
Suspend-SCVirtualMachine -VM $vm | Out-Null
#send power state info to VM
(Set-SCGuestInfo -VM $vm -Key "PowerState2" -Value "2. Pause")
#####
#Save state VM
Resume-SCVirtualMachine -VM $vm | Out-Null
Stop-SCVirtualMachine -VM $vm -SaveState | Out-Null
#send power state info to VM
(Set-SCGuestInfo -VM $vm -Key "PowerState3" -Value "3. Save")
#####
#shutdown VM and write KVPs
Start-SCVirtualMachine -VM $vm | Out-Null
Stop-SCVirtualMachine -VM $vm -Shutdown | Out-Null
#send power state info to VM
(Set-SCGuestInfo -VM $vm -Key "PowerState4" -Value "4. OFF")
#####
#start VM and check
Start-SCVirtualMachine -VM $vm | Out-Null
#use KVP to pass in new IP information
#get basic information
(Read-SCGuestInfo -VM $vm -Key FullyQualifiedDomainName | select KVPMAP).KVPMAP
#get multiple values
$info = @{"FullyQualifiedDomainName"="";"NetworkAddressIPv4"="";"NetworkAddressIPv6"=""}
(Read-SCGuestInfo -VM $vm -KVPMap $info | Select KVPMAP).KVPMAP
#Pass new IP settings
#get mac address of NIC
Read-SCVirtualMachine -VM $vm | Out-Null
($nic1mac = $vm.VirtualNetworkAdapters[0].MacAddress)
#build kvp map with new IP information
$NICIP = @{("["+$nic1mac+"]"+"IPAddress")="10.1.1.34";("["+$nic1mac+"]"+"Mask")="255.255.255.0";("["+$nic1mac+"]"+"Gateway")="10.1.1.1"}
#send ip info to VM
(Set-SCGuestInfo -VM $vm -KVPMAP $NICIP | Select KVPMAP).KVPMAP
#cleanup
Stop-SCVirtualMachine -VM $vm -Force | Out-Null
Remove-SCVirtualMachine -VM $vm | Out-Null
Remove-SCVMConfiguration -VMConfiguration $vmconfig | Out-Null
Remove-SCVMTemplate -VMTemplate $template | Out-Null

 

 This is a registry partial I stored in he Library. This gets called by the guest OS as part of first logon (in script abov)

 Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Favorites]
"OU"="HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Group Policy\\State\\Machine"
"GUESTINFO"="HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Virtual Machine\\External"
"AUTOLOGON"="HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon"

 

This is the unattend.xml partial stored in Library and used in the template.

<?xml version="1.0" encoding="utf-8"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
    <settings pass="oobeSystem">
        <component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="https://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">
            <UserAccounts>
                <DomainAccounts>
                    <DomainAccountList wcm:action="add">
                      <Domain>dcmanager.lab</Domain>
                        <DomainAccount wcm:action="add">
                          <Group>Administrators</Group>
                          <Name>mmsbuildaccount</Name>
                        </DomainAccount>
                    </DomainAccountList>
                </DomainAccounts>
            </UserAccounts>
          <FirstLogonCommands>
     <SynchronousCommand wcm:action="add">
              <CommandLine>regedit.exe /s \\vmmteched\demo\MMSDEMOREGFAV.reg</CommandLine>
              <Description>Add favorites to registry</Description>
              <Order>1</Order>
            </SynchronousCommand>           
          </FirstLogonCommands>
     <SynchronousCommand wcm:action="add">
              <CommandLine>regedit.exe</CommandLine>
              <Description>Launch registry editor</Description>
              <Order>2</Order>
            </SynchronousCommand>           
          </FirstLogonCommands>
        </component>
    </settings>
</unattend>