Import-DSCModuleAsCmdlet.ps1
<#PSScriptInfo
.VERSION 1.0.0.0 .GUID 3cb0dc06-9a9f-471c-9c13-8278489e712b .AUTHOR Jeffrey Snover .COMPANYNAME Microsoft .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> [CmdletBinding()] [Alias()] [OutputType([int])] Param ( # Param1 help description [Parameter(Mandatory=$true, Position=0)] $Module, [Parameter(Position=1)] $Resource = "*" ) $m = Get-Module -Name $module -ListAvailable foreach ($resModule in (dir (Join-Path $M.ModuleBase "DSCResources"))) { Import-Module -Prefix $resModule.Name -Name $resModule.fullname -Force foreach ($Cmd in Get-Command -Module $resModule.Name -Name $Resource) { Set-Alias -Name $(($cmd.Name -replace "TargetResource","DSR" ) -replace "MSFT_") -Value $cmd.Name -Scope global } } <# .Synopsis Import the DSCResources in a module as a set of GET/SET/TEST cmdlets .DESCRIPTION All the DSC Resource providers implement the same signature so we import the module using the ResourceName as a Prefix. This produces a long name with "TargetResource" at the end of every cmdlet so we produce an alias which removes that and any "MSFT_" references and then add "DSR" for Desired State Resource so that we don't clobber any existing cmdlets (e.g. Service, Process, etc) NOTE: this only works for Script Resources .EXAMPLE JPS> .\Import-DSCModuleAsCmdlet.ps1 PSDesiredStateConfiguration Import-Module : The specified module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\en-US' was not loaded because no valid module file was found in any module directory. At D:\jsnover\OneDrive\Scripts\Import-DSCModuleAsCmdlet.ps1:31 char:5 + Import-Module -Prefix $resModule.Name -Name $resModule.fullname - ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (C:\WINDOWS\syst...Resources\en-US:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand Import-Module : The specified module 'C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\MSFT_LogResource' was not loaded because no valid module file was found in any module directory. At D:\jsnover\OneDrive\Scripts\Import-DSCModuleAsCmdlet.ps1:31 char:5 + Import-Module -Prefix $resModule.Name -Name $resModule.fullname - ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (C:\WINDOWS\syst...SFT_LogResource:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand JPS> Get-ServiceResourceDSR -Name Alg Name Value ---- ----- State Stopped Dependencies {} DisplayName Application Layer Gateway Service Name Alg Description Provides support for 3rd party protocol plug-ins for Internet Connection Sharing BuiltInAccount LocalService Path C:\WINDOWS\System32\alg.exe StartupType Manual .EXAMPLE JPS> Test-ServiceResourceDSR -Name Alg -StartupType Automatic -Verbose VERBOSE: Perform operation 'Query CimInstances' with following parameters, ''queryExpression' = SELECT * FROM Win32_Service WHERE Name='Alg','queryDialect' = WQL,'namespaceName' = root\cimv2'. VERBOSE: Operation 'Query CimInstances' complete. VERBOSE: Startup type for service 'ALG' is 'Manual'. It does not match 'Automatic'. False .EXAMPLE JPS> Set-ServiceResourceDSR -Name Alg -StartupType Automatic -Ensure Present -Verbose VERBOSE: Service 'Alg' already exists. Write properties such as Status, DisplayName, Description, Dependencies will be ignored for existing services. VERBOSE: Perform operation 'Query CimInstances' with following parameters, ''queryExpression' = SELECT * FROM Win32_Service WHERE Name='Alg','queryDialect' = WQL,'namespaceName' = root\cimv2'. VERBOSE: Operation 'Query CimInstances' complete. VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''instance' = Win32_Service: Application Layer Gateway Service (Name = "ALG"),'methodName' = Change,'namespaceName' = root/cimv2'. VERBOSE: Operation 'Invoke CimMethod' complete. VERBOSE: Service 'Alg' started. JPS> Get-ServiceResourceDSR -Name Alg Name Value ---- ----- State Running Dependencies {} DisplayName Application Layer Gateway Service Name Alg Description Provides support for 3rd party protocol plug-ins for Internet Connection Sharing BuiltInAccount LocalService Path C:\WINDOWS\System32\alg.exe StartupType Automatic #> |