Hi,
I am unable to get the Datastore Name and Output of below command to a file
please help.
$AllVMs = @()
$DSS = Get-Content "D:\DS\DS.txt"
foreach ($DS in $DSS){
Get-Datastore $DS | Get-VM | select @{N='Datastore';E={$_.Parent_Name}}, @{N='VM_Name';E={$_.Name}}, @{N="Port_Group";E={@(Get-VirtualPortGroup -vm $_.Name)}}
}
$AllVMs | Out-File .\DSlog.txt -Append -Force
Try like this
$DSS = Get-Content "D:\DS\DS.txt"
$AllVMs = foreach ($DS in $DSS){
Get-Datastore $DS | Get-VM |
select @{N='Datastore';E={$DS}},
@{N='VM_Name';E={$_.Name}},
@{N="Port_Group";E={@(Get-VirtualPortGroup -vm $_.Name)}}
}
$AllVMs | Out-File .\DSlog.txt -Append -Force
But this one is more PS like
Get-Datastore -Name $DSS -PipelineVariable ds | Get-VM |
select @{N = 'Datastore'; E = { $ds.Name } },
@{N = 'VM_Name'; E = { $_.Name } },
@{N = "Port_Group"; E = { @(Get-VirtualPortGroup -vm $_.Name) } } |
Out-File .\DSlog.txt -Append -Force
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$DSS = Get-Content "D:\DS\DS.txt"
$AllVMs = foreach ($DS in $DSS){
Get-Datastore $DS | Get-VM |
select @{N='Datastore';E={$DS}},
@{N='VM_Name';E={$_.Name}},
@{N="Port_Group";E={@(Get-VirtualPortGroup -vm $_.Name)}}
}
$AllVMs | Out-File .\DSlog.txt -Append -Force
But this one is more PS like
Get-Datastore -Name $DSS -PipelineVariable ds | Get-VM |
select @{N = 'Datastore'; E = { $ds.Name } },
@{N = 'VM_Name'; E = { $_.Name } },
@{N = "Port_Group"; E = { @(Get-VirtualPortGroup -vm $_.Name) } } |
Out-File .\DSlog.txt -Append -Force
---------------------------------------------------------------------------------------------------------
Was it helpful? Let us know by completing this short survey here.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You $AllVMs variable is an empty array. I think you wanted to populate it in the loop but forgot it?
$AllVMs = @()
$DSS = Get-Content "D:\DS\DS.txt"
foreach ($DS in $DSS){
$AllVMs += Get-Datastore $DS | Get-VM | select @{N='Datastore';E={$_.Parent_Name}}, @{N='VM_Name';E={$_.Name}}, @{N="Port_Group";E={@(Get-VirtualPortGroup -vm $_.Name)}}
}
$AllVMs | Out-File .\DSlog.txt -Append -Force
Thank you very much LucD, that worked