如何在运行时获取所需状态配置的文件资源以确定它的 $DestinationPath?这似乎仅在生成 mof 文件时设置。
我下面的脚本显示了我正在尝试做的事情。我知道为什么它不起作用,我找不到如何使它起作用。
该脚本查看目标计算机上的 PSModulePath 变量并从变量中提取单个路径。我想将此路径传递给脚本中的文件资源,以在该路径中安装一些自定义 Powershell 模块。
我使用变量 $LocalInstallPath 但这不起作用,因为它是在创建 mof 文件时在 File 资源中设置的。尝试设置为环境变量,但同样的问题。
有没有办法做到这一点?
Configuration InstallCustomPowershellModules
{
param($MachineName)
Node $MachineName
{
$CustomModuleSource = '\\fileserver\modules\Module1'
Script GetLocalPath
{
SetScript = {
$FullPath = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
$SplitPath = $FullPath -split ";"
foreach ($ShortPath in $SplitPath)
{
$result = Select-String -Pattern 'system32' -InputObject $ShortPath
if($result -ne $null)
{
break
}
}
$LocalInstallPath = $ShortPath
}
TestScript = { $false }
GetScript = { $true }
}
File InstallCustomModule
{
Ensure = 'Present'
Type = 'Directory'
SourcePath = $CustomModuleSource
DestinationPath = $LocalInstallPath
Force = $true
Recurse = $true
}
}
}