0

尝试访问网络共享文件夹时,DSC 会返回“访问被拒绝”错误,尽管我已为其提供了有效凭据。

我正在使用 DSC 配置,其中 DSC“脚本”资源如下:

Script myScriptResource {
        GetScript = {return $true}
        SetScript = {
            $setupShare = '\\SomeNetworkSharesFolder\subFolder'
            # This line produces valid results when run directly on node VM.
            $build = Get-ChildItem "FileSystem::$setupShare" -Name | Sort-Object -Descending | Select-Object -First 1 | Out-String
            Write-Host "Final Build: $build"
        }
        TestScript = {return $false} #Always run Set-Script block!
        Credential = $ValidNetworkShareCredential
        PsDscRunAsCredential = $ValidNetworkShareCredential
    }

我收到一个错误:

VERBOSE: [MyNodeVM]:                            [[Script]myScriptResource] Performing the operation "Set-TargetResource" on target "Executing t
he SetScript with the user supplied credential".
Access is denied
    + CategoryInfo          : PermissionDenied: (\\SomeNetworkSharesFolder\subFolder:) [], CimException
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : myNodeVM

这可能是因为节点 VM 上的 LCM 默认使用本地 SYSTEM 用户凭据。

我尝试通过导航到 Windows 服务管理器(提示:RUN然后services.msc)手动更改用户凭据,并在winRM 服务属性的登录选项卡中更改用户凭据。每次我尝试运行Windows 远程管理 (WS-Managment)服务时,我都会收到错误消息:

Windows 无法在本地计算机上启动 Windows 远程管理 (WS-Management) 服务。错误 1079:为该服务指定的帐户与为在同一进程中运行的其他服务指定的帐户不同。

我不知道如何更改 LCM 的凭据,以便它可以在执行 Get-ChildItem 时访问网络共享文件夹。

4

2 回答 2

1
Script myScriptResource {
    GetScript = {return $true}
    SetScript = {
        $username ="someusername"
        $secpasswd = ConvertTo-SecureString “somepassword” -AsPlainText -Force
        $mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
        $setupShare = '\\SomeNetworkSharesFolder\subFolder'
        $psDriveArgs = @{ Name = ([guid]::NewGuid()); PSProvider = "FileSystem"; Root = $setupShare; Scope = "Private"; Credential = $mycreds }
        new-psdrive @psDriveArgs -ErrorAction Stop
        # This line produces valid results when run directly on node VM.
        $build = Get-ChildItem "FileSystem::$setupShare"  | Sort-Object -Descending | Select-Object -First 1 | Out-String
        Write-Host "Final Build: $build"
    }
    TestScript = {return $false} #Always run Set-Script block!
}
于 2016-03-25T19:51:09.607 回答
0

没有一种简单的方法可以使其与脚本资源一起使用,因为您需要能够将凭据传递给脚本资源,以便您可以安装驱动器并使用它来复制/粘贴。如果要从共享中复制文件/目录,可以使用“文件”资源。如果要将文件/目录复制到共享,可以使用 xPsDesiredStateConfiguration ( https://gallery.technet.microsoft.com/xPSDesiredStateConfiguratio-417dc71d ) 模块中的“xFileUpload”资源。如果您确实需要使用脚本资源来完成这项工作,请查看 xFileUpload 资源是如何完成的。

于 2016-03-22T19:26:26.833 回答