Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Last active March 12, 2024 00:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinJump/a0bd8b69c41fdd762e68728f326d1569 to your computer and use it in GitHub Desktop.
Save KevinJump/a0bd8b69c41fdd762e68728f326d1569 to your computer and use it in GitHub Desktop.
Umbraco Setup and tear down scripts. (Gives you an approx 35 second Umbraco install)
# statup umbraco, and add some packages if you want
#
# e.g numbraco MyTestSite uSync Jumoo.TranslationManager vendr -NoStarter
#
# extras!!!
#
# open vscode in the folder as part of the build
# numbraco MyTestSite -code
#
# don't run the site
# numbraco MyTestSite -doNotRun
#
# Required dbatools powershell scripts and local SQLExpress install.
param(
[Parameter(Mandatory)]
[string]
$sitename,
[string[]]
$packages,
# dont install the starter kit.
[switch]
$NoStarter,
# open vscode in the folder
[switch]
$code,
# don't run the site at the end.
[switch]
$doNotRun
)
# check if you don't have DBATools it will still work
Function Test-CommandExists
{
Param ($command)
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = ‘stop’
try {if(Get-Command $command){RETURN $true}}
Catch {Write-Host “$command does not exist”; RETURN $false}
Finally {$ErrorActionPreference=$oldPreference}
} #end function test-CommandExists
# optional if you don't have dbatools, remove this, Umbraco will create the DB for you (will take 10-15 seconds longer)
if (Test-CommandExists new-dbaDatabase) {
Write-Host "Creating Database" -ForegroundColor Blue
new-dbaDatabase -SqlInstance $env:COMPUTERNAME\sqlExpress -name $sitename
}
else {
Write-Host "DBA Tools not installed, going for auto creation" -ForegroundColor Yellow
}
dotnet new umbraco -n $sitename --connection-string "Server=$env:COMPUTERNAME\SQLExpress;Database=$sitename;Integrated Security=true"
Set-Location $sitename
# you can put username and password here, but if you set UMB_USER, UMB_EMAIL and UMB_PASSWORD on your local pc, then you don't have to.
Set-Item Env:\UMBRACO__CMS__GLOBAL__INSTALLMISSINGDATABASE true
Set-Item Env:\UMBRACO__CMS__UNATTENDED__INSTALLUNATTENDED true
Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSERNAME $env:UMB_USER
Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSEREMAIL $env:UMB_EMAIL
Set-Item Env:\UMBRACO__CMS__UNATTENDED__UNATTENDEDUSERPASSWORD $env:UMB_PASSWORD
if (!$nostarter) {
Write-Host "Adding the starter kit" -ForegroundColor Blue
dotnet add package Umbraco.TheStarterKit -s https://api.nuget.org/v3/index.json
}
foreach($package in $packages) {
Write-Host "Adding $package" -ForegroundColor Blue
dotnet add package $package
}
if ($code) {
code $sitename
}
if ($doNotRun) {
dotnet build
}
else {
Write-Host "Running Site" -ForegroundColor Blue
Set-Location ..
$project = ".\$sitename\$sitename.csproj";
dotnet run --project $project
}
#
# Cleanup an umbraco install and its db
#
# unumbraco MyTestSite -removeFolder
#
# -removeFolder (option) when set will also remove the folder from disk
#
param(
[Parameter(Mandatory)]
[string]
$sitename,
[switch]
$removeFolder
)
Write-Host ">> Removing database $sitename"
remove-dbaDatabase -SqlInstance $env:COMPUTERNAME\sqlExpress -database $sitename -Confirm:$false
if ($removeFolder) {
# check to see if we are in the site folder,
$currentFolderName = Split-PAth -path (Get-Location) -leaf
if ($currentFolderName -eq $sitename) {
# if we are go up a folder.
Write-Host ">> Moving to parent folder of $sitename"
Set-Location ..
}
Write-Host ">> Removing folder $sitename"
if (Test-Path $sitename) {
Remove-Item -Path $sitename -Recurse
}
}
@KevinJump
Copy link
Author

KevinJump commented Oct 18, 2021

version of Sebastiaan's Unattended umbraco scripts https://gist.github.com/nul800sebastiaan/1553316fda85011270ce2bde35243e5b.

For local setups I have three environmental varitables on my machine UMB_USER, UMB_EMAIL and UMB_PASSWORD. these mean i can run the scripts above and the sites will get created with what ever those values are set to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment