1

I am trying to write an automation script to remove calendar permissions on Exchange Online automatically using Powershell and Task Scheduler.

When I run the command, the permission is removed as expected, but when I create the task, I get an error that says "Missing argument in parameter list" and I am not sure why.

This is the entire script:

$Trigger= New-ScheduledTaskTrigger -Once -At "01/11/2023 10:00:00"
$User= "user-account"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoExit [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; $userName = '[email protected]'; $passwordText = Get-Content 'C:\Users\User\Secure.txt'; $securePwd = $passwordText | ConvertTo-SecureString; $credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $securePwd; connect-exchangeonline -Credential $credObject; Remove-MailboxFolderPermission -Identity [email protected]:\Calendar -User [email protected] -Confirm:$false"
Register-ScheduledTask -TaskName "REMOVE-CONFIG" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force

I have determined that the issue comes in here: -ArgumentList $userName, $securePwd. When the task runs the script, it does't recognise that there are these two $ variables in the command.

How do I get this script to execute correctly?

Any help will be appreciated!

Kind regards,

Dust

UPDATE This is my new command that still doesn't work however the command works if I manually create a task in task scheduler:

$Trigger= New-ScheduledTaskTrigger -Once -At "01/11/2023 19:00:00"
$User= "MYDOMAIN\User"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoExit -Command [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; Set-Variable -Name userName -Value '[email protected]'; Set-Variable -Name passwordText -Value (Get-Content C:\Users\User\Secure.txt); Set-Variable -Name securePwd -Value ($passwordText | ConvertTo-SecureString); Set-Variable -Name credObject -Value (New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $securePwd); connect-exchangeonline -Credential $credObject; Remove-MailboxFolderPermission -Identity [email protected]:\Calendar -User [email protected] -Confirm:$false"
Register-ScheduledTask -TaskName "REMOVE-CONFIG" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force`
1
  • The entire argument list is in double quotes and treated as a string. So best solution is to get password as a separate command : $password = Get-Content 'C:\Users\User\Secure.txt'. Then use the variable $password in the argument list. Commented Jan 11, 2023 at 11:20

1 Answer 1

1

I suggest to call PowerShell with parameter -EncodedCommand so you don't need to worry about quoting and escaping. Instead, you can just write a regular PowerShell script and encode it to base-64 afterwards. Using a here-string @'…' lets you avoid to crunch everything into a single line.

$Trigger = New-ScheduledTaskTrigger -Once -At "01/11/2023 19:00:00"
$User = "MYDOMAIN\User"

$command = @'
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$userName = '[email protected]'
$passwordText = Get-Content C:\Users\User\Secure.txt
$securePwd = $passwordText | ConvertTo-SecureString
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $securePwd
connect-exchangeonline -Credential $credObject 
Remove-MailboxFolderPermission -Identity [email protected]:\Calendar -User [email protected] -Confirm:$false
'@

# Encode command to base-64
$commandBytes = [System.Text.Encoding]::Unicode.GetBytes( $command )
$commandBase64 = [Convert]::ToBase64String( $commandBytes )

$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoExit -EncodedCommand $commandBase64"
Register-ScheduledTask -TaskName "REMOVE-CONFIG" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer but this hasn't worked either. If I manually copy the whole command into the action field while creating a task in task scheduler, everything works fine, authenticates and removes permissions as I'd expect. It appears that it's got something to do with the command being in "".
@DustinEllse I've updated my answer based on your latest edits. Now using -EncodedCommand which is much easier to use for complex commands.
Thank you very much! I'll try this as soon as I can get back into my test environment. May I ask you to kindly edit out the email address that I mistakenly forgot in my script (the one that ends in -cld) as I'd like to avoid any bots grabbing it lol!
This worked perfectly! Thank you very much for your help on this, it's much appreciated! I'll be using this command to write a GUI that will allow me to schedule adding and removing permissions which will make my life so much easier! I've been stumped by the various issues I've faced with this so far as I've never used Task Scheduler before. This is great! Thanks again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.