Hi,
I am fairly new to the powershell scripting and even newer to the vSphere CLI. I have no problem reading and understanding scripts, but making one is an whole other story. Anyway, I am looking for help with creating a script that will check all the vm's on all the ESX Hosts, if they have a snapshot and if they do echo the VM name on screen.
This is what I have so far but I am stuck and probably on the wrong track. Any help/suggestions are more than welcome!
$user = "<removed>"
$passw = "<removed>"
$VIServers = "<IP ESXi Host>", "<IP ESX Host1>", "<IP ESX Host2>", "<IP ESX Host3>", "<IP ESX Host4>", "<IP ESX Host5>", "<IP ESX Host6>"
foreach ($VIServer in $ViServers) {
Connect-VIServer -Server $VIServer -user $user -Password $passw
$vms = get-vmhost $VIServer | get-vm
foreach ($vm in $vms ) {
<code which checks to see if there is a snapshot and echo's the VM name on screen. Clueless>
}
}
}
In forward thanks!
Regards,
Erik
Why directly to the ESX:es? .. Connect to vCenter instead and then it´s very easy..
Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created
-Kenth
Why directly to the ESX:es? .. Connect to vCenter instead and then it´s very easy..
Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created
-Kenth
Or if you want to solve it without vCenter..
$strEsx = @("s-esxt0100.int.lio.se","s-esxt0200.int.lio.se")
$strEsx | foreach-object {connect-viserver $_
Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created}
Hope it gets you going a bit .. 😃
- Kenth
It is recommended to connect to vCenter if you have that available.
In case you don't have vCenter, you need to connect to all ESX hosts directly.
You can do this with one call to the Connect-VIServer cmdlet however, as I've demonstrated in the code below:
$user = "<removed>" $passw = "<removed>" $VIServers = "<IP ESXi Host>", "<IP ESX Host1>", "<IP ESX Host2>", "<IP ESX Host3>", "<IP ESX Host4>", "<IP ESX Host5>", "<IP ESX Host6>" Set-PowerCLIConfiguration -DefaultVIServerMode "Multiple" -Confirm:$false Connect-VIServer $VIServers -user $user -Password $passw Get-VM | Get-Snapshot | Select vm,name,description,powerstate,created,sizeMB,@{N="DaysOld";E={((Get-date)-$_.created).Days}} Disconnect-VIServer $VIServers -Confirm:$false
Arnim
I think you can make it a bit faster by doing it like this
Get-Snapshot -VM (Get-VM) | Select VM,Name,Description,Created | Sort Created
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You are probably right! 😃
First of all: Thanks guys! All your input helped me a lot!
There is one weird thing though. If I run this "Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created" from the script the onscreen output is different from when I run the exact same command in the CLI directly
Output when ran from script
VM : SVR-AS-038
Name : script test
Description :
Created : 12/30/2010 11:44:16 AM
VM : SVR-IS-032
Name : script test 2
Description :
Created : 12/30/2010 2:57:53 PM
Output when ran from CLI directly
VM Name Description Created
-- ---- ----------- -------
SVR-AS-038 script test 12/30/2010 11:44...
SVR-IS-032 script test 2 12/30/2010 2:57:...
I prefer the latter. Is this possible when using a script?
Sure, pipe the output to a Format-Table cmdlet (alias ft)
Get-VM | Get-Snapshot | Select VM,Name,Description,Created | Sort Created | ft -Autosize
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
or add pipe "Out-Null" after the connection to vCenters ..
$strEsx | foreach-object {connect-viserver $_ |Out-Null
-Kenth
LucD you are scaringly quick! You almost replied before I even asked the question! Loving it!
But it doesn't work I get this error:
out-lineoutput : The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-table" command which is conflicting with the default formatting. + CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
Try my thingy ..
pipe Out-Null in the connection string
ie..
Connect-viserver MYSERVER |Out-Null
-Kenth
Add
| Format-Table -AutoSize
To the end of the code.
Arnim
@gekko (you're awesome too )
Sorry, I was busy testing the solution provided by LucD when you replied. Did not see your reply until I submitted mine. Nevertheless, your solution does work! Excellent!
@Armin
Same error as the solution provided by LucD, not that strange as it is the same solution :smileysilly:
And then there is another challenge. Should I create a new thread or just post it anyway?
I want to schedule another VSphere PowerCLI script, but that doesn't work because I first have to start/load the vSphere PowerCLI before I can run this script (This script checks every VM if there is a CD image mounted, if so the image is dismounted and the CD is disconnected. This is because of a known issue with Backup Exec). Any ideas on this one?
LOL, thanks m8!
Lets see if i got it right .. :
In the Program/script box you enter :
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe
In the arguments box :
"& 'D:\Script\Script.ps1'"
This is in Task Scheduler ..
-Kenth
Edit: forgot a ' in
"& 'D:\Script\Script.ps1'"
problem is that de vSphere PowerCLI isn't loaded by default in powershell, so the script fails using your procedure. I first have to find a way to load up the vSphere PowerCLI and then run the script.. do I maybe have to reconfigure powershell?
ahh okey!
Add this to your script, at the top :
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
You can add a Add-PSSnapin cmdlet to one of the PS profile files
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi guys!
Thanks for all your help, all scripts are working the way we want!
Only one note regarding the scheduling, I had to add -command "C:\scripts\script.ps1" to the "Add arguments (optional):" field, instead of
"& 'D:\Script\Script.ps1'"
Happy new year!
Regards,
Erik