Folks,
I attempted to write a simple powercli script today to create snapshots and delete any old than 2 days for the VM named Nix.
[code]
Connect-VIServer -server grima.domain.com
$sName = Automated Snapshot
$today = Get-Date
#Create a snapshot of each vm
Get-VM -Name Nix | New-Snapshot -Name $sName +$today
#Delete Snapshots older than 48hrs.
Get-VM -Name Nix | Get-Snapshot | Where { $_.Created -lt (Get-Date).AddDays(-2)} | remove-snapshot -confirm:$false
[code]
When I try and run it via powershell I get the following error
[quote]
The term 'Connect-VIServer' is not recognized as the name of a cmdlet, function
, script file, or operable program. Check the spelling of the name, or if a pat
h was included, verify that the path is correct and try again.
At C:\Users\jukas\Desktop\Hourly.ps1:9 char:17
+ Connect-VIServer <<<< -server grima.domain.com
+ CategoryInfo : ObjectNotFound: (Connect-VIServer:String) [], Co
mmandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Automated' is not recognized as the name of a cmdlet, function, scrip
t file, or operable program. Check the spelling of the name, or if a path was i
ncluded, verify that the path is correct and try again.
At C:\Users\jukas\Desktop\Hourly.ps1:11 char:19
+ $sName = Automated <<<< Snapshot
+ CategoryInfo : ObjectNotFound: (Automated:String) [], CommandNo
tFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Get-VM' is not recognized as the name of a cmdlet, function, script f
ile, or operable program. Check the spelling of the name, or if a path was incl
uded, verify that the path is correct and try again.
At C:\Users\jukas\Desktop\Hourly.ps1:15 char:7
+ Get-VM <<<< | New-Snapshot -Name $sName +$today
+ CategoryInfo : ObjectNotFound: (Get-VM:String) [], CommandNotFo
undException
+ FullyQualifiedErrorId : CommandNotFoundException
Done
[/quote]
Yet if I connect directly via power shell I can connect and at least enter a basic snapshot
[quote]
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Connect-
VIServer grima.domain.com
Name Port User
---- ---- ----
grima.domain.com 443 Jukas
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VM -
Name Nix | New-Snapshot -Name Automated
Name Description PowerState
---- ----------- ----------
Automated PoweredOff
[/quote]
The script above looked ok to me, based on what I've read in the documentation. Can anyone shed any light on what I'm doing incorrectly?