Cmdlets/Install-PSVSCode.ps1

# ? TITEL Install-PSVSCode
# ? DESCRIPTION
# ? TAGS UserCmdlet
# ? VERSION 2019.09.09

using Module Microsoft.PowerShell.Management
using Module Microsoft.PowerShell.Utility
using namespace System
using namespace System.Runtime.InteropServices
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
Set-StrictMode -Version Latest

<# TODO Umbauen auf:
    Install-PackageProvider -Name Chocolatey -Force -ForceBootstrap
    Install-Package -Name VisualStudioCode -Verbose -Force
#>


function Install-PSVSCode {

    [CmdletBinding(SupportsShouldProcess=$True)]
    param()

    if($PSCmdlet.ShouldProcess("Visual Studio Code", "Downloading")) {
        $uri = "https://vscode-update.azurewebsites.net/latest/win32-x64/stable"
        $vsCodeInstallExe = Join-Path -Path $env:USERPROFILE -ChildPath Downloads\VSCodeInstaller.exe
        Remove-Item -Path $vsCodeInstallExe -Force -ErrorAction Ignore
        $bitsDL = Start-BitsTransfer -Source $uri -Destination $vsCodeInstallExe -Asynchronous
        $vsCodePath = Join-Path -Path $env:ProgramFiles -ChildPath '\Microsoft VS Code\bin\code.cmd'
        while (($bitsDL.JobState -eq "Transferring") -or ($bitsDL.JobState -eq "Connecting")) {
            Write-Progress -Activity "Downloading: Visual Studio Code" `
                            -Status "Status $([math]::round($bitsDl.BytesTransferred / 1mb))mb / $([math]::round($bitsDl.BytesTotal / 1mb))mb" `
                            -PercentComplete ([int]($bitsDl.BytesTransferred / $bitsDl.BytesTotal * 100))
        }
        if($null -ne $bitsDl) {Complete-BitsTransfer -BitsJob $bitsDl}
        "Downloading: Visual Studio Code COMPLETED !" | Write-Verbose
    }

    if($PSCmdlet.ShouldProcess("Visual Studio Code", "Installing")) {
        Start-Process -FilePath $vsCodeInstallExe -ArgumentList /silent, /mergetasks=!runcode -Wait
        "Visual Studio Code installing COMPLETED !" | Write-Verbose
    }
    
    if($PSCmdlet.ShouldProcess("Visual Studio Code", "Extensions installing")) {
        "Visual Studio Code Extensions installing ..." | Write-Verbose
        $vsCodeExtensions = "aaron-bond.better-comments", `
                            "chrmarti.regex", `
                            "DotJoshJohnson.xml", `
                            "humao.rest-client", `
                            "ms-mssql.mssql", `
                            "ms-vscode.powershell", `
                            "vscode-icons-team.vscode-icons", `
                            "streetsidesoftware.code-spell-checker", `
                            "streetsidesoftware.code-spell-checker-german", `
                            "tomoki1207.pdf", `
                            "yzhang.markdown-all-in-one", `
                            "yzane.markdown-pdf"
        $vsCodeExtensions | ForEach-Object -Process {
            $pInfo = New-Object System.Diagnostics.ProcessStartInfo
            $pInfo.FileName = $vsCodePath
            $pInfo.RedirectStandardError = $true
            $pInfo.RedirectStandardOutput = $true
            $pInfo.UseShellExecute = $false
            $pInfo.Arguments = "--install-extension $_", "--force"
            
            $p = New-Object System.Diagnostics.Process
            $p.StartInfo = $pinfo
            $p.Start() | Out-Null
            $p.WaitForExit()
            $out =  $p.StandardOutput.ReadToEnd() -replace "[\n]", [String]::Empty
            $err =  $p.StandardError.ReadToEnd() -replace "[\n]", [String]::Empty
            $exitCode = $p.ExitCode
            " OUT: {0} | ERR: {1} | EXCODE: {2}" -f $out, $err, $exitCode | Write-Verbose
        }
        "... COMPLETED !" | Write-Verbose
    }

    if($PSCmdlet.ShouldProcess("Visual Studio Code", "Set Settings")) {
        $settingsFile = Join-Path -Path $env:APPDATA -ChildPath Code\User\settings.json
        @"
        {
            "files.autoSave": "afterDelay",
         
            "editor.minimap.enabled" : false,
            "editor.tabCompletion" : "on",
            "editor.mouseWheelZoom" : true,
         
            "explorer.confirmDragAndDrop": false,
            "explorer.confirmDelete" : false,
         
            "terminal.integrated.fontFamily": "Consolas",
            "terminal.integrated.fontSize" : 15,
            "terminal.integrated.lineHeight": 0,
         
             
            "workbench.colorTheme": "Default Light+",
            "workbench.iconTheme" : "material-icon-theme",
             
            "powershell.integratedConsole.focusConsoleOnExecute": false,
            "powershell.sideBar.CommandExplorerVisibility" : false,
            "powershell.helpCompletion" : "LineComment",
             
            "cSpell.language": "en,de",
            "cSpell.enabledLanguageIds": [
                "asciidoc",
                "c",
                "cpp",
                "csharp",
                "css",
                "go",
                "handlebars",
                "html",
                "jade",
                "javascript",
                "javascriptreact",
                "json",
                "latex",
                "less",
                "markdown",
                "php",
                "plaintext",
                "powershell",
                "pub",
                "python",
                "restructuredtext",
                "rust",
                "scss",
                "text",
                "typescript",
                "typescriptreact",
                "yml"
                ],
                "window.zoomLevel": 0,
        }
"@
 | Set-Content $settingsFile -Force
        "settings.json modifing COMPLETED !" | Write-Verbose
    }

    if($PSCmdlet.ShouldProcess("Visual Studio Code", "Starting")) {
        Start-Process -FilePath $vsCodePath
        "Start Visual Studio Code COMPLETED !" | Write-Verbose
        }
}