Powershell DSC qual é o contexto de execução para a configuração atual?
-
21-12-2019 - |
Pergunta
Estou tentando implantar um perfil do PowerShell por meio do DSC.A configuração deve copiar um arquivo .ps1 de um compartilhamento de rede para um caminho local.
A execução do script falha com o seguinte erro SourcePath deve estar acessível para a configuração atual. ainda assim, esse caminho é acessível a partir do console, então qual usuário/contexto é usado durante a configuração do dsc?
Aqui está o roteiro
Edite após a resposta de @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
E o erro recebido ( argumento inválido)
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
Solução
O gerenciador de configuração local DSC é executado como SYSTEM.Portanto, não terá acesso ao compartilhamento.Você precisa passar as credenciais para acessar o compartilhamento.Para as credenciais, você precisa usar certificados para criptografar a senha ou usar uma senha em texto simples.
Para a senha em texto simples, verifique o artigo que publiquei na PowerShell Magazine.http://www.powershellmagazine.com/2013/09/26/using-the-credential-attribute-of-dsc-file-resource/
Se você quiser usar certificados para criptografia de senha, verifique a postagem do blog PS Team em http://blogs.msdn.com/b/powershell/archive/2014/01/31/want-to-secure-credentials-in-windows-powershell-desired-state-configuration.aspx
Atualização com base nos comentários abaixo:
O $AllNodes.Nodename é a chave ao usar dados de configuração.Não substitua isso por um nome de nó estático.
$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