powershell 输出参数 (@{Name=name}:String)
powershell outputs argument with (@{Name=name}:String)
我正在尝试 运行 在虚拟机列表
上执行命令 Get-VMNetworkAdapter
我正在使用以下命令获取列表:
Get-VM –ComputerName (Get-ClusterNode –Cluster clustername)|select name
它看起来不错,当我使用
$vmm=Get-VM –ComputerName (Get-ClusterNode –Cluster clustername)|select name
foreach ($item in $vmm)
{Get-VMNetworkAdapter -VMName $item}
它给了我例外
nvalidArgument: (@{Name=vmname}:String)
喜欢它添加所有这些符号..
失去它们的正确方法是什么?
您需要扩展 属性。 Select 不会删除 object 否则:
$vmm = Get-VM –ComputerName (Get-ClusterNode –Cluster clustername) `
| Select-Object -ExpandProperty name
解释 -ExpandProperty
的作用:
首先,-ExpandProperty
的缺点是一次只能对一个 属性进行。
Select-Object
通常将结果包装在另一个 object 中,以便属性保持属性。如果你说 $x = Get-ChildItem C:\Windows | Select-Object Name
,那么你会得到一个 object 数组,其中有一个 属性: 名称。
PS C:\> $x = Get-ChildItem C:\Windows | Select-Object Name
PS C:\> $x
Name
----
45235788142C44BE8A4DDDE9A84492E5.TMP
8A809006C25A4A3A9DAB94659BCDB107.TMP
.
.
.
PS C:\> $x[0].Name
45235788142C44BE8A4DDDE9A84492E5.TMP
PS C:\> $x[0].GetType().FullName
System.Management.Automation.PSCustomObject
注意到 header 了吗? Name
是 object 的 属性。
此外,基础 object 及其类型仍然是 种 那里:
PS C:\> $x | Get-Member
TypeName: Selected.System.IO.DirectoryInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Name NoteProperty string Name=45235788142C44BE8A4DDDE9A84492E5.TMP
TypeName: Selected.System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Name NoteProperty string Name=bfsvc.exe
通常情况下,这很好。特别是因为我们通常需要 object.
的多个属性
然而,有时并不是我们想要的。有时,我们想要一个与我们选择的 属性 类型相同的数组。当我们稍后使用它时,我们想要 只是 属性 而不是其他任何东西,我们希望它与 类型 完全相同 =64=] 仅此而已。
PS C:\> $y = Get-ChildItem C:\Windows | Select-Object -ExpandProperty Name
PS C:\> $y
45235788142C44BE8A4DDDE9A84492E5.TMP
8A809006C25A4A3A9DAB94659BCDB107.TMP
.
.
.
PS C:\> $y[0].Name
PS C:\> $y[0]
45235788142C44BE8A4DDDE9A84492E5.TMP
PS C:\> $y.GetType().FullName
System.Object[]
PS C:\> $y[0].GetType().FullName
System.String
请注意没有 header,任何对名称 属性 的调用都会失败;没有名字 属性 了。
而且,原来的object没有任何遗留:
PS C:\> $y | Get-Member
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
.
.
.
.
基本上,这相当于这样做:
$z = Get-ChildItem C:\Windows | ForEach-Object { $_.Name }
我认为这就是您必须在 PowerShell v1.0 或 v2.0 中执行此操作的方式...我用过它已经太多年了,记不清了。
我正在尝试 运行 在虚拟机列表
上执行命令Get-VMNetworkAdapter
我正在使用以下命令获取列表:
Get-VM –ComputerName (Get-ClusterNode –Cluster clustername)|select name
它看起来不错,当我使用
$vmm=Get-VM –ComputerName (Get-ClusterNode –Cluster clustername)|select name
foreach ($item in $vmm)
{Get-VMNetworkAdapter -VMName $item}
它给了我例外
nvalidArgument: (@{Name=vmname}:String)
喜欢它添加所有这些符号.. 失去它们的正确方法是什么?
您需要扩展 属性。 Select 不会删除 object 否则:
$vmm = Get-VM –ComputerName (Get-ClusterNode –Cluster clustername) `
| Select-Object -ExpandProperty name
解释 -ExpandProperty
的作用:
首先,-ExpandProperty
的缺点是一次只能对一个 属性进行。
Select-Object
通常将结果包装在另一个 object 中,以便属性保持属性。如果你说 $x = Get-ChildItem C:\Windows | Select-Object Name
,那么你会得到一个 object 数组,其中有一个 属性: 名称。
PS C:\> $x = Get-ChildItem C:\Windows | Select-Object Name
PS C:\> $x
Name
----
45235788142C44BE8A4DDDE9A84492E5.TMP
8A809006C25A4A3A9DAB94659BCDB107.TMP
.
.
.
PS C:\> $x[0].Name
45235788142C44BE8A4DDDE9A84492E5.TMP
PS C:\> $x[0].GetType().FullName
System.Management.Automation.PSCustomObject
注意到 header 了吗? Name
是 object 的 属性。
此外,基础 object 及其类型仍然是 种 那里:
PS C:\> $x | Get-Member
TypeName: Selected.System.IO.DirectoryInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Name NoteProperty string Name=45235788142C44BE8A4DDDE9A84492E5.TMP
TypeName: Selected.System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Name NoteProperty string Name=bfsvc.exe
通常情况下,这很好。特别是因为我们通常需要 object.
的多个属性然而,有时并不是我们想要的。有时,我们想要一个与我们选择的 属性 类型相同的数组。当我们稍后使用它时,我们想要 只是 属性 而不是其他任何东西,我们希望它与 类型 完全相同 =64=] 仅此而已。
PS C:\> $y = Get-ChildItem C:\Windows | Select-Object -ExpandProperty Name
PS C:\> $y
45235788142C44BE8A4DDDE9A84492E5.TMP
8A809006C25A4A3A9DAB94659BCDB107.TMP
.
.
.
PS C:\> $y[0].Name
PS C:\> $y[0]
45235788142C44BE8A4DDDE9A84492E5.TMP
PS C:\> $y.GetType().FullName
System.Object[]
PS C:\> $y[0].GetType().FullName
System.String
请注意没有 header,任何对名称 属性 的调用都会失败;没有名字 属性 了。
而且,原来的object没有任何遗留:
PS C:\> $y | Get-Member
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone(), System.Object ICloneable.Clone()
.
.
.
.
基本上,这相当于这样做:
$z = Get-ChildItem C:\Windows | ForEach-Object { $_.Name }
我认为这就是您必须在 PowerShell v1.0 或 v2.0 中执行此操作的方式...我用过它已经太多年了,记不清了。