PowerShell 7.3 - how to to Schedule a Task with Specific User Credentials on Remote Server
I've hit a wall trying to I'm deploying to production and I'm attempting to set up I'm trying to create a scheduled task on a remote server using PowerShell 7.3, but I keep running into issues when specifying the user credentials. I've attempted to use the `New-ScheduledTask` and `Register-ScheduledTask` cmdlets, but I get a `System.Security.Authentication.AuthenticationException` when using a specific user that has the required permissions. Hereβs the part of my script where I'm attempting to create the task: ```powershell $action = New-ScheduledTaskAction -Execute 'C:\Path\To\YourScript.ps1' $trigger = New-ScheduledTaskTrigger -AtStartup $credential = Get-Credential -UserName 'DOMAIN\username' -Message 'Enter password' $task = New-ScheduledTask -Action $action -Trigger $trigger -User $credential.UserName -Password $credential.GetNetworkCredential().Password -RunLevel Highest Register-ScheduledTask -TaskName 'MyTask' -InputObject $task -ComputerName 'RemoteServer' ``` When I run this code, I receive the following behavior: `Register-ScheduledTask : The user name or password is incorrect.` Iβve double-checked the credentials and they work fine if I log into the server directly. Additionally, I've tried running the script in a different context using `Invoke-Command` to see if that made a difference: ```powershell Invoke-Command -ComputerName 'RemoteServer' -ScriptBlock { param($action, $trigger, $username, $password) $task = New-ScheduledTask -Action $action -Trigger $trigger -User $username -Password $password -RunLevel Highest Register-ScheduledTask -TaskName 'MyTask' -InputObject $task } -ArgumentList $action, $trigger, $credential.UserName, $credential.GetNetworkCredential().Password ``` However, I still face the same authentication errors. I've also confirmed that the remote machine is set up for PowerShell remoting and that I have the necessary permissions. Is there a specific configuration I need to be mindful of when scheduling tasks with user credentials remotely? Any insights would be greatly appreciated! I've been using Powershell for about a year now. I appreciate any insights! The project is a CLI tool built with Powershell. Is there a better approach? I'm working in a Windows 10 environment. Thanks in advance! Any ideas how to fix this?