如何解决 - PowerShell DSC 不支持相对路径?

当我们从在线或网站下载文件并为此使用“文件” DscResource时,PowerShell DSC 通常会出现“不支持相对路径”错误。

在下面的示例中,我们使用 DSC 从 GitHub 将 PowerShell 7.1.4 版本下载到本地计算机,我们收到以下错误。

示例

Configuration FileCopy{
   Node LocalHost{

      File CopyFromBlob{
         SourcePath = "https://github.com/PowerShell/PowerShell/releases/download/v7.1.4/PowerShell-7.1.4-win-x86.msi"
         DestinationPath = "C:\Temp\"
         Ensure = 'Present'
      }
   }
}
FileCopy -OutputPath C:\Temp\dsc\FileCopy
Start-DscConfiguration -Path C:\Temp\dsc\FileCopy -Wait -Force
输出结果
Relative path is not supported. The related file/directory is:https://github.com/PowerShell/PowerShell/releases/download/v7.1.4/PowerShell-7.1.4-win-x86.msi.
   + CategoryInfo : InvalidArgument: (:) [], CimException
   + FullyQualifiedErrorId : MI RESULT 4
   + PSComputerName : LocalHost

要解决此错误,我们可以使用“Script” DSC 资源,或者我们需要下载并安装额外的 DSC 资源“xRemoteFile”以在线下载文件。

要安装“xRemoteFile”资源,请使用以下命令。

Find-DscResource xRemoteFile | Install-Module -Force -Verbose

安装命令后,我们可以使用以下配置。

Configuration FileCopy{

   Import-DscResource -ModuleName xPSDesiredStateConfiguration
   Node LocalHost{
      xRemoteFile FileDownload{
         URI = "https://github.com/PowerShell/PowerShell/releases/download/v7.1.4/PowerShell-7.1.4-win-x86.msi"
         DestinationPath = "C:\Temp\PowerShell-7.1.4-win-x86.msi"
      }
   }
}
FileCopy -OutputPath C:\Temp\dsc\FileCopy
Start-DscConfiguration -Path C:\Temp\dsc\FileCopy -Wait -Force

您还可以使用内置的 Script DSC 资源,如下所示。

Configuration FileCopy{
   Node LocalHost{
      Script DownloadFile{
         GetScript = {""}
         SetScript = {
            Invoke-WebRequest -
            Uri "https://github.com/PowerShell/PowerShell/releases/download/v7.1.4/PowerShell-7.1.4-win-x86.msi"
               -OutFile C:\Temp\PowerShell-7.1.4-win-x86.msi
         }
         TestScript = {
            If(!(Test-Path C:\Temp\PowerShell-7.1.4-win-x86.msi)){return $false}
            else{return $true}
         }
      }
   }
}