Skip to content

Commit

Permalink
Install-M365DSCDevBranch, Update-M365DSCModule, Update-M365DSCDepende…
Browse files Browse the repository at this point in the history
…ncies: Make Scope available microsoft#3621

Co-authored-by: Thomas Subotitsch @tbone4711
  • Loading branch information
fasteiner committed Aug 30, 2023
1 parent 579928e commit 50125c1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* DEPENDENCIES
* Updated Microsoft.Graph to version 2.4.0.
* Updated ReverseDSC to version 2.0.0.18
* Updated Install-M365DSCDevBranch, Update-M365DSCDependencies and Update-M365DSCModule to be usable with -Scope, allowing the user to install/update the module dependencies without admin rights, using current user scope. Confirm-M365DSCDependencies error message changed to reflect this change.
FIXES [#3621](https://github.com/microsoft/Microsoft365DSC/issues/3621)


* MISC
* Fixes an issue with the generic export CIM Instance logic.
FIXES [#3610](https://github.com/microsoft/Microsoft365DSC/issues/3610)
Expand Down
72 changes: 53 additions & 19 deletions Modules/Microsoft365DSC/Modules/M365DSCUtil.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ function Confirm-M365DSCDependencies
{
$ErrorMessage += ' * ' + $invalidDependency.ModuleName + "`r`n"
}
$ErrorMessage += 'Please run Update-M365DSCDependencies as Administrator.'
$ErrorMessage += 'Please run Update-M365DSCDependencies with scope "currentUser" or as Administrator.'
$ErrorMessage += 'Please run Uninstall-M365DSCOutdatedDependencies.'
$Script:M365DSCDependenciesValidated = $false
Add-M365DSCEvent -Message $ErrorMessage -EntryType 'Error' `
Expand Down Expand Up @@ -2257,16 +2257,26 @@ function Get-SPOUserProfilePropertyInstance
.Description
This function downloads and installs the Dev branch of Microsoft365DSC on the local machine
.Parameter Scope
Specifies the scope of the update of the module. The default value is AllUsers(needs to run as elevated user).
.Example
Install-M365DSCDevBranch
.Example
Install-M365DSCDevBranch -Scope CurrentUser
.Functionality
Public
#>
function Install-M365DSCDevBranch
{
[CmdletBinding()]
param()
param(
[Parameter()]
[ValidateSet("CurrentUser", "AllUsers")]
$Scope = "AllUsers"
)

#region Download and Extract Dev branch's ZIP
Write-Host 'Downloading the Zip package...' -NoNewline
Expand All @@ -2283,24 +2293,24 @@ function Install-M365DSCDevBranch
#region Install All Dependencies
$manifest = Import-PowerShellDataFile "$extractPath\Microsoft365DSC-Dev\Modules\Microsoft365DSC\Microsoft365DSC.psd1"
$dependencies = $manifest.RequiredModules
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
if ((-not(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) -and ($Scope -eq "AllUsers"))
{
Write-Error 'Cannot update the dependencies for Microsoft365DSC. You need to run this command as a local administrator.'
}
else
{
foreach ($dependency in $dependencies)
{
Write-Host "Installing {$($dependency.ModuleName)}..." -NoNewline
$existingModule = Get-Module $dependency.ModuleName -ListAvailable | Where-Object -FilterScript { $_.Version -eq $dependency.RequiredVersion }
if ($null -eq $existingModule)
{
Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -Force -AllowClobber -Scope 'AllUsers' | Out-Null
Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -Force -AllowClobber -Scope $Scope | Out-Null
}
Import-Module $dependency.ModuleName -Force | Out-Null
Write-Host 'Done' -ForegroundColor Green
}
}
else
{
Write-Error 'Cannot update the dependencies for Microsoft365DSC. You need to run this command as a local administrator.'
}
#endregion

#region Install M365DSC
Expand Down Expand Up @@ -2734,12 +2744,21 @@ This function installs all missing M365DSC dependencies
.Parameter Force
Specifies that all dependencies should be forcefully imported again.
.Parameter ValidateOnly
Specifies that the function should only return the dependencies that are not installed.
.Parameter Scope
Specifies the scope of the update of the module. The default value is AllUsers(needs to run as elevated user).
.Example
Update-M365DSCDependencies
.Example
Update-M365DSCDependencies -Force
.Example
Update-M365DSCDependencies -Scope CurrenUser
.Functionality
Public
#>
Expand All @@ -2754,7 +2773,10 @@ function Update-M365DSCDependencies

[Parameter()]
[Switch]
$ValidateOnly
$ValidateOnly,
[Parameter()]
[ValidateSet("CurrentUser", "AllUsers")]
$Scope = "AllUsers"
)

$Global:MaximumFunctionCount = 32767
Expand All @@ -2779,7 +2801,11 @@ function Update-M365DSCDependencies

if ((-not $found -or $Force) -and -not $ValidateOnly)
{
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
if ((-not(([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) -and ($Scope -eq "AllUsers"))
{
Write-Error 'Cannot update the dependencies for Microsoft365DSC. You need to run this command as a local administrator.'
}
else
{
Write-Information -MessageData "Installing $($dependency.ModuleName) version {$($dependency.RequiredVersion)}"
Remove-Module $dependency.ModuleName -Force -ErrorAction SilentlyContinue
Expand All @@ -2788,11 +2814,7 @@ function Update-M365DSCDependencies
Remove-Module 'Microsoft.Graph.Authentication' -Force -ErrorAction SilentlyContinue
}
Remove-Module $dependency.ModuleName -Force -ErrorAction SilentlyContinue
Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -AllowClobber -Force -Scope 'AllUsers'
}
else
{
Write-Error 'Cannot update the dependencies for Microsoft365DSC. You need to run this command as a local administrator.'
Install-Module $dependency.ModuleName -RequiredVersion $dependency.RequiredVersion -AllowClobber -Force -Scope "$Scope"
}
}

Expand Down Expand Up @@ -4011,19 +4033,32 @@ function Test-M365DSCModuleValidity
.Description
This function updates the module, dependencies and uninstalls outdated dependencies.
.Parameter Scope
Specifies the scope of the update of the module. The default value is AllUsers(needs to run as elevated user).
.Example
Update-M365DSCModule
.Example
Update-M365DSCModule -Scope CurrentUser
.Example
Update-M365DSCModule -Scope AllUsers
.Functionality
Public
#>
function Update-M365DSCModule
{
[CmdletBinding()]
param()
param(
[Parameter()]
[ValidateSet("CurrentUser", "AllUsers")]
$Scope = "AllUsers"
)
try
{
Update-Module -Name 'Microsoft365DSC' -ErrorAction Stop
Update-Module -Name 'Microsoft365DSC' -ErrorAction Stop -Scope $Scope
}
catch
{
Expand All @@ -4049,9 +4084,8 @@ function Update-M365DSCModule
{
throw $_
}
Update-M365DSCDependencies
Update-M365DSCDependencies -Scope $Scope
Uninstall-M365DSCOutdatedDependencies
}

<#
.Description
Expand Down

0 comments on commit 50125c1

Please sign in to comment.