如何在 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}
}
}
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP