Could not find item C:\ProgramData

Hello,
I am having an issue while executing a script from PSU Automation.
I have the below error when it is run from PSU (repository folder) whereas when I run from a local folder it works fine. Any help would be appreciated.

New-VM : 2/20/2023 12:42:41 PM	New-VM		**Could not find item C:\ProgramData.	**
**At C:\ProgramData\UniversalAutomation\Repository\TESTING.ps1:57 char:5**
+     New-VM -Name $VMname
+     ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [New-VM], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

Edit :

I did some tests and found that the script from PSU works when I ask to create the VM directly to a host (-VMhost $hosts) and not to the cluster (-ResourcePool $mycluster).
Am I missing something here?

Without seeing the code, it’s hard to diagnose.

I’m sorry for that, this is the code to create VMs directly in the cluster which shows the above error.

#$vcenter = "vcenter"
#$vcentercred = $Secret:MyNewSecret
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

#Connect-VIServer -Server $vcenter -Credential $vcentercred

Connect-VIServer $vcenter

$vmlist = Import-CSV E:\vmlist.csv

foreach ($item in $vmlist) {
 
    # Variables
    #$vcenter = $item.vcenter
    $myCluster = $item.myCluster                                
    $myTemplate = $item.myTemplate
    $myDatastore = $item.myDatastore
    $custspec =$item.custspec                
    $Hostname = $item.Hostname                                         
    $ipaddr = $item.ipaddr                                          
    $subnet = $item.subnet                                             
    $gateway = $item.gateway 
    $pdns = $item.pdns
    $sdns = $item.sdns
    $NetworkName = $item.NetworkName                                     
    $CPU = $item.cpu
    $memory = $item.memory
    $DiskCapacity = $item.DiskCapacity
    $DiskStorageFormat = "Thick" 
  
    # Get the Specification and set the Nic Mapping
    If ($Varable) {
        Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns
    } else {
        Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns
    }
 
    # Clone the template with the adjusted Customization Specification and create VMs
    New-VM -Name $Hostname -Template $myTemplate -ResourcePool $myCluster -Datastore $myDatastore -DiskStorageFormat $DiskStorageFormat -OSCustomizationSpec $custspec -Confirm:$false
    Set-VM -VM $Hostname -NumCpu $CPU -MemoryGB $Memory -Confirm:$false | Out-Null
    New-HardDisk -CapacityGB $DiskCapacity -vm $Hostname -Datastore $myDatastore
    $vm = Get-VM $Hostname
    $Network_Adapter = Get-NetworkAdapter -VM $vm
    Set-NetworkAdapter -NetworkAdapter $Network_Adapter -NetworkName $NetworkName -Type "VMXNET3" -Confirm:$false
    Start-VM -VM $Hostname -RunAsync

}

When I replace -Resourcepool $mycluster to -VMhost $myhosts it works perfectly.

Can you give an example of a line in the CSV file? Just replace any real names/IP’s with a fake one.

You can try adding a line like this…


"New-VM -Name $Hostname -Template $myTemplate -ResourcePool $myCluster -Datastore $myDatastore -DiskStorageFormat $DiskStorageFormat -OSCustomizationSpec $custspec -Confirm:$false"

Rationale here is that you can manually check that all the variables look correct.

Here is an example

By adding the below line

"New-VM -Name $Hostname -Template $myTemplate -ResourcePool $myCluster -Datastore $myDatastore -DiskStorageFormat $DiskStorageFormat -OSCustomizationSpec $custspec -Confirm:$false"

I get this result

New-VM -Name TESTVM01 -Template Win2016_Std -ResourcePool  labcluster01 -Datastore ESXLAB -DiskStorageFormat Thick -OSCustomizationSpec Windows Server 2016 -Confirm:False
2 Likes

It looks like “labcluster01” is being passed as " labcluster01". Double check your file?

I tried to run the below simple code without csv file but same error…

Connect-VIServer -Server $vcenter -Credential $vcentercred

New-VM -Name TEST -ResourcePool labcluster01

[error] 2/23/2023 10:28:38 AM New-VM Could not find item C:\ProgramData.

The below code works fine

Connect-VIServer -Server $vcenter -Credential $vcentercred

New-VM -Name TEST -VMHost labhost01

It is weird.

1 Like

This is similar, in terms of the error at least. Looks like it may be sensitive to the current directory, but doesn’t handle folder paths with a space in the name correctly?

Folder paths and files don’t have space in the names.
Even when I try the code as posted this morning without importing a csv file I have the error :frowning:

It’s not a path you are putting in, it’s the module itself.

Look at the above link, they are getting the same error and fixed it by changing the working directory.

What a miracle
This works !!! :smiling_face_with_tear: :smiling_face_with_tear: :smiling_face_with_tear:

Connect-VIServer -Server $vcenter -Credential $vcentercred
cd 'C:\temp'
New-VM -Name TEST -ResourcePool labcluster01

Many Thanks @Jori