Skip to content
Merged
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
54 changes: 28 additions & 26 deletions assets/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@
<?define InfoURL="https://github.com/PowerShell/PowerShell" ?>
<?define ProductName = "$(env.ProductName)" ?>
<?define ProductCode = "$(env.ProductCode)" ?>
<?define ProductSimpleVersionWithNameAndOptionalArchitecture = "$(var.ProductName) $(env.SimpleProductVersion) ($(sys.BUILDARCH))"?>
<!-- UpgradeCode GUID MUST REMAIN SAME THROUGHOUT ALL VERSIONS, otherwise, updates won't occur. -->
<?if $(sys.BUILDARCH)=x64?>
<?define UpgradeCode = "$(env.UpgradeCodeX64)" ?>
<?define ProductSemanticVersionWithNameAndOptionalArchitecture = "$(var.ProductName) $(env.ProductSemanticVersion)"?>
<?define ExplorerContextMenuDialogText = "&$(var.ProductName) $(env.ProductSemanticVersion)"?>
<?define ExplorerContextMenuDialogText = "&$(var.ProductName) $(env.SimpleProductVersion)"?>
<?else?>
<?define UpgradeCode = "$(env.UpgradeCodeX86)" ?>
<?define ProductSemanticVersionWithNameAndOptionalArchitecture = "$(var.ProductName) $(env.ProductSemanticVersion) ($(sys.BUILDARCH))"?>
<?define ExplorerContextMenuDialogText = "&$(var.ProductName) $(env.ProductSemanticVersion) ($(sys.BUILDARCH))"?>
<?define ExplorerContextMenuDialogText = "&$(var.ProductName) $(env.SimpleProductVersion) ($(sys.BUILDARCH))"?>
<?endif?>
<?define ProductDirectoryName = "$(env.ProductDirectoryName)" ?>
<?define ProductVersion = "$(env.ProductVersion)" ?>
<?define SimpleProductVersion = "$(env.SimpleProductVersion)" ?>
<?define ProductSemanticVersion = "$(env.ProductSemanticVersion)" ?>
<?define ProductVersionWithName = "$(var.ProductName)_$(var.ProductVersion)"?>
<?define ProductSemanticVersionWithName = "$(var.ProductName)-$(env.ProductSemanticVersion)"?>
<?define ProductProgFilesDir = "$(env.ProductProgFilesDir)" ?>
<?define AddPathDefault = "$(env.AddPathDefault)"?>
<!-- Explorer context submenu entries. The ampersand denotes the keyboard shortcut. -->
<?define ExplorerContextSubMenuDialogText = "Open &here"?>
<?define ExplorerContextSubMenuElevatedDialogText = "Open here as &Administrator"?>
<!-- The ProductCode is Product Id and needs to be unique for every PowerShell version to allow SxS install: http://wixtoolset.org/documentation/manual/v3/xsd/wix/product.html -->
<Product
Id="$(var.ProductCode)"
Name="PowerShell 6-$(sys.BUILDARCH)"
Name="PowerShell $(var.SimpleProductVersion)-$(sys.BUILDARCH)"
Language="1033"
Version="$(var.ProductVersion)"
Manufacturer="Microsoft Corporation"
Expand Down Expand Up @@ -148,7 +147,7 @@
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="$(var.ProductProgFilesDir)">
<Directory Id="INSTALLFOLDER" Name="PowerShell">
<Directory Id="$(var.ProductDirectoryName)" Name="$(var.ProductSemanticVersion)">
<Directory Id="$(var.ProductDirectoryName)" Name="$(var.SimpleProductVersion)">
<Component Id="ProductVersionFolder" Guid="{e1a7f05e-0cd6-4227-80a8-e4fb311f045c}">
<CreateFolder/>
</Component>
Expand All @@ -165,50 +164,51 @@
</Component>
<!-- add ourselves to %PATH% so pwsh.exe can be started from Windows PowerShell or cmd.exe -->
<Component Id="SetPath" Guid="{9dbb7763-7baf-48e7-b025-3bdedcb0632f}" KeyPath="yes">
<Condition>ADD_PATH=1</Condition>
<Environment Id="PATH" Action="set" Name="PATH" Part="last" Permanent="no" System="yes" Value="[$(var.ProductDirectoryName)]"/>
</Component>
<!-- Explorer context menu with 2 submenus to open PowerShell normally or as an Administator.
See https://blogs.msdn.microsoft.com/andrew_richards/2017/03/01/enhancing-the-open-command-prompt-here-shift-right-click-context-menu-experience/ for details -->
<Component Id="ExplorerContextMenu" Guid="{df82e941-fced-4de9-aef8-c81a2946dd68}" KeyPath="yes">
<Condition>ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL</Condition>
<!-- When clicking on background in Explorer -->
<RegistryKey Root="HKCR" Key="Directory\Background\shell\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)">
<RegistryKey Root="HKCR" Key="Directory\Background\shell\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)">
<RegistryValue Type="string" Name="MUIVerb" Value="$(var.ExplorerContextMenuDialogText)"/>
<RegistryValue Type="string" Name="Icon" Value="[$(var.ProductDirectoryName)]assets\Powershell_black.ico"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)"/>
</RegistryKey>
<!-- When clicking on Drive in Explorer -->
<RegistryKey Root="HKCR" Key="Drive\shell\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)">
<RegistryKey Root="HKCR" Key="Drive\shell\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)">
<RegistryValue Type="string" Name="MUIVerb" Value="$(var.ExplorerContextMenuDialogText)"/>
<RegistryValue Type="string" Name="Icon" Value="[$(var.ProductDirectoryName)]assets\Powershell_black.ico"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)"/>
</RegistryKey>
<!-- When clicking on Desktop background in Explorer -->
<RegistryKey Root="HKCR" Key="DesktopBackground\shell\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)">
<RegistryKey Root="HKCR" Key="DesktopBackground\shell\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)">
<RegistryValue Type="string" Name="MUIVerb" Value="$(var.ExplorerContextMenuDialogText)"/>
<RegistryValue Type="string" Name="Icon" Value="[$(var.ProductDirectoryName)]assets\Powershell_black.ico"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)"/>
</RegistryKey>
<!-- When clicking on folder in Explorer -->
<RegistryKey Root="HKCR" Key="Directory\shell\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)">
<RegistryKey Root="HKCR" Key="Directory\shell\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)">
<RegistryValue Type="string" Name="MUIVerb" Value="$(var.ExplorerContextMenuDialogText)"/>
<RegistryValue Type="string" Name="Icon" Value="[$(var.ProductDirectoryName)]assets\Powershell_black.ico"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)"/>
<RegistryValue Type="string" Name="ExtendedSubCommandsKey" Value="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)"/>
</RegistryKey>
<!-- Sub menus to open PowerShell at the current location either as a normal shell or as Administrator -->
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)\shell\open">
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\open">
<RegistryValue Type="string" Name="MUIVerb" Value="$(var.ExplorerContextSubMenuDialogText)"/>
<RegistryValue Type="string" Name="Icon" Value="[$(var.ProductDirectoryName)]assets\Powershell_black.ico"/>
</RegistryKey>
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)\shell\open\command">
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\open\command">
<RegistryValue Type="string" Value="[$(var.ProductDirectoryName)]pwsh.exe -NoExit -Command Set-Location -LiteralPath '%V'"/>
</RegistryKey>
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)\shell\runas">
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\runas">
<RegistryValue Type="string" Name="MUIVerb" Value="$(var.ExplorerContextSubMenuElevatedDialogText)"/>
<RegistryValue Type="string" Name="Icon" Value="[$(var.ProductDirectoryName)]assets\Powershell_black.ico"/>
<RegistryValue Type="string" Value="" Name="HasLUAShield"/>
</RegistryKey>
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(env.ProductSemanticVersion)$(sys.BUILDARCH)\shell\runas\command">
<RegistryKey Root="HKCR" Key="Directory\ContextMenus\$(var.ProductName)$(var.SimpleProductVersion)$(sys.BUILDARCH)\shell\runas\command">
<RegistryValue Type="string" Value="[$(var.ProductDirectoryName)]pwsh.exe -NoExit -Command Set-Location -LiteralPath '%V'"/>
</RegistryKey>
</Component>
Expand All @@ -219,14 +219,14 @@
<Directory Id="ApplicationProgramsFolder" Name="$(var.ProductName)">
<Component Id="ApplicationProgramsMenuShortcut" Guid="{A77507A7-F970-4618-AC30-20AFE36EE2EB}">
<Shortcut Id="PowerShell_ProgramsMenuShortcut"
Name="$(var.ProductSemanticVersionWithNameAndOptionalArchitecture)"
Description="$(var.ProductSemanticVersionWithNameAndOptionalArchitecture)"
Name="$(var.ProductSimpleVersionWithNameAndOptionalArchitecture)"
Description="$(var.ProductSimpleVersionWithNameAndOptionalArchitecture)"
Target="[$(var.ProductDirectoryName)]pwsh.exe"
WorkingDirectory="$(var.ProductDirectoryName)"
Icon = "PowerShellExe.ico" />
<RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
<!-- HKMU is HKLM when installing perMachine and HKCU when installing perUser-->
<RegistryValue Root="HKMU" Key="Software\Microsoft\$(var.ProductSemanticVersionWithNameAndOptionalArchitecture)\ProgramsMenuShortcut" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
<RegistryValue Root="HKMU" Key="Software\Microsoft\PowerShellCore\ProgramsMenuShortcut\$(var.SimpleProductVersion)" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</Directory>
</Directory>
Expand All @@ -237,11 +237,13 @@
<Fragment>
<UI>
<Property Id="REGISTER_MANIFEST" Value="1" />
<Property Id="ADD_PATH" Value="$(var.AddPathDefault)" />
<Dialog Id="ExplorerContextMenuDialog" Width="370" Height="270" Title="!(loc.ExitDialog_Title)">
<!-- If the checkboxes are defined first, then they are first in the tab order and can be ticked and unticked using the spacebar -->
<Control Id="RegisterManifestCheckBox" Type="CheckBox" X="20" Y="60" Width="290" Height="17" Property="REGISTER_MANIFEST" CheckBoxValue="0" Text="Register Windows Event Logging Manifest"/>
<Control Id="EnablePSRemotingCheckBox" Type="CheckBox" X="20" Y="80" Width="290" Height="17" Property="ENABLE_PSREMOTING" CheckBoxValue="0" Text="Enable $(var.ProductName) remoting"/>
<Control Id="ContextMenuOpenPowerShell" Type="CheckBox" X="20" Y="100" Width="290" Height="17" Property="ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL" CheckBoxValue="0" Text="Add '$(var.ExplorerContextSubMenuDialogText)' context menus to Explorer"/>
<Control Id="AddToPathCheckBox" Type="CheckBox" X="20" Y="60" Width="290" Height="17" Property="ADD_PATH" CheckBoxValue="0" Text="Add $(var.ProductName) to Path Environment Variable"/>
<Control Id="RegisterManifestCheckBox" Type="CheckBox" X="20" Y="80" Width="290" Height="17" Property="REGISTER_MANIFEST" CheckBoxValue="0" Text="Register Windows Event Logging Manifest"/>
<Control Id="EnablePSRemotingCheckBox" Type="CheckBox" X="20" Y="100" Width="290" Height="17" Property="ENABLE_PSREMOTING" CheckBoxValue="0" Text="Enable $(var.ProductName) remoting"/>
<Control Id="ContextMenuOpenPowerShell" Type="CheckBox" X="20" Y="120" Width="290" Height="17" Property="ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL" CheckBoxValue="0" Text="Add '$(var.ExplorerContextSubMenuDialogText)' context menus to Explorer"/>
<Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="!(loc.WixUIBack)"/>
<Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Cancel="yes" Default="yes" Text="!(loc.WixUINext)"/>
<Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)">
Expand Down
128 changes: 128 additions & 0 deletions test/packaging/windows/msi.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

function Test-Elevated {
[CmdletBinding()]
[OutputType([bool])]
Param()

# if the current Powershell session was called with administrator privileges,
# the Administrator Group's well-known SID will show up in the Groups for the current identity.
# Note that the SID won't show up unless the process is elevated.
return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544")
}

function Invoke-Msiexec {
param(
[Parameter(ParameterSetName = 'Install', Mandatory)]
[Switch]$Install,

[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
[Switch]$Uninstall,

[Parameter(Mandatory)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a Test-Path in validate script

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

[ValidateScript({Test-Path -Path $_})]
[String]$MsiPath,

[Parameter(ParameterSetName = 'Install')]
[HashTable] $Properties

)
$action = "$($PsCmdlet.ParameterSetName)ing"
if ($Install.IsPresent) {
$switch = '/I'
} else {
$switch = '/x'
}

$additionalOptions = @()
if ($Properties) {
foreach ($key in $Properties.Keys) {
$additionalOptions += "$key=$($Properties.$key)"
}
}

$argumentList = "$switch $MsiPath /quiet /l*vx $msiLog $additionalOptions"
$msiExecProcess = Start-Process msiexec.exe -Wait -ArgumentList $argumentList -NoNewWindow -PassThru
if ($msiExecProcess.ExitCode -ne 0) {
$exitCode = $msiExecProcess.ExitCode
throw "$action MSI failed and returned error code $exitCode."
}
}

Describe -Name "Windows MSI" -Fixture {
BeforeAll {
$msiX64Path = $env:PsMsiX64Path

# Get any existing powershell core in the path
$beforePath = @(([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
Where-Object {$_ -like '*files\powershell*'})

$msiLog = Join-Path -Path $TestDrive -ChildPath 'msilog.txt'

foreach ($pathPart in $beforePath) {
Write-Warning "Found existing PowerShell path: $pathPart"
}

if (!(Test-Elevated)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we depend on the RequireAdminitstrator tag?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I would have to build the infrastructure for that. It depends on Start-PSPester and this isn't like the product tests. At least for the MSI tests, everything will require admin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification!

Write-Warning "Tests must be elevated"
}
$uploadedLog = $false
}
BeforeEach {
$Error.Clear()
}
AfterEach {
if ($Error.Count -ne 0 -and !$uploadedLog) {
if ($env:APPVEYOR) {
Push-AppveyorArtifact $msiLog
} else {
Copy-Item -Path $msiLog -Destination $env:temp -Force
Write-Verbose "MSI log is at $env:temp\msilog.txt" -Verbose
}
$uploadedLog = $true
}
}

Context "Add Path disabled" {
It "MSI should install without error" -Skip:(!(Test-Elevated)) {
{
Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 0}
} | Should -Not -Throw
}

It "MSI should have not be updated path" -Skip:(!(Test-Elevated)) {
$psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
Where-Object {$_ -like '*files\powershell*' -and $_ -notin $beforePath}

$psPath | Should -BeNullOrEmpty
}

It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
{
Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
} | Should -Not -Throw
}
}

Context "Add Path enabled" {
It "MSI should install without error" -Skip:(!(Test-Elevated)) {
{
Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 1}
} | Should -Not -Throw
}

It "MSI should have updated path" -Skip:(!(Test-Elevated)) {
$psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
Where-Object {$_ -like '*files\powershell*' -and $_ -notin $beforePath}

$psPath | Should -Not -BeNullOrEmpty
}

It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
{
Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
} | Should -Not -Throw
}
}
}
Loading