Last active
January 27, 2025 04:08
This file contains 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
#prerequisite: .net SDK - https://dotnet.microsoft.com/en-us/download | |
#pass env id as param | |
#https://github.com/svdoever/svdoever.github.io/blob/2afce8fa91dba2a08d1940c23910abd73ac97f7e/src/pages/XM_Cloud_build_and_deploy_like_a_pro.md?plain=1#L2 | |
#https://thesitecorist.net/2022/12/19/sitecore-xm-cloud-logs/ | |
#https://www.sergevandenoever.nl/XM_Cloud_build_and_deploy_like_a_pro/ | |
[CmdletBinding()] | |
Param | |
( | |
[string] $orgClientId="sdssgs", | |
[string] $orgClientSecret="dfsdgfsfs-sdfsfs", | |
[string] $envId="sdfdsfsf", | |
[string] $cmUrl="https://sdfsf-sdfsf-devd289.sitecorecloud.io/", | |
[string] $outputPath=".", | |
[bool] $firstLoad=$false, | |
[bool] $downloadDeployLogs=$false, | |
[string] $downloadFileName="" | |
) | |
$watch = [System.Diagnostics.Stopwatch]::StartNew() | |
$watch.Start() # Timer start | |
$time = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
Write-Host("Start Date/Time - $time") | |
function ConnecttoXMCEnvironment() | |
{ | |
Register-PackageSource -Name Sitecore -Location https://nuget.sitecore.com/resources/v3/index.json -ProviderName NuGet #equivalent for VS nuget registration | |
dotnet new tool-manifest --force | |
dotnet tool install Sitecore.CLI --add-source https://sitecore.myget.org/F/sc-packages/api/v3/index.json | |
dotnet tool restore | |
Dotnet sitecore init | |
dotnet sitecore plugin add -n Sitecore.DevEx.Extensibility.Xmcloud | |
#add more plugins here | |
#dotnet sitecore cloud login #- this pops the login page that is intrusive | |
dotnet sitecore cloud login --client-credentials true --client-id $orgClientId --client-secret $orgClientSecret --allow-write | |
dotnet sitecore connect -r xmcloud --cm $cmUrl --allow-write true --environment-name default | |
} | |
function DownloadDeploymentLogs() | |
{ | |
$depPath=$outputPath + "\deploylogs" | |
if (!(test-path -path $depPath)) {new-item -path $depPath -itemtype directory} | |
if($depPath) { | |
write-host "Downloading XMC deploy log list to "$depPath | |
$depLogs=dotnet sitecore cloud deployment list --environment-id $envId --json | ConvertFrom-Json #Get deploy log list | |
foreach($depLog in $depLogs) | |
{ | |
#write-host $depObj.id | |
if($depLog.id) {dotnet sitecore cloud deployment log --deployment-id $depLog.id --path $depPath} | |
} | |
} | |
} | |
function DownloadAllEnviromentLogs() | |
{ | |
$envLogPath=$outputPath + "\envlogs" | |
if (!(test-path -path $envLogPath)) {new-item -path $envLogPath -itemtype directory} | |
write-host "Downloading XMC Env log list to "$envLogPath | |
$envLogs=dotnet sitecore cloud environment log list -id $envId #Get env log list | |
#write-host($envLogs) | |
foreach($envLog in $envLogs) | |
{ | |
if ($envLog.Trim() -Match "-") { | |
$logFileName=$envLog.Trim().Split('-',2).Trim() | |
write-host "Downloading log file named: "$logFileName | |
#dotnet sitecore cloud environment log download --environment-id "sfsdfdsf" --log Log.lj4nr.20241011.041315.txt --path .\envlogs | |
dotnet sitecore cloud environment log download --environment-id $envId --log $logFileName --path .\envlogs --timeout 600 | |
} | |
} | |
} | |
function DownloadLatestEnviromentLogs() | |
{ | |
# dotnet sitecore cloud environment log list --latest --environment-id asdada | |
$downloadPath="\envlogs\delta" | |
if (!(test-path -path $downloadPath)) {new-item -path $downloadPath -itemtype directory} | |
#CleanLogsFolder $downloadPath | |
$envLogPath=$outputPath + $downloadPath | |
write-host "Downloading XMC latest Env log list to "$envLogPath | |
$envLogs=dotnet sitecore cloud environment log list --latest --environment-id $envId #Get env log list | |
write-host "Latest logs:"$envLogs | |
foreach($envLog in $envLogs) | |
{ | |
if ($envLog.Trim() -Match "-") { | |
$logFileName=$envLog.Trim().Split('-',2).Trim() | |
write-host "Downloading log file named: "$logFileName | |
dotnet sitecore cloud environment log download --environment-id $envId --log $logFileName --path $envLogPath --timeout 600 | |
} | |
} | |
} | |
function CleanLogsFolder($folderName) | |
{ | |
#Get-ChildItem -Path (Join-Path $PSScriptRoot ".\deploylogs") | ForEach-Object { | |
# $dataPath = $_.FullName | |
# write-host($dataPath) | |
# Get-ChildItem -Path $dataPath -Exclude ".gitkeep" -Recurse | Remove-Item -Force -Recurse -Verbose | |
#} | |
Get-ChildItem -Path (Join-Path $PSScriptRoot $folderName) | ForEach-Object { | |
$deployPath = $_.FullName | |
write-host($deployPath) | |
Get-ChildItem -Path $deployPath -Exclude ".gitkeep" -Recurse | Remove-Item -Force -Recurse -Verbose | |
} | |
} | |
#CleanLogsFolder #Optional call | |
ConnecttoXMCEnvironment | |
if ($downloadFileName) | |
{ | |
dotnet sitecore cloud environment log download --environment-id $envId --log $downloadFileName --path .\envlogs --timeout 600 | |
} | |
else | |
{ | |
if ($downloadDeployLogs) {DownloadDeploymentLogs} | |
if ($firstLoad) { | |
DownloadAllEnviromentLogs #first time download | |
} | |
else { | |
DownloadLatestEnviromentLogs #incremental download | |
} | |
} | |
$watch.Stop() # Stopping the timer | |
Write-Host "Execution time - " $watch.Elapsed # Print script execution time | |
Write-Host("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment