Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell Scheduled Task; Encrypted Password: Key not valid for use in specified state

$
0
0

Hello everyone!

This is my first post in this forum and I'm looking forward to it!

Anyway here's the problem. I got a Powershell Script which I want to implement in the Task Scheduler. The script automates the VMWare Tool upgrade. The problem is the following: when I manually run the task over Powershell it completes without an error. BUT if I run the task through Task Scheduler it displays an error in the log I create.

Here's the script:

Start-Transcript -Path C:\temp\log.txt -Append

#Decrypt Password
$securePass = Get-Content C:\Path\EncryptedPwd.txt | ConvertTo-SecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePass)
$Pwd = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

#Username
$user = "USER"


#Load VMware Power-CLI Snap-In
Get-Module -Name VMWare* -Listavailable | Import-Module
#vCenter FQDN
$VCenterName = "VCenterFQDN"
#Connect vCenter 
Connect-VIServer -Server $VCenterName -User $user -Password $Pwd 
#Update VMware Tool nach TAGS
Get-VM -Tag "Tag" | Where-Object { $_.ExtensionData.Guest.ToolsVersionStatus -eq 'guestToolsNeedUpgrade' -and $_.PowerState -eq 'PoweredOn' } | Get-VMGuest | Where-Object { $_.GuestFamily -eq "windowsGuest" } | Update-Tools -NoReboot –RunAsync

Stop-Transcript

#exit(0)


<##
#Pausieren des Scripts 
Start-Sleep -s 180
#Generate Report für PRTG-Check 
Get-VM -Tag "Tag" | Where-Object { $_.ExtensionData.Guest.ToolsVersionStatus -eq 'guestToolsNeedUpgrade' -and $_.PowerState -eq 'PoweredOn' } | Get-VMGuest | Where-Object { $_.GuestFamily -eq "windowsGuest" } | Out-File C:\PathToReport.txt -width 120
##>

The error in the log is the following:

At C:\PathToPSScript.ps1:13 char:70
+ ... ent C:\Path\EncryptedPwd.txt | ConvertTo-SecureString
+                                                    ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
    + FullyQualifiedErrorId :
ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
ConvertTo-SecureString : Key not valid for use in specified state.

As you can see there's a problem in the ConvertTo-SecureString and I have not found a possible explanation till now why it is working when I start the script manually and why it isn't working when it is run throught the Task Scheduler.
There are more errors, but they are errors due to the ConvertTo-SecureString error.

Can anyone help me here?




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>