Skip to content

Instantly share code, notes, and snippets.

@gwblok
Created January 15, 2020 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwblok/e1109583b64a1b0326386338d592e4d0 to your computer and use it in GitHub Desktop.
Save gwblok/e1109583b64a1b0326386338d592e4d0 to your computer and use it in GitHub Desktop.
The idea behind this is for pre-caching. You provide a Package ID, the script reaches out to DP and builds a list of all the files It then makes BITS Transfer calls to each file pulling it down to your destination.
<#GARYTOWN.COM - @GWBLOK - Assisted big time by Jeff Scripter.
The idea behind this is for pre-caching.
You provide a Package ID, the script reaches out to DP and builds a list of all the files
It then makes BITS Transfer calls to each file pulling it down to your destination.
Since it's using BITS, it is also then using BranchCache (Assuming you have BranchCache Enabled)
Once Completed, you can then delete the destination (since it's then in branchcache).
If you don't want to actually delete what you download, Comment out the Delete Line at the end.
#>
#Content you want to download
$ContentID = "PS2000EE"
#Current MP (Which also happens to be the DP in our environment). Chanage to your on method to determine your DP or set it static
$CurrentMPDP = Get-WmiObject -namespace 'root\ccm' 'sms_authority' | % { $_.CurrentManagementPoint }
#URL of the content on the DP
$URL = "http://$CurrentMPDP/SMS_DP_SMSPKG$/$ContentID"
#Used later to make the folder structure
$Cut = "http://$CurrentMPDP/SMS_DP_SMSPKG$"
$Cut = $Cut.ToUpper()
$destinationtemp = "$env:TEMP\$ContentID"
# Uses Invoke-Webrequest to read the Content location on the DP, then drill down making a list of all the files
Function Get-DPReference
{
Param(
[String]$Path
)
$Request = Invoke-WebRequest -Uri $Path -UseBasicParsing
$SubLinks = ($Request.Links).href
$Return = New-Object System.Collections.ArrayList
foreach ($SubLink in $SubLinks)
{
write-verbose "$Sublink"
# If the it's a file (Assumed by having a .XYZ in the name, unlike a folder
if ($SubLink -match ".*\.[a-z0-9]{2,4}$")
{
$null = $Return.Add($SubLink)
}
# if it's a folder, recurse down the folder looking for more links.
Else
{
$Recurse = Get-DPReference $SubLink
foreach ($file in $Recurse)
{
$null = $Return.Add($file)
}
}
}
$Return
}
#This is the list of all the URLs (Files) in the package.
$PackageFiles = Get-DPReference $url
#This triggers a BITS download of each file, which is then going into branchcache.
foreach ($File in $PackageFiles)
{
$file = $file.ToUpper()
$relativepath = $file.replace($Cut,"")
[IO.FileInfo]$destination = "$destinationtemp\$relativepath"
New-Item -Path $destination.DirectoryName -ErrorAction Ignore -ItemType Directory
Start-BitsTransfer -Source $File -Destination "$destinationtemp\$relativepath" -Credential $cred
}
if ($DestinationTemp){Remove-item -path $destinationtemp -recurse -force}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment