为什么我无法在推送模式下使用安装在用户模块路径中的模块中的 DSC 资源?

Mic*_*ter 5 powershell dsc

我有一个 Powershell DSC 自定义资源,嵌入在模块中。如果您想查看完整的代码,可以在这里找到,但这可能不是必需的;我的问题不在于(我认为)我编写的代码,而在于如何访问它。

就我而言,我使用的是 DSC 推送模式。我使用一个普通的 Powershell 脚本来安装所需的模块、编译配置并启动它。在该脚本中,我将模块的位置添加到$env:PSModulePath. 完成此操作后,我可以看到该模块并将其导入我的交互式 shell 中,如下所示:

PS> Get-Module -ListAvailable | Where-Object -Property Name -eq cWinTrialLab | Import-Module
PS> Get-DscResource -Module cWinTrialLab

ImplementedAs   Name                      ModuleName                     Version    Properties
-------------   ----                      ----------                     -------    ----------
PowerShell      cWtlShortcut              cWinTrialLab                   0.0.1      {Ensure, ShortcutPath, TargetPath, DependsOn...}
Run Code Online (Sandbox Code Playgroud)

所以我创建一个测试配置文件:

Configuration TestConfig {

    Import-DscResource -ModuleName PSDesiredStateConfiguration
    Import-DscResource -ModuleName cWinTrialLab

    Node localhost {

        cWtlShortcut "TestShortcut" {
            Ensure = "Present"
            ShortcutPath = "$env:USERPROFILE\Desktop\TestShortcut.lnk"
            TargetPath = "C:\Windows\system32\cmd.exe"
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用它:

PS> . .\testDscConfig.ps1
PS> TestConfig
PS> Start-DscConfiguration -Wait -Force TestConfig
Run Code Online (Sandbox Code Playgroud)

编译 MOF 似乎可以工作,但实际上启动配置失败,并显示:

The PowerShell DSC resource cWtlShortcut from module <cWinTrialLab,0.0.1> does not exist at the PowerShell module path nor is it registered as a WMI DSC resource.
At $Home\Documents\psyops\submod\wintriallab\azure\test.ps1:13 char:1
+ Start-DscConfiguration -Wait -Force TestConfig
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : DscResourceNotFound
    + PSComputerName        : localhost
Run Code Online (Sandbox Code Playgroud)

什么可能导致这种情况?

  1. 我已确保我$env:PSModulePath.
  2. 似乎有些东西正在发挥作用;它可以确定错误消息中模块的正确版本(当我更改模块.psd1文件中的版本时,错误消息中报告的版本也会更改)
  3. 我不认为这与为什么 DSC 找不到已安装的资源有关?因为我使用的是推送模式,并且不存在模块版本控制问题。
  4. 它可能与 中的非标准条目有关吗$env:PSModulePath?我尝试将模块复制到$env:ProgramFiles\WindowsPowershell\Modules,但随后出现一个我真的不明白的错误: Importing module cWinTrialLab failed with error - File C:\Program Files\WindowsPowerShell\Modules\cWinTrialLab\cWtlShortcut\cWtlShortcut.psm1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies。作为记录:

    PS> Get-ExecutionPolicy -List
            Scope ExecutionPolicy
            ----- ---------------
    MachinePolicy       Undefined
       UserPolicy       Undefined
          Process       Undefined
      CurrentUser    Unrestricted
     LocalMachine    Unrestricted
    
    Run Code Online (Sandbox Code Playgroud)
  5. 也许是我的目录布局有问题?我实际上还没有见过包含多个基于类的 DSC 资源的模块示例,因此我对布局进行了猜测。
  6. (如果有此类模块的示例 - 单个模块,包含多个基于类的 DSC 资源 - 指向它的链接将非常有帮助。)

最后,我在 Windows 10 上使用 Powershell 5.1,并从 Windows 更新应用所有更新。(最终,我将在 Server 2016 计算机上部署我的配置。)

我还能错过什么?

Mat*_*ore 2

处理 DSC 配置在系统帐户下运行,因此不会搜索特定用户的 PSModulePath。将模块放在标准位置之一,通常位于$env:ProgramFiles\WindowsPowerShell\Modules.

当您说您修改了 时$env:PSModulePath,我假设您是为当前用户执行此操作,而不是修改计算机范围的设置。

如果您需要资源访问特定用户可用的资源,某些资源将允许将凭据对象作为参数传递给资源。