Microsoft365DSC icon indicating copy to clipboard operation
Microsoft365DSC copied to clipboard

Cannot find a variable with the name 'SourceLocation'

Open yingying96 opened this issue 2 years ago • 8 comments

Hi guys, i'm trying to follow this white paper here - https://office365dsc.azurewebsites.net/Pages/Resources/Whitepapers/Managing%20Microsoft%20365%20with%20Microsoft365Dsc%20and%20Azure%20DevOps.pdf

However i'm facing this issue here when running through the release pipeline. Not too sure what's the issue on this. When i try on local, i can also replicate the same issue.

image

2022-04-22T04:32:24.7955570Z ##[error]Get-Variable : Cannot find a variable with the name 'SourceLocation'. At C:\Program

Files\WindowsPowerShell\Modules\PowerShellGet\2.2.4\PSModule.psm1:12199 char:21

  • ... if (Get-Variable -Name SourceLocation -ErrorAction SilentlyCo ...

  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (SourceLocation:String) [Get-Var

    iable], ItemNotFoundException

    • FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.G

    etVariableCommand 2022-04-22T04:32:24.8077335Z ##[error]PowerShell exited with code '1'.

yingying96 avatar Apr 22 '22 06:04 yingying96

Hello,

To me this is expected to fail as this is in the DynamicParam section of the module. What surprises me though is that you shouldn't see this error as the switch tells PowerShell to "SilentlyContinue" in case of an error.

DynamicParam {
        if (Get-Variable -Name Name -ErrorAction SilentlyContinue) {
            $moduleSource = Get-PSRepository -Name $Name -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

            if ($moduleSource) {
                $providerName = (Get-ProviderName -PSCustomObject $moduleSource)

                $loc = $moduleSource.SourceLocation

                if (Get-Variable -Name SourceLocation **-ErrorAction SilentlyContinue**) {
                    $loc = $SourceLocation
                }

                if (Get-Variable -Name PackageManagementProvider **-ErrorAction SilentlyContinue**) {
                    $providerName = $PackageManagementProvider
                }

                $null = Get-DynamicParameters -Location $loc -PackageManagementProvider ([REF]$providerName)
            }
        }
    }

I'm looking at the white paper to try and reproduce this.

Regards, Thierry

Kirikou974 avatar Apr 22 '22 10:04 Kirikou974

I see. Please let me know if u are able to successfully update the config by following the whitepaper. Here's my deploy.ps1 config which is largely similar to the whitepaper except for some local path which i was trying to run on my machine but got similar errors.

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType] 'Tls12'
Update-M365DSCDependencies
function Write-Log
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Message
    )

    $timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
    Write-Host "$timestamp - $Message"
}

Write-Log -Message 'Checking for presence of Microsoft365Dsc module and all required modules'
Write-Log -Message ' '

$modules = Import-PowerShellDataFile -Path 'D:\Git\Microsoft 365 Config\DscResources.psd1'

$workingDirectory = $PSScriptRoot

if ($modules.ContainsKey("Microsoft365Dsc"))
{
    Write-Log -Message 'Checking Microsoft365Dsc version'
    $psGalleryVersion = $modules.Microsoft365Dsc
    $localModule = Get-Module 'Microsoft365Dsc' -ListAvailable

    Write-Log -Message "- Required version: $psGalleryVersion"
    Write-Log -Message "- Installed version: $($localModule.Version)"
    Write-Log -Message ' '

    if ($localModule.Version -ne $psGalleryVersion)
    {
        Write-Log -Message 'Incorrect version installed. Removing current module.'
        foreach ($requiredModule in $localModule.RequiredModules)
        {
            $requiredModulePath = Join-Path -Path 'C:\Program Files\WindowsPowerShell\Modules' -ChildPath $requiredModule.Name
            Remove-Item -Path $requiredModulePath -Force -Recurse -ErrorAction 'SilentlyContinue'
        }

        Write-Log -Message 'Installing Microsoft365Dsc and required modules'
        Set-PSRepository -Name 'PSGallery' -InstallationPolicy 'Trusted'
        Install-Module -Name 'Microsoft365Dsc' -RequiredVersion $psGalleryVersion -AllowClobber

        Write-Log -Message 'Modules installed successfully!'
        Write-Log -Message ' '
    }
    else
    {
        Write-Log -Message 'Correct version installed, continuing.'
        Write-Log -Message ' '
    }
}
else
{
    Write-Log "[ERROR] Unable to find Microsoft365Dsc in DscResources.psd1. Cancelling!"
    exit
}

Start-DscConfiguration -Path 'D:\Git\Microsoft 365 Config' -Verbose -Wait -Force
 
if ($error.Count -gt 0)
{
    write-host "the following errors occurred during dsc configuration";
    write-host ($error | fl * | out-string );
    throw $error[-1];
}

yingying96 avatar Apr 23 '22 07:04 yingying96

We managed to resolve this issue using our self hosted pipeline. Seems like we had that issue because of the azure pipeline (win 2019)

yingying96 avatar Apr 25 '22 06:04 yingying96

I can preproduce this issue within my lab. This issue is not related to Microsoft365DSC though. If we do find a solutions to overcome this error message, we should have it documented.

@Kirikou974 Were you able to find a solution to this issue?

andikrueger avatar Jun 10 '22 10:06 andikrueger

One solution could be to remove the last six lines of code. There is an error playback built into the script. This will throw an error, that causes to release to fail. The configuration will complete just fine. All messages below are generated by those last lines.

2022-06-10T10:51:25.5301942Z the following errors occurred during dsc configuration
2022-06-10T10:51:25.5919388Z Get-Variable : Cannot find a variable with the name 'PackageManagementProvider'.
2022-06-10T10:51:25.5935776Z At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:12205 char:21
2022-06-10T10:51:25.5956308Z + ...         if (Get-Variable -Name PackageManagementProvider -ErrorAction ...
2022-06-10T10:51:25.5966279Z +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-06-10T10:51:25.5985658Z     + CategoryInfo          : ObjectNotFound: (PackageManagementProvider:String) [Get-Variable], ItemNotFoundException
2022-06-10T10:51:25.6002262Z     + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
2022-06-10T10:51:25.6017571Z  
2022-06-10T10:51:25.6033508Z Get-Variable : Cannot find a variable with the name 'SourceLocation'.
2022-06-10T10:51:25.6047816Z At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:12201 char:21
2022-06-10T10:51:25.6061684Z + ...         if (Get-Variable -Name SourceLocation -ErrorAction SilentlyCo ...
2022-06-10T10:51:25.6104611Z +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-06-10T10:51:25.6123524Z     + CategoryInfo          : ObjectNotFound: (SourceLocation:String) [Get-Variable], ItemNotFoundException
2022-06-10T10:51:25.6138275Z     + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
2022-06-10T10:51:25.6154885Z  
2022-06-10T10:51:25.6159942Z 
2022-06-10T10:51:25.7328127Z ##[error]Get-Variable : Cannot find a variable with the name 'SourceLocation'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:12201 char:21
+ ...         if (Get-Variable -Name SourceLocation -ErrorAction SilentlyCo ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (SourceLocation:String) [Get-Variable], ItemNotFoundException
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand
2022-06-10T10:51:25.7378433Z ##[error]PowerShell exited with code '1'.
2022-06-10T10:51:25.7873150Z ##[section]Finishing: PowerShell Script

@ykuijs Is there any alternative solution to doing so or what would be the impact of leaving it out?

andikrueger avatar Jun 10 '22 10:06 andikrueger

@ykuijs can you have a look when you are back? Thanks

NikCharlebois avatar Jul 22 '22 19:07 NikCharlebois

I found the root cause of this. It is caused by the way the Set-PSRepository cmdlet generates but suppresses errors. You can add $error.Clear() on the line after the Set-PSRepository cmdlet in the deploy.ps1 script. That will fix the issue.

This will be fixed in the new scripts for the new version of the whitepaper I am working on.

ykuijs avatar Sep 13 '22 12:09 ykuijs

Additional info: The error was only thrown when a new version of M365DSC had to be installed by the deploy script, because the Set-PSRepository cmdlet is executed in that process. When the specified version is already installed, there are no issues.

ykuijs avatar Sep 13 '22 13:09 ykuijs

Closing as there is a solution available.

andikrueger avatar Oct 17 '22 17:10 andikrueger