DSC - PowerShell 模块路径中不存在模块

Jas*_*tti 5 powershell dsc

这是我遇到的错误:“PowerShell 提供程序 <module> 在 PowerShell 模块路径中不存在,也未注册为 WMI 提供程序”

我并不是 PowerShell 和 DSC 的新手,但我很难弄清楚这一点。我已按照此处的指南进行故障排除。我还发现了这个几乎相同的问题......唯一的解决方案似乎是重新启动(我尝试过)。

所以我做了一个配置,导入一个模块,OctopusDSC。在对模块导入进行故障排除期间,我遗漏了很多参数,但这是我所拥有的:

Configuration OctopusServer
{
    Import-DscResource -Module OctopusDSC

    Node "WIN-ABC123" 
    {
        cTentacleAgent OctopusTentacle 
        { 
            Ensure = "Present"
            State = "Started"
            Name = "Tentacle"
            ApiKey = ""
            OctopusServerUrl = ""
            DefaultApplicationDirectory = "C:\Utility"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该模块存在于C:\Program Files\WindowsPowerShell\Modules本地和服务器上:

C:\>tree "C:\Program Files\WindowsPowerShell\Modules" /f
Folder PATH listing
Volume serial number is 9EC4-62C1
C:\PROGRAM FILES\WINDOWSPOWERSHELL\MODULES
????OctopusDSC
    ?   OctopusDSC.psd1
    ?
    ????DSCResources
        ????cTentacleAgent
                cTentacleAgent.psm1
                cTentacleAgent.schema.mof
Run Code Online (Sandbox Code Playgroud)

模块路径看起来不错(为了便于阅读,我在每个分号处都加了回车):

PS C:\> $env:PSModulePath
C:\Users\jasonc\Documents\WindowsPowerShell\Modules;
C:\Program Files\WindowsPowerShell\Modules;
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;
C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell
Run Code Online (Sandbox Code Playgroud)

我可以打电话Get-Module

PS C:\> Get-Module OctopusDSC -ListAvailable

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1      OctopusDSC                          {Get-TargetResource, Set-TargetResource, Test-TargetResource}
Run Code Online (Sandbox Code Playgroud)

我可以打电话Get-DscResource

PS C:\temp> Get-DscResource  -Name "cTentacleAgent"

ImplementedAs   Name                      Module                         Properties
-------------   ----                      ------                         ----------
PowerShell      cTentacleAgent            OctopusDSC                     {Name, ApiKey, DefaultApplicationDirectory,...
Run Code Online (Sandbox Code Playgroud)

这是我的错误:

PS C:\> Start-DscConfiguration -Path .\OctopusServer -Verbose -WhatIf -Wait
What if: [WIN-ABC123]: LCM:  [ Start  Set      ]
What if: [WIN-ABC123]: LCM:  [ End    Set      ]
The PowerShell provider OctopusDSC does not exist at the PowerShell module path nor is it registered as a WMI provider.
    + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : ModuleNameNotFound
    + PSComputerName        : WIN-ABC123
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗??

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34014
BuildVersion                   6.3.9600.16394
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2
Run Code Online (Sandbox Code Playgroud)

更新

我注意到此问题发生在安装了 Microsoft Monitoring Agent 的计算机上,但如果未安装该代理,则不会发生。

安装后,PSModulePath 机器级环境变量如下所示(为了清晰起见,拆分):

PS C:\> [environment]::GetEnvironmentVariable("PSModulePath","Machine")
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;
C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell
Run Code Online (Sandbox Code Playgroud)

如果我删除代理的路径,只C:\Windows\system32\WindowsPowerShell\v1.0\Modules\留在那里,就会找到 OctopusDSC。我必须重新启动 WMI Provider Host 进程才能更改环境变量。如果我将路径添加回...失败。

现在这让我感到困惑,因为模块安装的实际路径一直是C:\Program Files\WindowsPowerShell\Modules,并且大概是添加到PSModulePathPowerShell 本身计算的(我假设是因为我通常没有看到在系统环境中设置的路径变量)。

我所看到的其他地方复制C:\Program Files\WindowsPowerShell\ModulesPSModulePath可能会导致模块解析问题。那么它周围可能还有其他错误和敏感性吗?

我觉得这是一种答案,但我仍在寻找解决方法,因为删除该路径不是首选。

Kev*_*inC 1

作为健全性检查,请查看创建 .MOF 的目录...其中是否还有其他 MOF 文件?Start-DscConfiguration 运行目录中的所有 MOF。我重命名了我的 MOF 文件,但无法使错误消失,直到最终意识到错误出在旧的 MOF 中:(