问题 - 通用描述
在about_pipelines(help pipeline)的帮助中可以看到,powershell在管道¹下发送一个对象.因此Get-Process -Name notepad | Stop-Process,在管道下方发送一个进程.
假设我们有第三方CmdLet(Do-SomeStuff)无法以任何方式修改或更改.如果传递一个字符串数组或传递一个字符串对象,则Do-SomeStuff执行不同的操作.
DO-SomeStuff仅仅是一个例子,它可以被取代ForEach-Object,Select-Object,Write-Host(或任何其它小命令接受管道输入)
Do-SomeStuff将在此示例中一次处理数组中的各个项目.
$theArray = @("A", "B", "C")
$theArray | Do-SomeStuff
Run Code Online (Sandbox Code Playgroud)
如果我们想将完整数组作为一个对象发送到Do-SomeStuff,可能会尝试这样的事情
@($theArray) | Do-SomeStuff
Run Code Online (Sandbox Code Playgroud)
但由于PowerShell"忽略"了新的单项数组,因此它不会产生预期的结果.
那么,你如何"强制" $theArray作为单个数组对象传递给管道而不是当时的内容项?
问题 - 实际示例
如下所示,Write-Host如果传递一个数组或者它一次传递一个数组中的单个项,则输出是不同的.
PS C:\> $theArray = @("A", "B", "C")
PS C:\> Write-Host $theArray
A B C
PS C:\> $theArray | foreach{Write-Host $_}
A
B
C
PS C:\> @($theArray) | foreach{Write-Host $_}
A
B …Run Code Online (Sandbox Code Playgroud) 上下文
/// - Azure
/// - Azure Resource Management
/// https://msdn.microsoft.com/en-us/library/azure/dn578292.aspx
///
/// - Azure Resource Manager Template Language
/// https://msdn.microsoft.com/en-us/library/azure/dn835138.aspx
///
/// - Azure Resource Manager Template Language functions and expressions
/// - Azure Resource Manager Template Language function:
/// resourceId('resourceNamespace/resourceType','resourceName')
///
/// - Powershell
/// - Azure PowerShell
/// - Azure PowerShell Resource Manager Mode (Switch-AzureMode AzureResourceManager)
/// - Azure PowerShell CmdLet: New-AzureResourceGroup
///
Run Code Online (Sandbox Code Playgroud)
模板中的这一行(请参阅下面的完整模板)
"sourceDatabaseId": "[resourceId('Microsoft.Sql/servers/databases', 'TestDB')]"
运行PowerShell New-AzureResourceGroup CmdLet 时出现此错误:
PS … powershell json azure azure-management-api azure-resource-manager