Skip to content

Instantly share code, notes, and snippets.

@JamesDawson
Created October 2, 2013 00:18
Show Gist options
  • Select an option

  • Save JamesDawson/6787258 to your computer and use it in GitHub Desktop.

Select an option

Save JamesDawson/6787258 to your computer and use it in GitHub Desktop.
param
(
$resources = @(),
$pullServerRoot = "C:\ProgramData\PSDSCPullServer"
)
function New-Hash
{
param
(
$algorithm,
$path
)
# Compute the hash of the input file
$fileStream = [IO.File]::OpenRead((Resolve-Path $path))
$hasher = [Security.Cryptography.HashAlgorithm]::Create($algorithm)
$hash = $hasher.ComputeHash($fileStream)
$fileStream.Close()
$fileStream.Dispose()
return $hash
}
function New-ChecksumFile
{
param
(
$path
)
$hash = New-Hash -algorithm sha256 -path $path
# Get a texual representation of the hash (removing the '-' seperator that BitConverter outputs)
$hashStr = [system.bitconverter]::tostring($hash).Replace("-","")
# Use this method to create the .checksum file to avoid any trailing newline characters (as these break the validation)
[IO.File]::WriteAllText("$($path).checksum", $hashStr)
}
function New-ResourceZip
{
param
(
$modulePath,
$outputDir
)
# Read the module name & version
$module = Import-Module $modulePath -PassThru
$moduleName = $module.Name
$version = $module.Version.ToString()
Remove-Module $moduleName
$zipFilename = ("{0}_{1}.zip" -f $moduleName, $version)
$outputPath = Join-Path $outputDir $zipFilename
if (Test-Path $outputPath) { del $outputPath }
# Code to create an 'acceptable' structured ZIP file for DSC
# Courtesy of: @Neptune443 (http://blog.cosmoskey.com/powershell/desired-state-configuration-in-pull-mode-over-smb/)
[byte[]]$data = New-Object byte[] 22
$data[0] = 80
$data[1] = 75
$data[2] = 5
$data[3] = 6
[System.IO.File]::WriteAllBytes($outputPath, $data)
$acl = Get-Acl -Path $outputPath
$shellObj = New-Object -ComObject "Shell.Application"
$zipFileObj = $shellObj.NameSpace($outputPath)
if ($zipFileObj -ne $null)
{
$target = get-item $modulePath
# CopyHere might be async and we might need to wait for the Zip file to have been created full before we continue
# Added flags to minimise any UI & prompts etc.
$zipFileObj.CopyHere($target.FullName, 0x14)
[Runtime.InteropServices.Marshal]::ReleaseComObject($zipFileObj) | Out-Null
Set-Acl -Path $outputPath -AclObject $acl
}
else
{
Throw "Failed to create the zip file"
}
return $outputPath
}
# Create checksums for .mof configuration files in the PullServer Configuration directory
$mofFiles = gci -Path $pullServerRoot\Configuration -Filter *.mof
$mofFiles | % { New-ChecksumFile -path $_.FullName }
# Package and checksum each of the custom resources specified when calling this script
$resources | % {
$resourceZipPath = New-ResourceZip -modulePath (Resolve-Path $_) -outputDir $pullServerRoot\Modules
New-ChecksumFile -path $resourceZipPath
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment