I know this is a really old post but hopefully my reply will alert someone. Also, fair warning, I'm very bad with PowerCLI/Powershell/Scripting in general.
I'm trying to do the same thing as OP did, but with a different target string and across my entire vSphere environment. I'm not sure if I'm just doing something wrong or if the versions of PowerCLI/PowerShell are different enough to where some parameters/variables/cmdlets are different from the original posting.
The specific error(s) I'm running into are during the 'Copy-DatastoreItem' and 'Get-ChildItem' pieces, and both are presenting errors that my path to my datastore doesn't exist. It seems to be defaulting to a local mapped drive on my workstation, and then looking for the datastore there.
I modified the original script to target only a single VM for testing reasons, as well as a target folder of a VSAN datastore where the VM in question currently resides.
$vmName = "VM"
$tgtFolder = "datastore"
$tgtString = 'sound.virtualDev = "hdaudio"'
In the following error message, "X:\" is the mapped drive on my workstation.
Copy-DatastoreItem : The specified destination folder 'X:\datastore' does not exists. Use the Force parameter to force
operation.
At line:10 char:3
+ Copy-DatastoreItem -Item "DS:$vmxPath" -Destination $tgtFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], PathException
+ FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Cmdlets.Provider.Exceptions.PathException,VMware.VimAutomation.ViCore.Cmdl
ets.Commands.CopyDatastoreItem
Get-ChildItem : Cannot find path 'X:\d_tst_cl1_vsan\VM' because it does not exist.
At line:14 char:1
+ Get-ChildItem -Path $tgtFolder -Filter "*.vmx" |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (X:\datastore\VM:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Hopefully my explanation isn't too confusing. Thanks in advance for any help!