Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate target user #50

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions TargetUser.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

function Get-TargetUser(
[Parameter(Mandatory=$false)]
[string]$defaultTargetUserName)
{

if (!$defaultTargetUserName){ $defaultTargetUserName = $Env:UserName }

$newTargetUserName = $defaultTargetUserName
$newTargetUserSID = ""
$validUserSelected = $false

DO
{

$userInput = Read-Host "Please enter a username for the installation. Hit <Enter> to install for $defaultTargetUserName "

if (!$userInput) {
$newTargetUserName = $defaultTargetUserName
} else {
$newTargetUserName = $userInput
}

$newTargetUserObject = New-Object System.Security.Principal.NTAccount($newTargetUserName)
try {
$newTargetUserSID = $newTargetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value
$validUserSelected = $true
} catch {
Write-Warning "Specified user $userInput not found"
}

} until ($validUserSelected)

if ($newTargetUserName -eq $Env:UserName){
$newTargetUserProfileDirectory = $env:USERPROFILE
$newTargetUserLocalAppData = $env:LOCALAPPDATA
} else {
$newTargetUserProfileDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name USERPROFILE
$newTargetUserLocalAppData = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name LOCALAPPDATA
}

return [PSCustomObject]@{
userName = $newTargetUserName
SID = $newTargetUserSID
userProfile = $newTargetUserProfileDirectory
localAppData = $newTargetUserLocalAppData
}

}
90 changes: 47 additions & 43 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ param(
[switch] $PreRelease
)

function Generate-HelperScript(
Import-Module .\TargetUser.psm1

$targetUser = Get-TargetUser

function New-HelperScript(
# The cache folder
[Parameter(Mandatory=$true)]
[string]$cache)
Expand Down Expand Up @@ -223,9 +227,9 @@ function GetActiveProfiles(
[bool]$isPreview)
{
if ($isPreview) {
$file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json"
$file = "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json"
} else {
$file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
$file = "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
}
if (-not (Test-Path $file)) {
Write-Error "Couldn't find profiles. Please run Windows Terminal at least once after installing it. Exit."
Expand Down Expand Up @@ -266,16 +270,16 @@ function GetProfileIcon (
} elseif ($profile.icon -like "ms-appdata:///Roaming/*") {
#resolve roaming cache
if ($isPreview) {
$profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
$profilePng = $icon -replace "ms-appdata:///Roaming", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
} else {
$profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
$profilePng = $icon -replace "ms-appdata:///Roaming", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\"
}
} elseif ($profile.icon -like "ms-appdata:///Local/*") {
#resolve local cache
if ($isPreview) {
$profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\"
$profilePng = $icon -replace "ms-appdata:///Local", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\"
} else {
$profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\"
$profilePng = $icon -replace "ms-appdata:///Local", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\"
}
} elseif ($profile.icon -like "ms-appx:///*") {
# resolve app cache
Expand Down Expand Up @@ -366,15 +370,15 @@ function CreateProfileMenuItems(
$profileIcon = GetProfileIcon $profile $folder $localCache $icon $isPreview

if ($layout -eq "Default") {
$rootKey = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid"
$rootKeyElevated = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid"
$rootKey = "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid"
$rootKeyElevated = "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid"
CreateMenuItem $rootKey $name $profileIcon $command $false
CreateMenuItem $rootKeyElevated $name $profileIcon $elevated $true
} elseif ($layout -eq "Flat") {
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true
}
}

Expand All @@ -387,47 +391,47 @@ function CreateMenuItems(
[bool]$includePreview)
{
$folder = GetProgramFilesFolder $includePreview
$localCache = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\Cache"
$localCache = "$($targetUser.localAppData)\Microsoft\WindowsApps\Cache"

if (-not (Test-Path $localCache)) {
New-Item $localCache -ItemType Directory | Out-Null
}

Generate-HelperScript $localCache
New-HelperScript $localCache
$icon = GetWindowsTerminalIcon $folder $localCache

if ($layout -eq "Default") {
# defaut layout creates two menus
New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null
New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Force | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null
New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Force | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell' -Force | Out-Null
New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Force | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null
New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Force | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Force | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null
New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Force | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null
New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null

New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell' -Force | Out-Null
New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Force | Out-Null
} elseif ($layout -eq "Mini") {
$command = """$executable"" -d ""%V."""
$elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V."""
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false
CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false
CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true
return
}

Expand All @@ -445,18 +449,18 @@ if ((Get-Process -Id $pid).Path -like "*WindowsApps*") {
exit 1
}

if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and
-not (Test-Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal")) {
Write-Error "Please execute uninstall.old.ps1 to remove previous installation."
exit 1
}
#if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and
# -not (Test-Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal")) {
# Write-Error "Please execute uninstall.old.ps1 to remove previous installation."
# exit 1
#}

if ($PSVersionTable.PSVersion.Major -lt 6) {
Write-Error "Must be executed in PowerShell 6 and above. Learn how to install it from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7 . Exit."
exit 1
}

$executable = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\wt.exe"
$executable = "$($targetUser.localAppData)\Microsoft\WindowsApps\wt.exe"
if (-not (Test-Path $executable)) {
Write-Error "Windows Terminal not detected at $executable. Learn how to install it from https://github.com/microsoft/terminal (via Microsoft Store is recommended). Exit."
exit 1
Expand Down
Loading