Hi all,
I have a similar issue, but I have administrative permission on the VCenter. In fact if i run manually the script, it work. but if i scheduled it from windows task scheduler don't work with the same error of the thread.
the script is:
<# encript password - I use this the first time to encript the password on a file
$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content c:\tmp\enc_password.txt
#>
#Inserisco la username
$UserName = "USERNAME"
#take encripted password
$SecurePassword = gc "c:\tmp\enc_password.txt" | ConvertTo-SecureString
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword
#create a connection to the VCenter VmWare
Connect-VIServer -Server VCENTER -Protocol https -Credential $Credentials -Force
$srv = "SERVER01"
#Shutdown the server
#Stop-VM -VM "$srv" -Confirm:$false
Get-VM $srv | Shutdown-VMGuest -Confirm:$false
#Wait the server is down
while ($test -ne $False){
$test = Test-Connection -ComputerName "$srv" -Quiet -Count 1
}
sleep 30
#Execute the snapshot
New-Snapshot -VM $srv -Name WindowsPatching
sleep 30
#start the server
Start-VM -VM "$srv"
The error is:
ConvertTo-SecureString : Key not valid for use in specified state.
At C:\temp\Create_Snapshot.ps1:10 char:47
+ $SecurePassword = gc "c:\tmp\enc_password.txt" | ConvertTo-SecureString
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
+ FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.Conv
ertToSecureStringCommand
New-Object : Cannot find an overload for "PSCredential" and the argument count: "2".
At C:\temp\Create_Snapshot.ps1:12 char:16
+ ... edentials = New-Object System.Management.Automation.PSCredential ("$U ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Connect-VIServer : Cannot validate argument on parameter 'Credential'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At C:\temp\Create_Snapshot.ps1:15 char:74
+ ... r VCENTER -Protocol https -Credential $Credentials -Force
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Connect-VIServer], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIS
erver
I think the error is in the manage the credential task scheduler, it don't work correctly with the powershell. I tried to use the same account to scheduled the task, but don't work.
The Hypervisor is: VMware ESXi, 6.5.0, 20502893
You have any idea?
Thank a lot
Antonio