private/MDT/ps/Import-MDTCmdlets.ps1
|
function Import-MDTCmdlets { <# .SYNOPSIS Loads the MDT PowerShell module required for MDT cmdlets. .DESCRIPTION Loads the MDT components in the required order: 1. Adds the Microsoft.BDD.PSSnapIn snap-in 2. Imports MicrosoftDeploymentToolkit.psd1 from the default MDT installation path Returns $true when both components load successfully, $false otherwise. This function is called automatically by the OSDeployMDT module during import. It can also be called directly to reload MDT components at any time. .EXAMPLE Import-MDTCmdlets Loads the MDT snap-in and module. Returns $true on success. .EXAMPLE if (-not (Import-MDTCmdlets)) { Write-Error 'MDT components could not be loaded.' } Use the boolean return value to gate MDT operations. .INPUTS None. This function does not accept pipeline input. .OUTPUTS System.Boolean .NOTES Author: David Segura Company: Recast Software Requires: Microsoft Deployment Toolkit installed at the default path Default MDT module: <MDTInstallDir>\bin\MicrosoftDeploymentToolkit.psd1 #> [CmdletBinding()] [OutputType([bool])] param () <# #region Load MDT snap-in try { if (-not (Get-PSSnapin -Name Microsoft.BDD.PSSnapIn -ErrorAction SilentlyContinue)) { Add-PSSnapin -Name Microsoft.BDD.PSSnapIn -ErrorAction Stop -Verbose } } catch { return $false } #endregion #> #region Load MDT PowerShell module $mdtInstallDir = Get-MDTInstallDir if ($null -eq $mdtInstallDir) { Write-Warning "[$(Get-Date -format s)] MDT installation directory not found." Write-Warning "[$(Get-Date -format s)] Ensure the Microsoft Deployment Toolkit is installed." return $false } $mdtModulePath = Join-Path -Path $mdtInstallDir -ChildPath 'bin\MicrosoftDeploymentToolkit.psd1' try { Import-Module -Name $mdtModulePath -Force -ErrorAction Stop } catch { Write-Warning "[$(Get-Date -format s)] Failed to load the MDT PowerShell module (MicrosoftDeploymentToolkit.psd1)." Write-Warning "[$(Get-Date -format s)] Ensure the Microsoft Deployment Toolkit is installed." Write-Warning "[$(Get-Date -format s)] $_" return $false } #endregion return $true } |