如何在 PowerShell DSC 中解決相對路徑不受支援的問題?


當我們從線上或網站下載檔案併為其使用 “檔案” DscResource 時,通常會出現“相對路徑不受支援”錯誤。

在以下示例中,我們使用 DSC 將 PowerShell 7.1.4 版本從 GitHub 下載到本地計算機,結果出現了以下錯誤。

示例

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

要解決此錯誤,我們可以使用 “指令碼” 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

您還可以使用內建指令碼 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}
         }
      }
   }
}

更新時間:2021 年 9 月 1 日

503 次瀏覽

開啟您的職業

完成課程後取得認證

開始學習
廣告
© . All rights reserved.