我试图在‘m列表上运行命令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)
就像它增加了所有的符号..。失去他们的正确方法是什么?
发布于 2015-12-22 16:35:19
你需要扩大财产。否则Select不会删除对象:
$vmm = Get-VM –ComputerName (Get-ClusterNode –Cluster clustername) `
| Select-Object -ExpandProperty name要解释-ExpandProperty做了什么:
首先,-ExpandProperty的缺点是一次只能处理一个属性。
Select-Object通常将结果包装在另一个对象中,这样属性就会保留为属性。如果您说$x = Get-ChildItem C:\Windows | Select-Object Name,那么您将得到一个带有一个属性: Name的对象数组。
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注意标题?Name是对象的一个属性。
而且,带有它的类型的基本对象仍然在那里:
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一般情况下,这都很好。特别是因为我们通常需要对象的多个属性。
然而,有时候,并不是我们想要的。有时,我们需要一个与我们选择的属性类型相同的数组。当我们稍后使用它时,我们只想要该属性,而不需要其他任何东西,我们希望它与属性完全相同,而不是其他任何类型。
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注意没有标头,任何对Name属性的调用都失败了;不再有Name属性了。
而且,原始物体没有留下任何东西:
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中是如何做到的.我已经好多年没用它来记住了。
https://stackoverflow.com/questions/34420298
复制相似问题