VMware Cloud Community
vmpwcliuser31
Contributor
Contributor
Jump to solution

Question regarding variables and its output

Hey all,

I'm trying to create a [large for me] script that will basically swap VMs around (when a production VM 'blows up' and a full VM restore happens, I want to run a script that grabs certain information from the production server and the restored copy of the server and swaps them out basically.  It's about 15 steps to do what we do manually in the vCenter GUI, but getting stuck at the beginning (and feeling silly).

 

Right now, I have the script looking at the production server and grabbing the information (Folder it's in, datastore it's on, VLAN it's on) and trying to output that information to verify that I have it right before proceeding with the script.

 

This part of the code looks like:

 

$vmnn = get-vm $srvr | Get-NetworkAdapter | Select-Object NetworkName
$vmds = get-vm $srvr | get-datastore | select Name
$vmfldr = get-vm $srvr | select Folder

Write-Host "Production server is in folder $vmfldr and on datastore $vmds and on VLAN $vmnn"

When I run it, it shows: 

Production server is in folder @{Folder=ABC} and on datastore @{Name=ClonesDatastore} and on VLAN @{NetworkName=733-ABC}

 

I just want the values to be shown and I'm assuming that it's showing it in an expression instead of string value?  If I don't use the Write-Host cmdlt and just do:

$vmfldr

For instance, it'll show: 

Folder
------
ABC

 

I'd like it to look like this:

Production server is in folder ABC and on datastore ClonesDatastore and on VLAN 733-ABC

 

Any help would be appreciated!

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are including objects in that Write-Host line.
What you see is PowerShell's way of displaying those objects.
You can specify a specific property, in these cases Name, of those objects.

Write-Host "Production server is in folder $($vmfldr.Name) and on datastore $($vmds.Name) and on VLAN $($vmnn.Name)"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

You are including objects in that Write-Host line.
What you see is PowerShell's way of displaying those objects.
You can specify a specific property, in these cases Name, of those objects.

Write-Host "Production server is in folder $($vmfldr.Name) and on datastore $($vmds.Name) and on VLAN $($vmnn.Name)"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

vmpwcliuser31
Contributor
Contributor
Jump to solution

Thanks again, LucD! 🙂

 

For my own reference, the code that worked for me is:

Write-Host "Production server is in folder $($vmfldr.Folder) and on datastore $($vmds.Name) and on VLAN $($vmnn.NetworkName)"

 

At first it didn't quite work for me but noticed why...

When I ran this command:

get-vm helpervm | Get-NetworkAdapter | Select-Object NetworkName

 

I'd get this result:

NetworkName
-----------
733-ABC

 

So I had to use the header of 'NetworkName' and not just "Name" after the variable.  🙂  

 

Hooray for learning!

Tags (1)
Reply
0 Kudos