The Deployment Bunny

OS Deployment, Virtualization, Microsoft based Infrastructure…

  • Archives

  • Meta

PowerShell is King – Building a Reference Image Factory (v 3.2)

Posted by Mikael Nystrom on February 23, 2017

The Image Factory has been re-worked and updated. It also has moved to GitHub https://github.com/DeploymentBunny/ImageFactoryV3ForHyper-V/

Concept and flow

The idea is the same, we use MDT, create refimages task sequences that runs without any questions. A PowerShell script will grab all enabled Task Sequences in a specified folder, create a VM for each of them. Grab the Bios Serial number from each om them, update customsettings ini with that information, start the VM’s (number of concurrent VM’s determine how many VM’s can run at the same time, wait until all VM’s are done and finally removes all VM’s for a cleanup process. You can run the script as is (if you store it in C:\Setup\ImageFactoryV3ForHyper-V on you MDT server), or you can open it in ISE and run section by section to see what happens.

The XML file (C:\Setup\ImageFactoryV3ForHyper-V\ImageFactoryV3.xml)

Is a control file for settings, pretty straightforward, just make sure that all values are correct.

image

The Script C:\Setup\ImageFactoryV3ForHyper-V\ImageFactoryV3-Build.ps1

The script contains some functions and logic, each section is described so you do know what it does. There are switches to the script,

  • -UpdateBootImage
  • -EnableMDTMonitoring
  • -TestMode

UppdateBoot image $True, updates the bootimage before copy, EnableMDTMonitoring require that MDT monitoring is enabled of the deployment share and testmode is not yet working (I’ll get back on that)

image

The PSINI Module

One big change is that instead of using a hack, using KVP i switches over to use a module called PSINI, it basically turns customsettings.ini into a hash table, making it easy to configure using PowerShell. That also means you need to download that module from TechNet Gallery http://gallery.technet.microsoft.com/scriptcenter/ea40c1ef-c856-434b-b8fb-ebd7a76e8d91and all credit for that module goes to http://oliver.lipkau.net/blog

Have Fun

/Mike

14 Responses to “PowerShell is King – Building a Reference Image Factory (v 3.2)”

  1. Ayo Dada said

    Reblogged this on Ayo Dada's Blog.

  2. Janick said

    Hi Mikael,

    I have an issue, when i try to load the Customsettings.ini file, do you know what could be wrong ? if i run “Get-IniContent E:\DeploymentLAB\Control\CustomSettings.ini” it’s loading the values just fine?

    Below is the error I’m seeing.

    http://pastebin.com/hSge7NgN

    • I “think” it is because the name of your TS contains a .
      “OSDComputerName=W10EN-US.O2016EN”, therefore it is not a string, I can test it later…unless you test it first

      • Janick said

        I just tried with a TS with TS ID: REFWS2016 with no “-” or “.” – same error

        Set-IniContent : Cannot process argument transformation on parameter ‘NameValuePairs’. Cannot convert the “OSDComputerName=REFWS2016” value of type “System.String” to type “System.Collections.Hashtable”.
        At C:\Scripts\MDT\ImageFactoryV3ForHyper-V\ImageFactoryV3-Build.ps1:365 char:100
        + … NameValuePairs “OSDComputerName=$Ref”
        + ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidData: (:) [Set-IniContent], ParameterBindingArgumentTransformationException
        + FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-IniContent

    • The PSINI module was updated, so we needed to update the script as well, it has been fixed now.

  3. Usse ISE (elevated), run section by section in the script and see what you get from each section.

    • Janick said

      Same error, it happens with this code:

      #Get BIOS Serialnumber from each VM and update the customsettings.ini file
      Update-Log -Data “Get BIOS Serialnumber from each VM and update the customsettings.ini file”
      $BIOSSerialNumbers = @{}
      Foreach($Ref in $RefTaskSequenceIDs){

      #Get BIOS Serailnumber from the VM
      $BIOSSerialNumber = Invoke-Command -ComputerName $($Settings.Settings.HyperV.Computername) -ScriptBlock {
      Param(
      $VMName
      )
      $VMObject = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter “ElementName = ‘$VMName'”
      $VMObject.GetRelated(‘Msvm_VirtualSystemSettingData’).BIOSSerialNumber
      } -ArgumentList $Ref

      #Store serialnumber for the cleanup process
      $BIOSSerialNumbers.Add(“$Ref”,”$BIOSSerialNumber”)

      #Update CustomSettings.ini

      $IniFile = “$($Settings.settings.MDT.DeploymentShare)\Control\CustomSettings.ini”
      $CustomSettings = Get-IniContent -FilePath $IniFile -CommentChar “;”

      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “OSDComputerName=$Ref”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “TaskSequenceID=$Ref”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “BackupFile=$Ref.wim”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “SkipTaskSequence=YES”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “SkipApplications=YES”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “SkipCapture=YES”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

      if($TestMode -eq $True){
      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “DoCapture=NO”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
      }
      else{
      $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “DoCapture=YES”
      Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate
      }
      }

  4. Janick said

    Everything up until this code, works fine:

    $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs “OSDComputerName=$Ref”
    Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

  5. Janick said

    Got it working :)

    Just change to this :

    $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs @{‘OSDComputerName’=$Ref ; ‘TaskSequenceID’=$Ref ;’BackupFile’= “$Ref.wim” ; ‘SkipTaskSequence’=’YES’ ;’SkipApplications’=’YES’ ; ‘SkipCapture’=’YES’ }
    Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputObject $CSIniUpdate

  6. […] Mikael Nystrom Image Factory (Hyper-V): https://deploymentbunny.com/2017/02/23/powershell-is-king-building-a-reference-image-factory-v-3-2/ […]

  7. Markus said

    Hi Mikael,

    cool stuff, but when setting SkipTaskSequence=Yes
    LiteTouch.wsf isn’t able to copy TS.xml cause oEnvironment.Item(“TaskSequenceID”) is empty.
    Where’s the trick ;-?

    Regards Markus

  8. kevin said

    I am having a similar issue to what Janick was having, however his solution is not working for me.

    I remember this was working a couple of weeks ago, and now it isn’t not sure why. Failing on this code here.

    Get-IniContent : A parameter cannot be found that matches parameter name ‘CommentChar’.
    At C:\Setup\ImageFactoryV3ForHyper-V\ImageFactoryV3-Build.ps1:362 char:57
    + … $CustomSettings = Get-IniContent -FilePath $IniFile -CommentChar “;”
    + ~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-IniContent], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Get-IniContent
    Set-IniContent : The term ‘Set-IniContent’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Setup\ImageFactoryV3ForHyper-V\ImageFactoryV3-Build.ps1:390 char:24
    + $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$ …
    + ~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Set-IniContent:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Out-IniFile : The term ‘Out-IniFile’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Setup\ImageFactoryV3ForHyper-V\ImageFactoryV3-Build.ps1:391 char:9
    + Out-IniFile -FilePath $IniFile -Force -Encoding ASCII -InputO …
    + ~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Out-IniFile:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: