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 switched 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

25 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

    • danflynnbu said

      In the customsettings.ini downloaded it via a Zip from Git it lost all the line returns is that the issue you are seeing?

    • 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

    • The issue is most likely that the PSINI.psm1 module is not loaded, you need to download that from TechNet gallery and make sure it is imported.

      • Kevin Blake Schumacher said

        You are absolutely right. Looks like at some point in time I must have made a bad move and deleted all of the supporting files and only had the PSINI.psm1 module. Resynced the git project and it is working now.

      • danflynnbu said

        Could you add a Verify for this and a setting in the XML for the location of the PSINI folder?

    • Robin Doublet said

      I am having the same issue, and that’s because the PSIni.psm1 module i have downloaded from TechNet gallery is maybe not up to date. In the Get-IniContent function, there is no “-CommentChar” parameter, so. this error is throw.

    • Robin Doublet said

      You can download the full module on the author website : http://lipkau.github.io/PsIni/

  9. Logan said

    Mikael,

    Thank you for such an awesome solution. I am running into one issue with the process.

    I was originally having issues with the out-inifile not being recognized when I realized that the link in your article takes you directly to “get-inicontent” instead of the entire PSINI suite (unless i’ve missed something). After downloading the entire PSINI module, I am encountering the following error with updating customsettings.ini:

    WARNING: Update-IniEntry:: Unable to split ‘System.Collections.Hashtable’ into a distinct key/value pair.

    Any advice is appreciated, please let me know if you need more information.

  10. Michael Biver - Microsoft said

    Just a quick heads up. The script is piping in the Task sequence id as the OSDComputername.. The character limit on the OSDComputername property is 15. The limit on the Task Sequence ID is 16. :-) So if I had task sequences with the 16 character limit, when applying the unattend.xml I was getting the following error.

    Windows could not parse or process unattend answer file [C:\windows\Panther\unattend.xml] for pass [specialize]. The answer file is invalid.

    this error is also in the setupact.log

    2017-04-21 14:53:10, Error [setup.exe] SMI data results dump: Source = Name: Microsoft-Windows-Shell-Setup, Language: neutral, ProcessorArchitecture: x86, PublicKeyToken: 31bf3856ad364e35, VersionScope: nonSxS, /settings/ComputerName
    2017-04-21 14:53:10, Error [setup.exe] SMI data results dump: Description = Value is invalid.

    Once I changed my Task Sequence ID’s to the 15 character limit, everything worked just fine. Thanks a lot Mikael for the awesome Image Factory!

    p.s. thanks to Mike Niehaus for helping me pinpoint the error above.

  11. Louis Dawson said

    Great work Mikael. I used the previous version with great success, I missed having the date in the capture vm so added the following code to your script:

    #Update CustomSettings.ini
    $FDATE = “””_#year(date)” + ” & ” + “””” + “-” +”””” + ” & ” + “day(date)” + ” & “+ “””” + “-” + “””” +” & “+ “month(date)” + “#”””
    $CSIniUpdate = Set-IniContent -FilePath $IniFile -Sections “$BIOSSerialNumber” -NameValuePairs @{“BackupFile”=”$Ref$FDATE.wim”}

  12. Bend said

    Hi Mikael,

    First of all, awesome script!

    I have a slight problem though, the scripts creates the VM and start the TS, but when running the scripts it immediately crashes because it can find the wim file.
    Look at the time frame, it’s like it does not get any input from the hyper-v server exactly like your screenshot in the main post – what could be wrong ?

    The log:

    IMF32, Imagefactory 3.2 (Hyper-V), 08/18/2017 08:40:09
    IMF32, Logfile is E:\Scripts\MDT\ImageFactoryV3ForHyper-V\log.txt, 08/18/2017 08:40:09
    IMF32, XMLfile is E:\Scripts\MDT\ImageFactoryV3ForHyper-V\ImageFactoryV3.xml, 08/18/2017 08:40:09
    IMF32, Importing modules, 08/18/2017 08:40:09
    IMF32, Reading from E:\Scripts\MDT\ImageFactoryV3ForHyper-V\ImageFactoryV3.xml, 08/18/2017 08:40:09
    IMF32, Verify Connection to DeploymentRoot, 08/18/2017 08:40:09
    IMF32, Connect to MDT, 08/18/2017 08:40:09
    IMF32, Connected to E:\DeploymentLab, 08/18/2017 08:40:09
    IMF32, Get MDT Settings, 08/18/2017 08:40:09
    IMF32, Check if we should update the boot image, 08/18/2017 08:40:09
    IMF32, Check if we should use MDTmonitoring, 08/18/2017 08:40:09
    IMF32, Verify access to boot image, 08/18/2017 08:40:09
    IMF32, Access to E:\DeploymentLab\boot\LiteTouchPE_x86.iso is ok, 08/18/2017 08:40:09
    IMF32, Get TaskSequences, 08/18/2017 08:40:09
    IMF32, Found 1 TaskSequences to work on, 08/18/2017 08:40:09
    IMF32, Get detailed info about the task sequences, 08/18/2017 08:40:09
    IMF32, WIN10PROX64 Win10ProX64 1.0, 08/18/2017 08:40:09
    IMF32, Verify Connection to Hyper-V host, 08/18/2017 08:40:09
    IMF32, Upload boot image to Hyper-V host, 08/18/2017 08:40:21
    IMF32, Remove old WIM files in the capture folder, 08/18/2017 08:40:23
    IMF32, Create the VM’s on Host, 08/18/2017 08:40:23
    IMF32, Get BIOS Serialnumber from each VM and update the customsettings.ini file, 08/18/2017 08:40:28
    IMF32, Start VM’s on Host, 08/18/2017 08:40:29
    IMF32, ConcurrentRunningVMs is set to: 1, 08/18/2017 08:40:29
    Currently running VM’s : WIN10PROX64 at 08/18/2017 08:40:33

    IMF32, Wait until they are done, 08/18/2017 08:41:03
    IMF32, Cleanup VMs, 08/18/2017 08:41:35
    IMF32, Update CustomSettings.ini, 08/18/2017 08:41:36
    IMF32, Cleanup MDT Monitoring data, 08/18/2017 08:41:36
    IMF32, Show the WIM’s, 08/18/2017 08:41:36
    IMF32, Could not find E:\DeploymentLab\Captures\WIN10PROX64.wim, something went wrong, sorry, 08/18/2017 08:41:36
    IMF32, The script took 0:Days 0:Hours 1:Minutes to complete., 08/18/2017 08:41:36

  13. […] you do not have an Image Factory, might I recommend borrowing Mikael Nystrom’s Reference Image Factory scripts. I use an Image Factory to generate fresh new Windows images on a monthly basis. My images […]

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: