对于另一个脚本,我需要一个我的VM列表。 我使用以下脚本:
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vmXX
Connect-VIServer vmXI
$vms = get-vm | where { ($_.powerstate -eq "poweredon") }
$rows = @()
Foreach ($VM in $vms) {
$View = $VM | get-view
$Config = $View.config
if ($Config.Template) { continue }
$row = New-Object -TypeName PSObject
$row | Add-Member -MemberType NoteProperty -Name $Config.Name
$rows += $row
}
现在将名称输出为VM:VMXX - Build Server 我已经尝试了-replace和-trim,但我找不到一种方法来获得一个只包含VM名称的干净清单,而不是别的。
答案 0 :(得分:1)
我认为您遗失的主要内容是-Name "" -Value ""
行中的Add-Member
格式。
像这样。
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer @("svc01","svc02") -WarningAction 0 -Force
$vms = Get-VM | Where-Object { ($_.powerstate -eq "PoweredOn") }
$rows = @()
ForEach ($vm in $vms) {
if ($($vm | Get-View).config.template) { continue }
$row = New-Object -TypeName PSObject
$row | Add-Member -MemberType NoteProperty -Name "Name" -Value $vm.Name
$rows += $row
}
$rows
答案 1 :(得分:0)
我将我的代码更改为以下似乎可以解决的问题:
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vmXX
$outfile1 = "d:\beheer\scripts\XX.csv"
$FileExists = Test-Path $outfile1
If ($FileExists -eq $True) { del $outfile1 }
foreach ($vmachine in Get-VM | where {$_.powerstate -eq "poweredon"} |where { $_.ExtensionData.Config.ManagedBy.extensionKey -notmatch "com.vmware.vcDr" }| sort )
{
$list = $vmachine.Name
$VM = $list -split (" ")
$vm1 = $VM[0] | Out-File $outfile1 -append
}
Connect-VIServer vm65
# PowerShell Checks If a File Exists
$outfile1 = "d:\beheer\scripts\CommVault\VMlist.csv"
foreach ($vmachine in Get-VM | where {$_.powerstate -eq "poweredon"} |where { $_.ExtensionData.Config.ManagedBy.extensionKey -notmatch "com.vmware.vcDr" } | sort )
{
$list = $vmachine.Name
$VM = $list -split (" ")
$vm1 = $VM[0] | Out-File $outfile1 -append
}