Playing around with a powershell script for unattended install, but i still end up with install wizard

Hey there

Can someone see where I’m going wrong in this script?
Nothing seems to be failing, but it feels like I’m missing something.

// Allan

# Prompt for project name
$projectName = Read-Host "Enter project name"

# Prompt for Umbraco version (or use latest)
$versionInput = Read-Host "Enter Umbraco version (e.g. 16.2.0) or press Enter for latest"

if ([string]::IsNullOrWhiteSpace($versionInput)) {
    $versionInput = Invoke-RestMethod "https://api.nuget.org/v3-flatcontainer/umbraco.templates/index.json" |
        Select-Object -ExpandProperty versions |
        Select-Object -Last 1
    Write-Host "Using latest Umbraco.Templates version: $versionInput"
} else {
    Write-Host "Using specified version: $versionInput"
}

# Install Umbraco.Templates
dotnet new -i "Umbraco.Templates::$versionInput"

# Create solution folder
New-Item -ItemType Directory -Path $projectName -Force | Out-Null
Set-Location $projectName

# Create .sln file
dotnet new sln --name $projectName

# Create Umbraco project in www folder
dotnet new umbraco --name $projectName --output ./www

# Add project to solution
dotnet sln add "./www/$projectName.csproj" --in-root

# Create appsettings.Local.json with Unattended Install
@"
{
  ""ConnectionStrings"": {
    ""umbracoDbDSN"": ""Data Source=|DataDirectory|Umbraco.sqlite.db;Cache=Shared;Foreign Keys=True;Pooling=True"",
    ""umbracoDbDSN_ProviderName"": ""Microsoft.Data.Sqlite""
  },
  ""Umbraco"": {
    ""CMS"": {
      ""Global"": {
        ""InstallMissingDatabase"": true
      },
      ""Unattended"": {
        ""InstallUnattended"": true,
        ""UnattendedUserName"": ""johndoe"",
        ""UnattendedUserEmail"": ""[email protected]"",
        ""UnattendedUserPassword"": ""12345Password!""
      }
    }
  }
}
"@ | Set-Content -Path "./www/appsettings.Local.json"

Write-Host "appsettings.Local.json created with Unattended Install configuration."

# Prompt for package installation
$installExtras = Read-Host "Do you want to install PreviewBlock, uSync and Contentment? (yes/no)"

if ($installExtras -eq "yes") {
    Write-Host "Installing packages..."

    switch -Regex ($versionInput) {
        "^16\." {
            $contentmentVersion = "6.0.0-alpha009"
            $usyncVersion = "16.0.5"
            $blockPreviewVersion = "4.0.3"
        }
        "^13\." {
            $contentmentVersion = "5.2.0"
            $usyncVersion = "13.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        "^12\." {
            $contentmentVersion = "4.3.0"
            $usyncVersion = "12.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        "^11\." {
            $contentmentVersion = "4.2.0"
            $usyncVersion = "11.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        "^10\." {
            $contentmentVersion = "4.0.0"
            $usyncVersion = "10.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        default {
            Write-Host "Unknown Umbraco version. Installing latest stable versions."
            $contentmentVersion = "5.2.0"
            $usyncVersion = "13.0.0"
            $blockPreviewVersion = "1.13.7"
        }
    }

    dotnet add "./www/$projectName.csproj" package Umbraco.Community.Contentment --version $contentmentVersion
    dotnet add "./www/$projectName.csproj" package uSync --version $usyncVersion
    dotnet add "./www/$projectName.csproj" package Umbraco.Community.BlockPreview --version $blockPreviewVersion

    Write-Host "Packages installed:"
    Write-Host "   - Contentment $contentmentVersion"
    Write-Host "   - uSync $usyncVersion"
    Write-Host "   - BlockPreview $blockPreviewVersion"
} else {
    Write-Host "Package installation skipped."
}

# Build and run the project
Set-Location "./www"
dotnet build
dotnet run

Write-Host "Umbraco project '$projectName' has been created and is running with Unattended Install."
Write-Host "User: johndoe | Email: [email protected] | Password: 12345Password!"
Write-Host "Database: Umbraco.sqlite.db | Login ready without browser setup."

Nice script! Should it not output to appsettings.Development.json, not appsettings.Local.json?

For unattended install, to my knowledge you need:

  • A database connectionstring
  • InstallUnattended set to true
  • A Unattended username, password and email

All seem present, so that’s not the issue.

Will run with the ASPNETCORE_ENVIRONMENT as Production
I think (or maybe also influenced by the presence of a launchprofile).. so as per @Rockerby won’t pick up your appsettings.Local.json

dotnet run -e Local should explicitly set

ps donet run -e Local -c Release if you don’t want to run in debug

1 Like

I also have..

# Ensure we have the version specific Umbraco templates
$templateList = dotnet new uninstall | Out-String #-NoNewline
$packageVersion = $templateList | Select-String '(?s)Umbraco.Templates.*Version: (.*?)\r\n'| Select-Object -expand Matches | Select-Object -expand Groups | Select-Object -ExpandProperty Value -Skip 1 -First 1
if ($packageVersion -ne $versionInput)
{
    dotnet new install Umbraco.Templates::$versionInput --force 
}

So as not to reinstall if already installed.. might save a few seconds :wink:

and also

$currentPath = Split-Path -parent $MyInvocation.MyCommand.Definition
Write-output $currentPath

to use the ps file location rather than current path for the project root..

and for good measure..

dotnet nuget locals http-cache --clear

Also launching in vs rather than running in terminal…

Invoke-Item ".\$projectName.sln"

If you don’t know about https://psw.codeshare.co.uk/ yet, then I can tell you that this covers most, if not all of your PS script here.

It supports unattended installs, adding NuGet packages by default, creating docker files, etc.

1 Like

Thank you for your input — it helped me move forward and create a usable script.

My initial thoughts with the script were:

  • Install the desired Umbraco version

  • Create the solution and project structure

  • Add Unattended Install with user and database

  • Offer optional packages like Contentment, uSync, and BlockPreview

  • Provide the option to open Visual Studio (.sln file)

It’s working now, so thank you.

I’m thinking of experimenting with connecting the frontend part — maybe using npm, webpack, and Bootstrap 5.

Her is the result :slight_smile:

# Prompt for project name
$projectName = Read-Host "Enter project name"

# Prompt for Umbraco version (or use latest)
$versionInput = Read-Host "Enter Umbraco version (e.g. 16.2.0) or press Enter for latest"

if ([string]::IsNullOrWhiteSpace($versionInput)) {
    $versionInput = Invoke-RestMethod "https://api.nuget.org/v3-flatcontainer/umbraco.templates/index.json" |
        Select-Object -ExpandProperty versions |
        Select-Object -Last 1
    Write-Host "Using latest Umbraco.Templates version: $versionInput"
} else {
    Write-Host "Using specified version: $versionInput"
}

# Install Umbraco.Templates
dotnet new -i "Umbraco.Templates::$versionInput"

# Create solution folder
New-Item -ItemType Directory -Path $projectName -Force | Out-Null
Set-Location $projectName

# Create .sln file
dotnet new sln --name $projectName

# Create Umbraco project in www folder
dotnet new umbraco --name $projectName --output ./www

# Add project to solution
dotnet sln add "./www/$projectName.csproj" --in-root

# Create appsettings.Development.json for Unattended Install
$config = @{
  ConnectionStrings = @{
    umbracoDbDSN = "Data Source=|DataDirectory|/Umbraco.sqlite.db;Cache=Shared;Foreign Keys=True;Pooling=True"
    umbracoDbDSN_ProviderName = "Microsoft.Data.Sqlite"
  }
  Umbraco = @{
    CMS = @{
      Global = @{ InstallMissingDatabase = $true }
      Unattended = @{
        InstallUnattended = $true
        UnattendedUserName = "John Doe"
        UnattendedUserEmail = "[email protected]"
        UnattendedUserPassword = "!Password12345"
      }
    }
  }
}

$config | ConvertTo-Json -Depth 10 | Out-File -FilePath "./www/appsettings.Development.json" -Encoding utf8

Write-Host "appsettings.Development.json created with Unattended Install configuration."

# Prompt for package installation
$installExtras = Read-Host "Do you want to install PreviewBlock, uSync and Contentment? (yes/no)"

if ($installExtras -match '^(y|yes)$') {
    Write-Host "Installing packages..."

    switch -Regex ($versionInput) {
        "^16\." {
            $contentmentVersion = "6.0.0-alpha009"
            $usyncVersion = "16.0.5"
            $blockPreviewVersion = "4.0.3"
        }
        "^13\." {
            $contentmentVersion = "5.2.0"
            $usyncVersion = "13.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        "^12\." {
            $contentmentVersion = "4.3.0"
            $usyncVersion = "12.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        "^11\." {
            $contentmentVersion = "4.2.0"
            $usyncVersion = "11.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        "^10\." {
            $contentmentVersion = "4.0.0"
            $usyncVersion = "10.0.0"
            $blockPreviewVersion = "1.13.7"
        }
        default {
            Write-Host "Unknown Umbraco version. Installing latest stable versions."
            $contentmentVersion = "5.2.0"
            $usyncVersion = "13.0.0"
            $blockPreviewVersion = "1.13.7"
        }
    }

    dotnet add "./www/$projectName.csproj" package Umbraco.Community.Contentment --version $contentmentVersion
    dotnet add "./www/$projectName.csproj" package uSync --version $usyncVersion
    dotnet add "./www/$projectName.csproj" package Umbraco.Community.BlockPreview --version $blockPreviewVersion

    Write-Host "Packages installed:"
    Write-Host "   - Contentment $contentmentVersion"
    Write-Host "   - uSync $usyncVersion"
    Write-Host "   - BlockPreview $blockPreviewVersion"
} else {
    Write-Host "Package installation skipped."
}

# Build and run the project
Set-Location "./www"
dotnet build

# Start Umbraco in the background
Start-Process "dotnet" -ArgumentList "run"

Write-Host "Umbraco is starting in the background..."

# Wait for the database to be created
Write-Host "Waiting for Umbraco.sqlite.db to be created..."
$timeout = 60
$elapsed = 0
while (-not (Test-Path "umbraco/Data/Umbraco.sqlite.db") -and $elapsed -lt $timeout) {
    Start-Sleep -Seconds 1
    $elapsed++
}

if (Test-Path "umbraco/Data/Umbraco.sqlite.db") {
    Write-Host "Umbraco project '$projectName' is ready with Unattended Install."
    Write-Host "User: John Doe | Email: [email protected] | Password: !Password12345"
    Write-Host "Database: Umbraco.sqlite.db has been created."
    
    Write-Host ""
    Write-Host "Tip: If you want to continue working in Visual Studio, simply press Ctrl+C to stop the running server."
    Write-Host "Then open the solution file '$projectName.sln' in Visual Studio and start coding from there."
    Write-Host ""

    Get-Process dotnet | Stop-Process

    Write-Host ""
    $answer = Read-Host "Would you like to open the solution file '$projectName.sln' in Visual Studio now? (yes/no)"

    if ($answer -match '^(y|yes)$') {
       $slnPath = Join-Path (Get-Location) "../$projectName.sln"
    
       if (Test-Path $slnPath) {
           Write-Host "Launching Visual Studio with '../$projectName.sln'..."
           Start-Process $slnPath
       } else {
        Write-Host "Solution file '../$projectName.sln' not found in current directory."
       }
    } else {
       Write-Host "No problem. You can open '../$projectName.sln' manually whenever you're ready."
    }

} else {
    Write-Host "Timeout reached. The database was not created within $timeout seconds."
    Write-Host "Please check logs or run 'dotnet run' manually to debug."
}



1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.