VMware Cloud Community
jessem
Enthusiast
Enthusiast
Jump to solution

Script to add VMs to Inventory not working

After I run this script, it gives me an error

At C:\Add-VMXToInventory.ps1:68 char:1

+ {

+ ~

Missing closing '}' in statement block.

    + CategoryInfo          : ParserError: (:) [], ParseException

    + FullyQualifiedErrorId : MissingEndCurlyBrace

even though I thought the statement is closing with a '}'.  Can anyone help?

-----------------------------------------

#

# Traverses all folders of a data store and adds all VMX files to the inventory

#

# Example:

#

# .\Add-VMXToInventory.ps1 -DatastoreName My_Datastore -VMFolder VDI -vCenter vcentername -ESXHost host.domain.com

#

param

(

   [Parameter(Mandatory=$true)] [string] $DatastoreName, # Case sensitive

   [Parameter(Mandatory=$true)] [string] $VMFolder,

   [Parameter(Mandatory=$true)] [string] $vCenter,

   [Parameter(Mandatory=$true)] [string] $ESXHost

)

#

# General options

#

#Requires -Version 2

Set-StrictMode -Version 2

#

# Functions

#

# Loads snapins

function LoadSnapins([string[]] $snapins)

{

   $loaded = Get-PSSnapin -Name $snapins -ErrorAction SilentlyContinue | % {$_.Name}

   $registered = Get-pssnapin -Name $snapins -Registered -ErrorAction SilentlyContinue  | % {$_.Name}

   $notLoaded = $registered | ? {$loaded -notcontains $_}

   if ($notLoaded -ne $null)

   {

      foreach ($newlyLoaded in $notLoaded)

   {

         Add-PSSnapin $newlyLoaded

      }

   }

}

#

# Start of script

#

# Load snapins

LoadSnapins @("VMware.VimAutomation.Core")

# Avoid stupid questions

Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false

# Connect to vCenter

Connect-VIServer $vCenter

# Set up search for .VMX files in datastore

$ds = Get-Datastore -Name $DatastoreName | %{Get-View $_.Id}

$SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$SearchSpec.matchpattern = "*.vmx"

$dsBrowser = Get-View $ds.browser

$DatastorePath = "[" + $ds.Summary.Name + "]"

# Find all .VMX file paths in datastore, filtering out ones with .snapshot (useful for NetApp NFS)

$SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch ".snapshot"} | %{$_.FolderPath + ($_.File | select Path).Path}

#Register all .vmx files as VMs on the datastore

foreach($VMXFile in $SearchResult)

{

   New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -RunAsync

}

0 Kudos
1 Solution

Accepted Solutions
jessem
Enthusiast
Enthusiast
Jump to solution

Thanks LucD.  I double-checked it and made the biggest end-user error.  I didn't update the ps1 from where the l was actually running it from.  It was missing the '}' at the very end.  I was just editing the wrong file/location!  Doh!

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

The code looks ok, but can you add the file  Add-VMXToInventory.ps1 as an attachment?


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

jessem
Enthusiast
Enthusiast
Jump to solution

Thanks LucD.  I double-checked it and made the biggest end-user error.  I didn't update the ps1 from where the l was actually running it from.  It was missing the '}' at the very end.  I was just editing the wrong file/location!  Doh!

0 Kudos