我有一个PowerShell DSC自定义资源,embedded在模块中。 完整的代码可以在这里find,如果你想看到它,但可能没有必要; 我的问题不是(我认为)在我写的代码中,而是在如何访问它。
就我而言,我正在使用DSC推送模式。 有一个正常的Powershell脚本我用来安装所需的模块,编译configuration,并启动它。 在该脚本中,我将模块的位置添加到$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...}
所以我创build一个testingconfiguration文件:
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" } } }
我尝试使用它:
PS> . .\testDscConfig.ps1 PS> TestConfig PS> Start-DscConfiguration -Wait -Force TestConfig
编译MOF似乎工作,但实际上启动configuration失败:
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
什么可能导致这个?
$env:PSModulePath中的所有文件夹中只有一个模块副本。 .psd1文件的版本时,错误消息中报告的版本也会更改) 它可能与$env:PSModulePath的非标准条目有关$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 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 。 作为logging:
PS> Get-ExecutionPolicy -List Scope ExecutionPolicy ----- --------------- MachinePolicy Undefined UserPolicy Undefined Process Undefined CurrentUser Unrestricted LocalMachine Unrestricted
最后,我在Windows 10上使用Powershell 5.1,并从Windows Update应用所有更新。 (最终,我将在一台Server 2016机器上部署我的configuration。)
还有什么我可以错过?
处理DSCconfiguration在系统帐户下运行,因此不search特定用户的PSModulePath。 将模块置于其中一个标准位置,通常位于$env:ProgramFiles\WindowsPowerShell\Modules 。
当你说你修改了你的$env:PSModulePath ,我假设你已经为你的当前用户做了这个,而不是修改一个机器的宽度设置。
如果您需要资源访问可用于特定用户的资源,则某些资源将允许将凭据对象作为parameter passing给资源。