首页 > 解决方案 > Powershell DSC 无法调用 Invoke-DscResource cmdlet

问题描述

我正在尝试使用 DSC 来配置我的 Service Fabric 群集虚拟机规模集中的节点。做一些注册表编辑,为了保持小,我只在下面显示一个。当我自己手动运行这些功能时,它们工作正常。当试图将它们嵌套在一个函数中时,我得到一个错误。

 configuration ServiceFabricNode {
     Node localhost
     {  
         SSLPerfectForwardSecrecyTLS12 ConfigureSSL {}          
         ServiceFabricAntivirusExclusions AntiVirusExclusions {}    
     }
 }

 configuration SSLPerfectForwardSecrecyTLS12 {
     Import-DscResource –ModuleName PSDesiredStateConfiguration
     Import-DscResource -ModuleName GraniResource

     # Disable Multi-Protocol Unified Hello
     Registry "DisableServerMultiProtocolUnifiedHello"
     {
         Key = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\Multi-Protocol
         Unified Hello\Server"
         ValueName = "Enabled"
         ValueType = "Dword"
         ValueData = "0"
         Ensure = "Present"
         Force = $true
      } 
  }

 configuration ServiceFabricAntivirusExclusions {   
      Import-DscResource -ModuleName WindowsDefender

      [string[]]$exclusionPath = "C:\Program Files\Microsoft Service abric\","D:\SvcFab\";
      Invoke-DscResource -Name WindowsDefender -ModuleName WindowsDefender -Method Set -Property @{ IsSingleInstance = 'Yes'; ExclusionPath = $exclusionPath }

      [string[]]$exlusionProcess = "Fabric.exe","FabricHost.exe","FabricInstallerService.exe","FabricSetup.exe","FabricDeployer.exe","ImageBuilder.exe","FabricGateway.exe","FabricDCA.exe","FabricFAS.exe","FabricUOS.exe","FabricRM.exe","FileStoreService.exe";
      Invoke-DscResource -Name WindowsDefender -ModuleName WindowsDefender -Method Set -Property @{ IsSingleInstance = 'Yes'; ExclusionProcess = $exlusionProcess } 
 }

 ServiceFabricNode

结果成

Compilation errors occurred while processing configuration 'ServiceFabricNode'. Please review the errors reported in error stream and modify your configuration code 
appropriately.
At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psm1:3917 char:5
+     throw $ErrorRecord
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (ServiceFabricNode:String) [], InvalidOperationException
    + FullyQualifiedErrorId : FailToProcessConfiguration

启用调试后,它会显示真正的异常

Cannot invoke the Invoke-DscResource cmdlet. The Invoke-DscResource cmdlet is in progress and must return before Invoke-DscResource can be invoked. Use -Force option if 
that is available to cancel the current operation.
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 1
    + PSComputerName        : localhost

我找不到 -Force 选项,Google 似乎过滤掉了 Invoke-DscResource 的所有错误,或者我是第一个使用它的人。有谁知道解决方案?也许我不必为 WindowsDefender 模块使用 Invoke-DscResource,但我没有看到其他方法。

标签: powershellazure-service-fabricdsc

解决方案


我通过以下博客信息弄清楚了http://nanalakshmanan.com/blog/Composite-Resources-Explained/

WindowsDefender 是一种复合资源

configuration ServiceFabricAntivirusExclusions
{
    Import-DscResource -ModuleName WindowsDefender

    [string[]]$exclusionPath = "C:\Program Files\Microsoft Service Fabric\","D:\SvcFab\";
    [string[]]$exlusionProcess = "Fabric.exe","FabricHost.exe","FabricInstallerService.exe","FabricSetup.exe","FabricDeployer.exe","ImageBuilder.exe","FabricGateway.exe","FabricDCA.exe","FabricFAS.exe","FabricUOS.exe","FabricRM.exe","FileStoreService.exe";

    WindowsDefender x
    { 
        IsSingleInstance = 'Yes';
        ExclusionPath = $exclusionPath;
        ExclusionProcess = $exlusionProcess;
    }
}

推荐阅读