Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: Adding ReverseDSC Support to the module #174

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion DSCResources/MSFT_xVHD/MSFT_xVHD.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function Get-TargetResource
[String]
$Generation = "Vhd"
)

# Check if Hyper-V module is present for Hyper-V cmdlets
if (!(Get-Module -ListAvailable -Name Hyper-V))
{
Expand Down Expand Up @@ -378,4 +377,53 @@ function GetNameWithExtension
$vhdName
}

function Export-TargetResource
{
[CmdletBinding()]
[OutputType([System.String])]
$InformationPreference = "Continue"

# Get all VMs on the host
$allVMs = Get-VM
$i = 1
$content = ""

# Loop through all VMs to get their VHDs;
foreach ($vm in $allVMs)
{
Write-Information " [$i/$($allVMs.Count)] Scanning VHDs for VM {$($vm.Name)}"
$vhds = Get-VHD -VMId $vm.ID
$j = 1
foreach ($vhd in $vhds)
{
Write-Information " [$j/$($vhds.Count)] $($vhd.Path)"

# Defining the Key parameters to allow Get-TargetResource to retrieve the instance;
$vhdPathParts = $vhd.Path.Split('\')
$vhdName = $vhdPathParts[$vhdPathParts.Length - 1].Split('.')[0]
$vhdPath = $vhd.Path.Replace($vhdPathParts[$vhdPathParts.Length - 1], "")

$keyParams = @{
Name = $vhdName
Generation = $vhd.VHDFormat
Path = $vhdPath
}

# Retrieving the current instance based on the key parameters;
$result = Get-TargetResource @keyParams

$content += " xVHD " + (New-GUID).ToString() + "`r`n"
$content += " {`r`n"

# This is where the magic happens. Converting the retrieved Hashtable into a DSC consumable string;
$currentDSCBlock = Get-DSCBlock -Params $result -ModulePath $PSScriptRoot
$content += $currentDSCBlock
$content += " }`r`n"
$j++
}
$i++
}
return $content
}

Export-ModuleMember -Function *-TargetResource
59 changes: 59 additions & 0 deletions Misc/ReverseDSCCollector.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
function Export-HyperVConfiguration
{
[CmdletBinding()]
[OutputType([System.String])]

$InformationPreference = "Continue"

$DSCContent = "Configuration HyperVConfiguration`r`n{`r`n"
$DSCContent += " Import-DSCResource -ModuleName xHyper-V`r`n`r`n"
$DSCContent += " Node localhost`r`n"
$DSCContent += " {`r`n"

#region xVHD
Write-Information "Extracting xVHD..."
$xVHDModulePath = Join-Path -Path $PSScriptRoot `
-ChildPath "..\DSCResources\MSFT_xVHD\MSFT_xVHD.psm1" `
-Resolve
Import-Module $xVHDModulePath | Out-Null
$DSCContent += Export-TargetResource
#endregion

$DSCContent += " }`r`n}`r`n"

#region Prompt the user for a location to save the extract and generate the files
if ($null -eq $Path -or "" -eq $Path)
{
$OutputDSCPath = Read-Host "Destination Path"
}
else
{
$OutputDSCPath = $Path
}

while ((Test-Path -Path $OutputDSCPath -PathType Container -ErrorAction SilentlyContinue) -eq $false)
{
try
{
Write-Information "Directory `"$OutputDSCPath`" doesn't exist; creating..."
New-Item -Path $OutputDSCPath -ItemType Directory | Out-Null
if ($?) {break}
}
catch
{
Write-Warning "$($_.Exception.Message)"
Write-Warning "Could not create folder $OutputDSCPath!"
}
$OutputDSCPath = Read-Host "Please Provide Output Folder for DSC Configuration (Will be Created as Necessary)"
}
<## Ensures the path we specify ends with a Slash, in order to make sure the resulting file path is properly structured. #>
if (!$OutputDSCPath.EndsWith("\") -and !$OutputDSCPath.EndsWith("/"))
{
$OutputDSCPath += "\"
}
$outputDSCFile = $OutputDSCPath + "HyperVConfiguration.ps1"
$DSCContent | Out-File $outputDSCFile

Invoke-Item -Path $OutputDSCPath
#endregion
}
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:

install:
- git clone https://github.com/PowerShell/DscResource.Tests
- ps: Install-Module ReverseDSC -RequiredVersion 1.9.4.5 -Force
- ps: Write-Verbose -Message "PowerShell version $($PSVersionTable.PSVersion)" -Verbose
- ps: Import-Module -Name "$env:APPVEYOR_BUILD_FOLDER\DscResource.Tests\AppVeyor.psm1"
- ps: Invoke-AppveyorInstallTask
Expand Down
8 changes: 7 additions & 1 deletion xHyper-V.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ Description = 'Module with DSC Resources for Hyper-V area'
# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '4.0'

# Adds dependency to ReverseDSC
RequiredModules = @(@{ModuleName = "ReverseDSC"; RequiredVersion = "1.9.4.5"; })

# Minimum version of the common language runtime (CLR) required by this module
CLRVersion = '4.0'

# Functions to export from this module
FunctionsToExport = '*'

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('Misc\ReverseDSCCollector.psm1')

# Cmdlets to export from this module
CmdletsToExport = '*'
CmdletsToExport = 'Export-HyperVConfiguration'

# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
PrivateData = @{
Expand Down