Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 在生成DSC配置时使用变量传递参数_Powershell_Azure Devops_Powershell Dsc - Fatal编程技术网

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

我正在从Powershell执行Powershell DSC脚本。下面是代码片段

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”