Hi LucD,
I am interested with option 2. This is the version of your function used to register any vms that are not registered on the datastores specified which is working great!
function Register-VMX {
param($entityName = $null,$dsNames = $null,$template = $false)
function Get-Usage{
Write-Host "Parameters incorrect" -ForegroundColor red
Write-Host "Register-VMX -entityName -dsNames [,...]"
Write-Host "entityName : a cluster-, datacenter or ESX hostname"
Write-Host "dsNames : one or more datastorename names"
Write-Host "template : register guests ($false)or templates ($true) - default : $false"
}
if($entityName -ne $null -and $dsNames -ne $null){
Get-Usage
exit
}
if($dsNames -eq $null){
switch((Get-Inventory -Name $entityName).GetType().Name.Replace("Wrapper","")){
"Cluster"{
$dsNames = Get-Cluster -Name $entityName | Get-VMHost | Get-Datastore | where {$_.Type -eq "VMFS"} | % {$_.Name}
}
"Datacenter"{
$dsNames = Get-Datacenter -Name $entityName | Get-Datastore | where {$_.Type -eq "VMFS"} | % {$_.Name}
}
"VMHost"{
$dsNames = Get-VMHost -Name $entityName | Get-Datastore | where {$_.Type -eq "VMFS"} | % {$_.Name}
}
Default{
Get-Usage
exit
}
}
}
$dsNames = $dsNames | Sort-Object
$pattern = "*.vmx"
if($template){
$pattern = "*.vmtx"
}
foreach($dsName in $dsNames){
Write-Host "Checking " -NoNewline; Write-Host -ForegroundColor red -BackgroundColor yellow $dsName
$ds = Get-Datastore $dsName | Get-View
$dsBrowser = Get-View $ds.Browser
$dc = Get-View (Get-View $ds.Parent).Parent
$tgtfolder = Get-View $dc.VmFolder
$esx = Get-View $ds.Host[0].Key
$pool = Get-View (Get-View $esx.Parent).ResourcePool
$vms = @()
foreach($vmImpl in $ds.Vm){
$vm = Get-View $vmImpl
write-host -Fore Magenta $vm.Name
$vm
$vms += $vm.Config.Files.VmPathName
}
$datastorepath = "[" + $ds.Summary.Name + "]"
$searchspec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchspec.MatchPattern = $pattern
$taskMoRef = $dsBrowser.SearchDatastoreSubFolders_Task($datastorePath, $searchSpec)
$task = Get-View $taskMoRef
while ("running","queued" -contains $task.Info.State){
$task.UpdateViewData("Info.State")
}
$task.UpdateViewData("Info.Result")
foreach ($folder in $task.Info.Result){
$found = $FALSE
if($folder.file -ne $null){
foreach($vmx in $vms){
if(($folder.FolderPath + $folder.File[0].Path) -eq $vmx){
$found = $TRUE
}
}
if (-not $found){
$vmx = $folder.FolderPath + $folder.File[0].Path
if($template){
$params = @($vmx,$null,$true,$null,$esx.MoRef)
}
else{
$params = @($vmx,$null,$false,$pool.MoRef,$null)
}
$taskMoRef = $tgtfolder.GetType().GetMethod("RegisterVM_Task").Invoke($tgtfolder, $params)
Write-Host "`t" $vmx "registered"
}
}
}
Write-Host "Done"
}
}
This is where is the function is called to register the vm's on the datastores specified
#Prompt for datastore to search for vm's to register.
[console]::ForegroundColor="yellow"
[string]$RegDSname = read-host "Please enter the name(s) of the datastore to scan." `
"Note if entering multiple datastores, then seperate with a ,
E.g. Lun1,Lun2"
[console]::ResetColor()
#Convert input into array if contains comma
If ($RegDSname.Contains(","))
{
[system.array]$RegDSname = $RegDSname.Split(",")
}
#Get-datastore to validate input is real datastore
Get-datastore $RegDSname
$Datastores = Get-datastore $RegDSname
write-host -Fore green "Datastore entered is : " $RegDSname
# Register all vm's on the provided datastore using function
# Register-VMX -dsNames "datastore1","datastore2"
Register-VMX -dsNames $RegDSname
Where in the function is the best place to put the compare against the csv file?
This is the code that I am using to import the csv file
$Import = read-host "Enter file to be used as import"
$OutputTrim = $Import.Trim(".csv")
Start-Transcript -path ("D:\Migration\Logs\PostMigrationSession_Log_" + $OutputTrim + "_" + $strDate + ".txt")
Write-Host -ForegroundColor Cyan "Servers in import file:"
[System.Array]$MigrationCSV = Import-Csv ($WorkFolder + "Imports\PostMigration\" + $Import)
$MigrationCSV | select -unique vmName