Powershell 中自定义对象的范围
Scoping Custom Objects in Powershell
所以我有一个 powershell 问题,看起来应该很容易解决,但我自己却很难解决。
以下面的程序为例:
$MyFavoriteAnimals = @()
$Pets = "Cat","Dog","Fish","Bird"
$Names = "Jeb","Rex","Sam","Roger"
For ($i=0; $i -lt 4; $i++)
{
$NewAnimal = @{"Kind" = $Pets[$i]; "Name" = $Names[$i]}
$MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
}
$MyFavoriteAnimals | FT Kind, Name -AutoSize
这工作正常,并有以下输出:
Kind Name
---- ----
Cat Jeb
Dog Rex
Fish Sam
Bird Roger
...但是如果我尝试将其放入函数中并将 $MyFavoriteAnimals 范围作为全局变量,则 New-Object 命令将停止工作:
Function My-Favorite-Animals {
$Global:MyFavoriteAnimals = @()
$Pets = "Cat","Dog","Fish","Bird"
$Names = "Jeb","Rex","Sam","Roger"
For ($i=0; $i -lt 4; $i++)
{
$NewAnimal = @{"Kind" = $Pets[$i]; "Name" = $Names[$i]}
$MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
}
}
My-Favorite-Animals
$MyFavoriteAnimals | FT Kind, Name -AutoSize
...我收到以下错误:
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At line:13 char:9
+ $MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
指定:**$Global:**MyFavoriteAnimals +=,如:
Function My-Favorite-Animals {
$Global:MyFavoriteAnimals = @()
$Pets = "Cat","Dog","Fish","Bird"
$Names = "Jeb","Rex","Sam","Roger"
For ($i=0; $i -lt 4; $i++)
{
$NewAnimal = @{"Kind" = $Pets[$i]; "Name" = $Names[$i]}
$Global:MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
}
}
My-Favorite-Animals
$MyFavoriteAnimals | FT Kind, Name -AutoSize
所以我有一个 powershell 问题,看起来应该很容易解决,但我自己却很难解决。
以下面的程序为例:
$MyFavoriteAnimals = @()
$Pets = "Cat","Dog","Fish","Bird"
$Names = "Jeb","Rex","Sam","Roger"
For ($i=0; $i -lt 4; $i++)
{
$NewAnimal = @{"Kind" = $Pets[$i]; "Name" = $Names[$i]}
$MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
}
$MyFavoriteAnimals | FT Kind, Name -AutoSize
这工作正常,并有以下输出:
Kind Name
---- ----
Cat Jeb
Dog Rex
Fish Sam
Bird Roger
...但是如果我尝试将其放入函数中并将 $MyFavoriteAnimals 范围作为全局变量,则 New-Object 命令将停止工作:
Function My-Favorite-Animals {
$Global:MyFavoriteAnimals = @()
$Pets = "Cat","Dog","Fish","Bird"
$Names = "Jeb","Rex","Sam","Roger"
For ($i=0; $i -lt 4; $i++)
{
$NewAnimal = @{"Kind" = $Pets[$i]; "Name" = $Names[$i]}
$MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
}
}
My-Favorite-Animals
$MyFavoriteAnimals | FT Kind, Name -AutoSize
...我收到以下错误:
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
At line:13 char:9
+ $MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
指定:**$Global:**MyFavoriteAnimals +=,如:
Function My-Favorite-Animals {
$Global:MyFavoriteAnimals = @()
$Pets = "Cat","Dog","Fish","Bird"
$Names = "Jeb","Rex","Sam","Roger"
For ($i=0; $i -lt 4; $i++)
{
$NewAnimal = @{"Kind" = $Pets[$i]; "Name" = $Names[$i]}
$Global:MyFavoriteAnimals += New-Object pscustomobject -Property $NewAnimal
}
}
My-Favorite-Animals
$MyFavoriteAnimals | FT Kind, Name -AutoSize