gpt4 book ai didi

powershell - 对于 Desired State Configuration File 资源,为什么 Credential 起作用,而 PsDscRunAsCredential 不起作用?

转载 作者:行者123 更新时间:2023-12-03 00:45:13 26 4
gpt4 key购买 nike

我正在尝试学习如何在 PowerShell Desired State Configuration 中使用凭据,但我不太明白 Credential 之间的区别。和 PsDscRunAsCredential资源的属性。

作为一个简单的测试用例,我试图将文件夹及其内容从文件共享复制到目标节点上的本地文件夹。目标节点的计算机帐户无权访问共享,因此我提供了有权访问的凭据。如果我将凭据分配给 Credential File 资源的属性,它可以工作。如果我使用 PsDscRunAsCredential属性,当它尝试访问文件共享时出现“访问被拒绝”错误。

Configuration FileWithCredential {

Import-DscResource -ModuleName PSDesiredStateConfiguration

Node $AllNodes.NodeName {

# copy files from a file share to a new folder on the target node
File CopyFileShareFolder {
Ensure = 'Present'
Type = 'Directory'
Recurse = $true
SourcePath = '\\fileshare\folder\subfolder\stuff_I_want_to_copy'
DestinationPath = 'c:\ps\DSC\filesharedestination'
# If I use Credential instead of PsDscRunAsCredential, it works
# Credential = Get-Credential -UserName ourdomain\myaccout -Message 'Enter Password'
PsDscRunAsCredential = Get-Credential -UserName ourdomain\myaccout -Message 'Enter Password'
}
}
}

$ConfigData = @{
AllNodes = @(
@{
NodeName = 'target-server'
PsDscAllowDomainUser = $true
PsDscAllowPlainTextPassword = $true
}
)
}

我正在 Windows 10 机器上编译 MOF 并将配置推送到 Server 2016 机器。两者都运行 PowerShell 5.1。
PS C:\Users\me\Documents\pscode\dsc_test2> FileWithCredential -ConfigurationData $ConfigData
PS C:\Users\me\Documents\pscode\dsc_test2> Start-DscConfiguration .\FileWithCredential\ -Verbose -Wait -Force

我明白 PsDscRunAsCredential是新的,我假设有理由更喜欢它而不是旧的 Credential ,但我无法弄清楚真正的区别是什么。它们基本上可以互换吗?如果不是,我错过了什么会使“作为”凭证工作?

注意:我了解允许纯文本密码的安全风险,但现在我只是想了解如何传递凭据并确保它们有效。学习如何安全地这样做是我的 list 上的下一个。

最佳答案

看来我的预感可能是对的。把我的评论变成答案:

I just had a thought: the File resource is.. somewhat special. It's one of the only resources implemented as binary. It has some strange quirks like not returning a ModuleName when you run Get-DscResource. Maybe it's implemented in a way that the LCM cannot change its context. It would be interesting to turn on debugging for DSC, then break into the debugger and check out your context, try to access the share and local file system, etc.



在运行测试时,情况似乎是(无论出于何种原因) File资源根本无法正确支持 PsDscRunAsCredential .

这是使用 Invoke-DscResource 的演示, 比较 FileScript并显示结果文件的所有者不同。

File ,店主永远是 SYSTEM .

Script ,店主是 SYSTEM当没有凭据运行时(预期),但是当使用凭据运行时,所有者是不同的(在我的情况下,所有者成为本地 Administrators 组,因为我提供的凭据是本地管理员)。
$cred = Get-Credential

$module = 'PsDesiredStateConfiguration'

$fprop = @{
DestinationPath = "$env:USERPROFILE\delme.txt"
Contents = "Hello"
}

$sprop = @{
GetScript = { @{} }
TestScript = { $false }
SetScript = [ScriptBlock]::Create("'Hello'|sc '$env:USERPROFILE\delme.txt'")
}

$cprop = @{ PsDscRunAsCredential = $cred }

function Assert-FileTest { Get-Item -LiteralPath $fprop.DestinationPath | % { $_.GetAccessControl().Owner | Write-Verbose -Verbose ; $_ } | Remove-Item -Force }

Invoke-DscResource -Name File -ModuleName $module -Method Set -Property $fprop -Verbose
Assert-FileTest

Invoke-DscResource -Name File -ModuleName $module -Method Set -Property ($fprop + $cprop) -Verbose
Assert-FileTest


Invoke-DscResource -Name Script -ModuleName $module -Method Set -Property $sprop -Verbose
Assert-FileTest

Invoke-DscResource -Name Script -ModuleName $module -Method Set -Property ($sprop + $cprop) -Verbose
Assert-FileTest

关于powershell - 对于 Desired State Configuration File 资源,为什么 Credential 起作用,而 PsDscRunAsCredential 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49661060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com