GlennJC / AL-Scripts

Developed Scripts for deploying various Labs using Automated-Lab
10 stars 3 forks source link

MDT as an AutomatedLab Custom Role #11

Closed raandree closed 6 years ago

raandree commented 6 years ago

Hi Glenn,

can you have a look at the fork https://github.com/raandree/AutomatedLab please. I have extended the custom role feature in AL to support parameters and also do some local work required to download or initialize stuff. This feature is not yet documented but I am working on it.

I have converted the work you have done here: https://github.com/raandree/AutomatedLab/tree/develop/LabSources/CustomRoles/MDT.

This sample script shows how to deploy a MDT server with AL. What do you think about that? Does it make sense?

Thanks, Raimund

GlennJC commented 6 years ago

Hi @raandree apologies for taking time to get back to you.

I'm having some issues testing the code, obviously doing something silly. I have replaced the modules in my standard AutomatedLab modules directory with the updated ones here, however getting the following error running the sample code:

The property 'IsCustomRole' cannot be found on this object. Verify that the property exists and can be set. At C:\Program Files\WindowsPowerShell\Modules\AutomatedLabDefinition\AutomatedLabDefinition.psm1:3086 char:13

will I also need to recompile the AutomatedLab.dll to get the updated AutomatedLab.PostInstallationActivity object type ?

raandree commented 6 years ago

Thanks Glenn for getting back to me. We have not released the new version of AL which will be 5.0. However the commits to the development branch trigger the build process. You can go there by clicking on the green checkmark next to the commit.

Each build creates all the artifacts which includes the installer. This is the latest build.

We have added a lot of stuff to V5:

GlennJC commented 6 years ago

Great, thanks for that.

I ended up compiling the Visual Studio solution in any case since I was positive that was the issue and seems to be chugging along at the moment and is deploying great so far, but I will use that commits page as soon as I finish this build.

This is a really neat solution, was surprised how little code changes were required to integrate this into AL.

Haven't tested it yet, but I presume using this model you can have other Roles included in the machine definition as well as a Custom role (for example RootDC)?

GlennJC commented 6 years ago

hi @raandree. Have done an bunch of testing, and all looks pretty good. I'm currently making some changes to the MDT install ps1 file to fix some outstanding issues (like authorizing DHCP servers into AD) and streamline the code a bit.

I've also converted the SCCM scripts I did to use the same CustomRoles approach, seems to be working pretty good so far.

One thing that has come up, is the previous powershell script was able to select a specific OS (Edition + Version) as the Import-MDTOperatingSystem takes a AutomatedLab.OperatingSystem class as input. With the new approach, the standard names are provided as simple text strings, so am unable to downselect say a specific version of Windows 10, or indeed include multiple version of Windows 10 into MDT.

I have made some modifications to the code in an attempt to provide an array of AutomatedLab.OperatingSystem objects as inputs to the custom role code (example below), however AL is throwing an error when attempting to serialize this to the machines.xml file. I presume this is a limitation with XML serializing a complex array of objects?

Example creating an Array of Operating Systems (yes, I am co-opting the OperatingSystemImageDescription field in the object to provide a friendly name, still working on a neater way to accomplish this)

$OSArray = @()

$OS1 = Get-LabAvailableOperatingSystem | Where-Object {$_.OperatingSystemName -like 'Windows 10*' -and  $_.Version -eq '10.0.10586.0'} | Select-Object -First 1
$OS1.OperatingSystemImageDescription = 'Windows 10 1511'
$OSArray += $OS1
$OS1 = Get-LabAvailableOperatingSystem | Where-Object {$_.OperatingSystemName -like 'Windows 10*' -and  $_.Version -eq '10.0.15063.296'} | Select-Object -First 1
$OS1.OperatingSystemImageDescription = 'Windows 10 1703'
$OSArray += $OS1

Setting up the PostInstallationActivity

$mdtRole = Get-LabPostInstallationActivity -CustomRole MDT -Properties @{
    DeploymentFolderLocation = 'C:\DeploymentShare'
    InstallUserID = 'MdtService'
    InstallPassword = 'Somepass1'
    OperatingSystemsByObject = $OSArray
    AdkDownloadPath = "$labSources\SoftwarePackages\ADK"
}

Modified HostInit.ps1 file:

param(
    [string]$DeploymentFolderLocation = 'C:\DeploymentShare',

    [string]$InstallUserID = 'MdtService',

    [string]$InstallPassword = 'Somepass1',

    [Parameter(Mandatory)]
    [string]$ComputerName,

    [string[]]$OperatingSystemsByName,

    [AutomatedLab.OperatingSystem[]]$OperatingSystemsByObject,

    [string]$AdkDownloadPath = "$labSources\SoftwarePackages\ADK"
)

$script = Get-Command -Name $PSScriptRoot\DownloadAdk.ps1
$param = Sync-Parameter -Command $script -Parameters $PSBoundParameters
& $PSScriptRoot\DownloadAdk.ps1 @param

$script = Get-Command -Name $PSScriptRoot\InstallMDT.ps1
$param = Sync-Parameter -Command $script -Parameters $PSBoundParameters
& $PSScriptRoot\InstallMDT.ps1 @param

Error throw by the Install-Lab command:

Exception calling "Export" with "1" argument(s): "There was an error generating the XML document."
At C:\Program Files\WindowsPowerShell\Modules\AutomatedLabDefinition\AutomatedLabDefinition.psm1:1268 char:5
+     $script:machines.Export($machineFilePath)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

I can see in the Machines.XML file that it has stopped processing at the OperatingSystemsByObject section:

<snip>
        <item>
          <key>
            <string>AdkDownloadPath</string>
          </key>
          <value>
            <ArrayOfAnyType>
              <anyType xsi:type="xsd:string">C:\LabSources\SoftwarePackages\ADK</anyType>
            </ArrayOfAnyType>
          </value>
        </item>
        <item>
          <key>
            <string>OperatingSystemsByObject</string>

Any thoughts how I may be able to provide an array or similar via this method, or will I need to do some trickery with constructing text strings or simpler object types to relay the same information?

Thanks

raandree commented 6 years ago

Hi Glenn, sorry for responding quite late. Serializing objects can be quite tricky and somehow the parameters have to be stored in the XML files. If selecting the version is quite an important thing to do, we can look for other ways to realize this.

If you have already solved that issue, we are even happier to take a pull request. If not I can look into this.