Hi, same project as I was working on before, but a different question.
I'm creating a menu driven interface for running common commands against multiple machines. The script reads the machine names from a text file using Get-Content, and then uses Get-VM to get the objects on which it then operates.
As the Get-VM cmdlet takes a while to run, I'm looking for a way to "cache" these objects if they have previously been retrieved, rather then getting them each time.
So, it would need to:
Check to see if the objects have already been retrieved; if not it should get the VM objects, cache them, then carry on with it's operation. It would also need to check that the text-file input hasn't been modified since the cache was created.
I've attached the WIP script if you fancy a look. Any other suggestions for improving it would be welcomed.
You could use the Last write time of the input file to check if it has been changed.
To get this time you can use
$strLastWriteTime = (Get-Item $strVMList).LastWriteTime
The VM objects cna be stored in an array.
Only read the VM objects again if you connect to another VC server or if the TXT file has been changed.
$arrVMList =Get-Content $strVMList $arrVMobj = Get-VM $arrVMList
I tried to implement this in your script.
I used some global variables to store the required information.
Have a look.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You could use the Last write time of the input file to check if it has been changed.
To get this time you can use
$strLastWriteTime = (Get-Item $strVMList).LastWriteTime
The VM objects cna be stored in an array.
Only read the VM objects again if you connect to another VC server or if the TXT file has been changed.
$arrVMList =Get-Content $strVMList $arrVMobj = Get-VM $arrVMList
I tried to implement this in your script.
I used some global variables to store the required information.
Have a look.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You may also want to look at using "Get-View -ViewType VirtualMachine" to replace Get-VM. The end result is not the same, the properties are all different, but it may work well for you and will be faster.
[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org
Thanks for the help.
LucD - I couldn't see any changes in the script you provided, I think you may have attached the original version by mistake. However, I have followed your advice about the array and using the file date, and I think I've got something that works (see below).
Halr9000 - Thanks, I will take a look at this.
I've attached my latest version (sorry, it's still a WIP, so there are no comments, and it's badly formatted). I had a bit of trouble with variables - I didn't realise that setting variables in functions, does not set them throughout the script unless you set the scope (I'm still new to this).
While it still takes a while to store the VM objects, it's still quicker than changing the hard drives on ~20 machines from Persistent to Non-Persistent and back again!
Yes, you are right.
This should be the one I intended to be attached.
Update: it looks like the forum SW doesn't want you attach a new file with the same name.
Then it always displays the original one.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference