Hi all,
I have a Powershell script that downloads calendar content into a CSV file from an O365 account hosted on MS servers. It runs perfectly when I run the PS script manually and type in the passwords myself. When I encrypt those passwords to run the script via Task Manager, the script fails with the below errors.
This code comes with the original script (CalendarDumpToCSV.ps1 by Glen Scales) and works when I manually type in passwords.
$psCred = Get-Credential $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString()) $service.Credentials = $creds
Problem is, I need this to run this script automatically through a server for regular updates.
To set and store the encrypted pass, I use the following code.
$credential = Get-Credential $credential.Password | ConvertFrom-SecureString | Set-Content c:\CSV\EncPass.txt
To use the encrypted pass (on the same machine), I use this in the script.
$emailusername = "email@address.com" $encrypted = Get-Content c:\CSV\EncPass.txt | ConvertTo-SecureString $credential = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)
I have tried three different methods to store the password, including plain text, and consistently get the same error.
Errors below. Any suggestions appreciated.
Thanks.
Shad
====== ERRORS Encountered ======
PS F:\X\Powershell] ./CalendarDumpToCSV.ps1 email@address.com Exception calling "Bind" with "2" argument(s): <strong>"The request failed. The remote server returned an error: (401) Unauthorized."</strong> At F:\X\Powershell\CalendarDumpToCSV.ps1:93 char:63 + $Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind [[[[ ($service,$folderid) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException Exception calling "FindAppointments" with "2" argument(s): "The element at position 0 is invalid Parameter name: parentFolderIds" At F:\X\Powershell\CalendarDumpToCSV.ps1:110 char:37 + $fiItems = $service.FindAppointments [[[[ ($Calendar.Id,$CalendarView) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException