VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get output in .txt file

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

$DSS = Get-Content "D:\DS\DS.txt"

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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

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

$DSS = Get-Content "D:\DS\DS.txt"

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

0 Kudos
vXav
Expert
Expert
Jump to solution

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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much  LucD, that worked Smiley Happy

0 Kudos