Last active
September 7, 2017 21:13
-
-
Save MIchaelMainer/ccadb392223a4477aee91fe608819c60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# | |
# Credit to Sergey Babkins | |
# https://blogs.msdn.microsoft.com/sergey_babkins_blog/2016/12/31/how-to-pretty-print-xml-in-powershell-and-text-pipelines/ | |
function Format-Xml { | |
<# | |
.SYNOPSIS | |
Format the incoming object as the text of an XML document. | |
#> | |
param( | |
## Text of an XML document. | |
[Parameter(ValueFromPipeline = $true)] | |
[string[]]$Text | |
) | |
begin { | |
$data = New-Object System.Collections.ArrayList | |
} | |
process { | |
[void] $data.Add($Text -join "`n") | |
} | |
end { | |
$doc=New-Object System.Xml.XmlDataDocument | |
$doc.LoadXml($data -join "`n") | |
$sw=New-Object System.Io.Stringwriter | |
$writer=New-Object System.Xml.XmlTextWriter($sw) | |
$writer.Formatting = [System.Xml.Formatting]::Indented | |
$doc.WriteContentTo($writer) | |
$sw.ToString() | |
} | |
} | |
# Export-ModuleMember -Function Format-Xml | |
################################################################################ | |
# | |
# Download metadata files. | |
# | |
Write-Output "PowerShell function executed at:$(get-date)"; | |
$baseUrl = "https://graph.microsoft.com" | |
$target = "/`$metadata" | |
#$basePath = "C:\repos\Graph_SDK_Pipeline\submodules\Graph_Metadata\" # comment this out to use the present working directory. | |
$pathSegment = "v1.0", "beta" | |
# Use this if you want this to run in the PWD. | |
if ([string]::IsNullOrEmpty($basePath)) | |
{ | |
$basePath = $PWD | |
} | |
# Make sure that $basePath contains a trailing \ | |
if ($basePath -notmatch '.+?\\$') | |
{ | |
$basePath = $basePath + "\" | |
} | |
# Download metadata for each segment listed in $pathSegment. | |
for ($i=0; $i -lt $pathSegment.Length; $i++) { | |
$url = $baseUrl + "/" + $pathSegment[$i] + $target | |
$a = Get-Date | |
#format month and day for the file name. Single digit month and day values are turned into two digits: 0x | |
if ($a.Month -lt 10) { | |
$month = "0" + $a.Month | |
} else { | |
$month = $a.Month | |
} | |
if ($a.Day -lt 10) { | |
$day = "0" + $a.Day | |
} else { | |
$day = $a.Day | |
} | |
# Create file name based on target metadata and the current date. | |
$fileName = $pathSegment[$i] + "_" + $a.Year + "_" + $month + "_" + $day +".xml" | |
$dirPath = $basePath + $pathSegment[$i] | |
# Create the target path directory if it doesn't already exist. | |
If(!(Test-Path $dirPath)) | |
{ | |
New-Item -ItemType Directory -Force -Path $dirPath | |
} | |
$path = $dirPath + "\" + $fileName | |
# Check that we haven't already downloaded this file. | |
# If it doesn't exist, let's download and format it. | |
If(!(Test-Path $path)) | |
{ | |
# Download the metadata and save it. | |
"Downloading [$url]`nSaving at [$path]" | |
$client = new-object System.Net.WebClient | |
$client.DownloadFile($url, $path) | |
# Format the metadata. | |
Format-Xml (Get-Content $path) | Out-File $path | |
} | |
Else | |
{ | |
"We've already downloaded [$url] for today." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment