Hello all,
I've written a script that will collect some info.
I've written it on a windows 8 client and tested it there also.
On the windows 8 client it functions just fine, I've now moved it to a windows 2008 R2 server where I needed to execute it from on a scheduled basis but there I'm seeing errors.
Here is the script:
------------------------------------------------------------
param (
[String]$User
,[String]$Password
,[String]$Hostname
)
#Load the VMWare PS Snapin
Add-PSSnapin VMware.VimAutomation.Core
#Connect to the VMWare server
Connect-VIServer -Server vcenter -User $user -Password $Password -WarningAction SilentlyContinue |Out-Null
#Script Execution
$VM = get-vm $Hostname
$networkadapters=get-vm $VM | get-networkadapter
$disks = $vm | get-harddisk
$OSDisk = $disks[0] | %{[string]$_.CapacityGB}
$VLAN = $networkadapters[0] | %{[string]$_.NetworkName}
$mac = $networkadapters[0] | %{[string]$_.Macaddress}
--------------------------------------------
When I run it on the windows 8 client than everything works as expected.
When I run this on the Windows 2008 R2 server, then I get the following error messages:
Unable to index into an object of type VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.FlatHardDiskImpl.
At C:\scripts\DiscoverScript.ps1:42 char:18
+ $OSDisk = $disks[ <<<< 0] | %{[string]$_.CapacityGB}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
Unable to index into an object of type VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.NetworkAdapterImpl.
At C:\scripts\DiscoverScript.ps1:43 char:26
+ $VLAN = $networkadapters[ <<<< 0] | %{[string]$_.NetworkName}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
Unable to index into an object of type VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.NetworkAdapterImpl.
At C:\scripts\DiscoverScript.ps1:44 char:25
+ $mac = $networkadapters[ <<<< 0] | %{[string]$_.Macaddress}
+ CategoryInfo : InvalidOperation: (0:Int32) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
It seems that on the Windows 2008 R2 server, I'm not allowed to reference the index of the array.
Is this because on windows 2008 R2 Powershell V2 is installed as opposed to Windows 8 where I have Powershell V3?
I'm running following version on both machines (W8 & 2008): VMware vSphere PowerCLI 5.1 Release 1 build 793510
Does anyone know why this is?
Is this because of the Powershell V2?
How can I make this work on Powershell v2? Should I just upgrade to v3?
Thanks!
Filip