Powershell 在生成DSC配置时使用变量传递参数
我正在从Powershell执行Powershell DSC脚本。下面是代码片段Powershell 在生成DSC配置时使用变量传递参数,powershell,azure-devops,powershell-dsc,Powershell,Azure Devops,Powershell Dsc,我正在从Powershell执行Powershell DSC脚本。下面是代码片段 Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock { param ($driveformat) cd c:/provisioning Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
param ($driveformat)
cd c:/provisioning
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
Install-Module -Name PSDscResources -Force
Install-Module -Name xStorage -Force
. .\DiskSetup.ps1
disksconfig -outputpath C:\DataDiskSetting -driveFormat $driveFormat
Start-DscConfiguration -Path C:\DataDiskSetting -Wait -Force -Verbose
} -ArgumentList ($driveformat)
生成配置时,我希望将driveformat作为变量“$driveformat”传递,而不是像“NTFS”这样的硬编码。不知何故,$driveformat的价值是不存在的,我们不知道如何解决这个问题。您可以在脚本中添加一个命名的参数
$driveformat
。请参见以下示例:
Param([String]$driveformat)
Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
param ($driveformat)
...
} -ArgumentList ($driveformat)
然后在from管道中,在参数字段中添加-driveformat“NTFS”
。请参见下面的屏幕截图:(我定义了一个管道变量driveformat
来保存值“NTFS”)
或者,您可以在脚本中添加参数(例如,$driveformat=$args[0]
)。见下文:
$driveformat = $args[0]
Invoke-Command -ComputerName $public_ip_address -Credential $UserCredential -ScriptBlock {
param ($driveformat)
...
} -ArgumentList ($driveformat)
然后,您可以直接在powershell任务的参数字段中传递变量(“NTFS”):
您没有显示定义的位置,
$driveformat
它作为参数从PS脚本中的管道中传递。好的,我们如何帮助您确定它为什么为空?值是从管道中传递的,例如“NTFS”