【问题标题】:Powershell DSC what is the execution context for current configuration?Powershell DSC 当前配置的执行上下文是什么?
【发布时间】:2014-07-24 17:37:21
【问题描述】:

我正在尝试通过 DSC 部署 powershell 配置文件。 配置应将 .ps1 文件从网络共享复制到本地路径。

运行脚本失败并出现以下错误SourcePath must be accessible for current configuration.但此路径可从控制台访问,那么在 dsc 配置期间使用什么用户/上下文?

这是脚本


@ravikanth 回复后编辑


$ConfigurationData = @{
AllNodes = @(
    @{
        NodeName="*"
        PSDscAllowPlainTextPassword=$true
     }
    )
}
Configuration MyProfile
{ 
  param ([string[]]$MachineName,
        [PSCredential]$Credential)

  Node $MachineName
  {
    Log startconfig
    {
        # The message below gets written to the Microsoft-Windows-Desired State Configuration/Analytic log
        Message = "starting the file resource with ID MyProfile with $($myinvocation.mycommand) user : $env:username"
    }
    File profile
    {
      Credential=$credential
      Ensure = 'Present'   
      SourcePath = "\\web-mridf\powershell\profil_1.ps1"
      DestinationPath = "c:\temp\test.txt"  
      Type = "File" # Default is "File".
      DependsOn = "[Log]startconfig"      
    }

     Log AfterDirectoryCopy
     {
        # The message below gets written to the Microsoft-Windows-Desired State Configuration/Analytic log
        Message = "Finished running the file resource with ID MyProfile"
        DependsOn = "[File]profile" # This means run "MyProfile" first.
     }
  }
}

MyProfile -MachineName web-mridf -OutputPath c:\temp\dsc
Start-DscConfiguration -Path c:\temp\dsc -credential (get-credential("DOMAIN\user")) -force -verbose -Wait 

收到的错误(invalid argument

PS C:\temp> .\dsc.ps1


    Répertoire : C:\temp\dsc


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        04/06/2014     10:54       2834 web-mridf.mof
COMMENTAIRES : Effectuez l'opération « Invoquer une méthode CIM » avec les
paramètres suivants : « 'methodName' = SendConfigurationApply,'className' =
MSFT_DSCLocalConfigurationManager,'namespaceName' =
root/Microsoft/Windows/DesiredStateConfiguration ».

COMMENTAIRES : [WEB-MRIDF] :                            [[File]profile]
SourcePath must be accessible for current configuration.
COMMENTAIRES : [WEB-MRIDF] :                            [[File]profile] The
related file/directory is: \\web-mridf\powershell\profil_1.ps1.
SourcePath must be accessible for current configuration. The related
file/directory is: \\web-mridf\powershell\profil_smac.ps1. . L'ID de ressource
associé est [File]profile.
    + CategoryInfo          : InvalidArgument : (:) [], CimException
    + FullyQualifiedErrorId : MI RESULT 4
    + PSComputerName        : web-mridf

COMMENTAIRES : [WEB-MRIDF] : Gestionnaire de configuration local :  [ Fin
Définir  ]
La fonction SendConfigurationApply n'a pas réussi.
    + CategoryInfo          : InvalidArgument : (root/Microsoft/...gurationMan
   ager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 4
    + PSComputerName        : web-mridf

COMMENTAIRES : L'opération « Invoquer une méthode CIM » est terminée.
COMMENTAIRES : Le temps nécessaire à l'exécution du travail de configuration
est de 0.881 secondes

【问题讨论】:

    标签: powershell powershell-4.0 dsc


    【解决方案1】:

    DSC 本地配置管理器作为 SYSTEM 运行。因此,它将无法访问共享。您需要传递凭据才能访问共享。对于凭证,您需要使用证书加密密码或使用纯文本密码。

    对于纯文本密码,请查看我在 PowerShell 杂志上发布的文章。 http://www.powershellmagazine.com/2013/09/26/using-the-credential-attribute-of-dsc-file-resource/

    如果您想使用证书进行密码加密,请查看 PS Team 博客文章http://blogs.msdn.com/b/powershell/archive/2014/01/31/want-to-secure-credentials-in-windows-powershell-desired-state-configuration.aspx

    根据以下cmets更新:

    $AllNodes.Nodename 是使用配置数据时的关键。不要用静态节点名替换它。

    $ConfigurationData = @{
        AllNodes = @(
            @{
                NodeName="*"
                PSDscAllowPlainTextPassword=$true
             }
            @{
                NodeName="ServerName"
             }
        )
    }
    
    Configuration MyProfile 
    { 
        param (
            [PSCredential]$Credential
        ) 
    
        Node $AllNodes.NodeName
        { 
            Log startconfig 
            { 
                # The message below gets written to the Microsoft-Windows-Desired State Configuration/Analytic log 
                Message = "starting the file resource with ID MyProfile with $($myinvocation.mycommand) user : $env:username" 
            } 
            File profile 
            { 
                Credential=$credential 
                Ensure = 'Present' 
                SourcePath = "e:\powershell\profil_smac.ps1" 
                DestinationPath = "c:\temp\test2.txt2" 
                Type = "File" # Default is "File". 
                DependsOn = "[Log]startconfig" 
            } 
    
            Log AfterDirectoryCopy 
            { 
                # The message below gets written to the Microsoft-Windows-Desired State Configuration/Analytic log 
                Message = "Finished running the file resource with ID MyProfile" 
                DependsOn = "[File]profile" # This means run "MyProfile" first. 
            } 
        } 
    } 
    
    MyProfile -configurationdata $configurationdata -machinename "web-mridf.groupe.sa.colas.com" -credential (get-credential("groupe\sys-mac-smacsr")) -OutputPath c:\temp\dsc 
    Start-DscConfiguration -Path c:\temp\dsc -force -verbose -Wait
    

    【讨论】:

    • 谢谢 Ravikanth,凭据似乎已由 dsc 处理,但仍然出现错误。我正在更新我的问题
    • 单独发送凭据不起作用。配置数据在哪里?您需要使用 Start-DscConfiguration 指定 -ConfigurationData 参数。查看我分享的 PowerShell 杂志链接,了解如何使用配置数据的示例。
    • 抱歉复制/粘贴错误配置数据存在于我的脚本中
    • 哦,好吧。您已添加配置数据但未使用它! :) 请参阅我在文章中的示例。您需要通过配置数据传递节点名称。此外,您正在使用 Start-DscConfiguration 的凭据参数。那是错的。您必须将凭据传递给您的配置命令,即 MyProfile。我更新了我的答案。
    • 再次感谢,我可以看到我的愚蠢错误^^。因此,如果我想传递多个机器名称,没有选项可以将配置数据应用到所有主机 (nodename="*"),如您在博客上建议的那样?
    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2018-06-22
    • 2011-06-13
    相关资源
    最近更新 更多