Skip to content

Commit

Permalink
Merge pull request #3622 from fasteiner/Dev
Browse files Browse the repository at this point in the history
#3621: Dependencies Make Scope available
  • Loading branch information
ykuijs authored Aug 31, 2023
2 parents d7fec90 + b299a6b commit 4b13379
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Change log for Microsoft365DSC

# UNRELEASED

* EXORemoteDomain
* Implemented a wait/retry mecanism between the New-RemoteDomain and Set-RemoteDomain to avoid timeout.
FIXES [#3628](https://github.com/microsoft/Microsoft365DSC/issues/3628)
* DEPENDENCIES
* 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)

# 1.23.830.1

Expand Down
71 changes: 53 additions & 18 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,7 +4084,7 @@ function Update-M365DSCModule
{
throw $_
}
Update-M365DSCDependencies
Update-M365DSCDependencies -Scope $Scope
Uninstall-M365DSCOutdatedDependencies
}

Expand Down

0 comments on commit 4b13379

Please sign in to comment.