需要帮助获取每个虚拟机的名称、状态、状态、操作系统版本、节点名称的 IP 地址列表
Need help in getting list of IP address with Name, Status, State, OSversion, Nodename against each VM
我需要脚本方面的帮助。下面是我从 Hyper v 节点中提取数据的脚本。我还想要一些 VM 属性在输出中。但我越来越空白。有人可以帮助理解什么是错的以及如何解决这个问题。 $Vms 中没有我想要的所有属性。如何组合 $VirtualM 和 $VMs 的属性并获得输出。
$Nodes = Get-Content -Path "C:\Nodes.txt"
foreach($Node in $Nodes)
{
$VMs = Get-VM | Get-VMNetworkAdapter | Select-Object -Property VMName, IPAddresses
$VirtualM = Get-VM | Select Name, Status, State
$Output = @()
foreach($VM in $VMs)
{
$results = [ordered]@{
'NodeName' = $Node;
'VMName' = $VM.VMName;
'IPaddress' = $VM.IPAddresses[0];
}
$Output += New-Object -TypeName PSObject -Property $results
$Output += New-Object -TypeName PSObject -Property $VirtualM
}
} Write-Output $Output | Select-Object -Property NodeName, VMName, IPAddress, State, Status
Output is as below
=========================
Output Comes Like Below
NodeName : ABC
VMName : s1
IPaddress : 192.168.1.5
State :
Status :
NodeName : ABC
VMName : s2
IPaddress : 192.168.1.6
State :
Status :
NodeName : ABC
VMName : s3
IPaddress : 192.168.1.7
State :
Status :
NodeName : DEF
VMName : D1
IPaddress : 192.168.1.9
State :
Status :
NodeName : DEF
VMName : D2
IPaddress : 192.168.1.10
State :
Status :
使用多步骤管道时,中间属性不再可用。
最好使用 ForEach
提供单独的变量名,就像您对 $Node.
所做的那样
这个未经测试的脚本:
## Q:\Test18\SO_52459730.ps1
$Nodes = @('ABC')
$Result = ForEach($Node in $Nodes) {
ForEach ($VM in (Get-VM -ComputerName $Node)){
[PSCustomObject]@{
NodeName = $Node
VMName = $VM.VMName
IPaddress = ($VM | Get-VMNetworkAdapter).IPAddresses[0]
State = $VM.State
Status = $VM.Status
}
}
}
$Result | Format-Table -Auto
#$Result | Out-Gridview
#$Result | Export-Csv All-VMs.csv -NoTypeInformation
应该return这样的输出:
NodeName VMName IPaddress State Status
-------- ------ --------- ----- ------
ABC s1 192.168.1.5 Running Operating normally
ABC s2 192.168.1.6 Running Operating normally
ABC s3 192.168.1.7 Running Operating normally
更新
添加一个 try{}catch{}
以查看对象失败的位置(如果失败)。
回到您的问题,您可以将来自不同变量的多个属性添加到一个对象中 (如下面的示例 $VM.Name
、 $VM.Name
和 $Node
).
在最终结果之前选择是不好的做法。您也不需要 运行 相同的命令两次,您可以“利用”您已有的 运行。如果您正在制作一个对象,您可以将来自不同变量的所有集体输入放入其中,然后您可以在其中指定您需要的内容。您还在每 $Node
之后重置 $Output
,您希望将数组放在外面并用对象抽取它。我从来没有使用过 VM cmdlet,所以我只是重建你的脚本而不测试它。
$Output = @()
foreach($Node in $Nodes) {
$VMs = Get-VM
foreach($VM in $VMs) {
$VMName = Get-VMNetworkAdapter -VMName $VM.Name
try{
$CombineObject= New-Object -type PSObject -Property @{
NodeName = $Node ;
VMName = $VMName.VMName ;
IPaddress = $VMName.IPAddresses ;
Name = $VM.Name ;
Status = $VM.Status ;
State = $VM.State
}
}catch{
Write-Output "Error occure with either: $Node or $($VM.Name) or $($VMName.VMName)"
}
$Output += $CombineObject
}
}
#Display the array of objects
$Output
由于这没有经过测试,您应该分别测试这两个 cmdlet 以确保您获得正确的 input/output
我需要脚本方面的帮助。下面是我从 Hyper v 节点中提取数据的脚本。我还想要一些 VM 属性在输出中。但我越来越空白。有人可以帮助理解什么是错的以及如何解决这个问题。 $Vms 中没有我想要的所有属性。如何组合 $VirtualM 和 $VMs 的属性并获得输出。
$Nodes = Get-Content -Path "C:\Nodes.txt"
foreach($Node in $Nodes)
{
$VMs = Get-VM | Get-VMNetworkAdapter | Select-Object -Property VMName, IPAddresses
$VirtualM = Get-VM | Select Name, Status, State
$Output = @()
foreach($VM in $VMs)
{
$results = [ordered]@{
'NodeName' = $Node;
'VMName' = $VM.VMName;
'IPaddress' = $VM.IPAddresses[0];
}
$Output += New-Object -TypeName PSObject -Property $results
$Output += New-Object -TypeName PSObject -Property $VirtualM
}
} Write-Output $Output | Select-Object -Property NodeName, VMName, IPAddress, State, Status
Output is as below
=========================
Output Comes Like Below
NodeName : ABC
VMName : s1
IPaddress : 192.168.1.5
State :
Status :
NodeName : ABC
VMName : s2
IPaddress : 192.168.1.6
State :
Status :
NodeName : ABC
VMName : s3
IPaddress : 192.168.1.7
State :
Status :
NodeName : DEF
VMName : D1
IPaddress : 192.168.1.9
State :
Status :
NodeName : DEF
VMName : D2
IPaddress : 192.168.1.10
State :
Status :
使用多步骤管道时,中间属性不再可用。
最好使用 ForEach
提供单独的变量名,就像您对 $Node.
这个未经测试的脚本:
## Q:\Test18\SO_52459730.ps1
$Nodes = @('ABC')
$Result = ForEach($Node in $Nodes) {
ForEach ($VM in (Get-VM -ComputerName $Node)){
[PSCustomObject]@{
NodeName = $Node
VMName = $VM.VMName
IPaddress = ($VM | Get-VMNetworkAdapter).IPAddresses[0]
State = $VM.State
Status = $VM.Status
}
}
}
$Result | Format-Table -Auto
#$Result | Out-Gridview
#$Result | Export-Csv All-VMs.csv -NoTypeInformation
应该return这样的输出:
NodeName VMName IPaddress State Status
-------- ------ --------- ----- ------
ABC s1 192.168.1.5 Running Operating normally
ABC s2 192.168.1.6 Running Operating normally
ABC s3 192.168.1.7 Running Operating normally
更新
添加一个 try{}catch{}
以查看对象失败的位置(如果失败)。
回到您的问题,您可以将来自不同变量的多个属性添加到一个对象中 (如下面的示例 $VM.Name
、 $VM.Name
和 $Node
).
在最终结果之前选择是不好的做法。您也不需要 运行 相同的命令两次,您可以“利用”您已有的 运行。如果您正在制作一个对象,您可以将来自不同变量的所有集体输入放入其中,然后您可以在其中指定您需要的内容。您还在每 $Node
之后重置 $Output
,您希望将数组放在外面并用对象抽取它。我从来没有使用过 VM cmdlet,所以我只是重建你的脚本而不测试它。
$Output = @()
foreach($Node in $Nodes) {
$VMs = Get-VM
foreach($VM in $VMs) {
$VMName = Get-VMNetworkAdapter -VMName $VM.Name
try{
$CombineObject= New-Object -type PSObject -Property @{
NodeName = $Node ;
VMName = $VMName.VMName ;
IPaddress = $VMName.IPAddresses ;
Name = $VM.Name ;
Status = $VM.Status ;
State = $VM.State
}
}catch{
Write-Output "Error occure with either: $Node or $($VM.Name) or $($VMName.VMName)"
}
$Output += $CombineObject
}
}
#Display the array of objects
$Output
由于这没有经过测试,您应该分别测试这两个 cmdlet 以确保您获得正确的 input/output