Executing a script on a remote server

Hi all!

I’ve been requested to create a shortcut for a colleague so that they can manually run a task, that is automated, if something doesn’t work.
I’ve tried to remote to the server and then manually run the script but it didn’t work.
Any help would be greatly appreciated.

Thanks!

You need to provide more information.

Like what was the command you tried to run.
What does the script do.
What didn’t work, like error message etc. etc.

The shortcut needs to run a script that uploads all files with the file extension .dat to an FTP server. This script is scheduled to run at certain times during the week but on the occasion that there is an error, my colleague would like a shortcut to run the script as when it may be needed.

I have managed to remote to the server and also find the script that needs to be run.

Enter-PSSession Server01
Set-Location D:\Scripts\
.\Script.ps1
Exit-PSSession

When the script runs it produces this error.

Get-ChildItem : Access is denied

When the Server01 runs the script as a scheduled task, or when I manually run it through the ISE, it runs with no errors.

Could be a number of reasons.

But first the error itself is pretty clear, whatever you’re trying to do is denied due to access restrictions.
So if it works when running as a scheduled task and manually on the machine then you have a starting point.

E.g. if it works for one account, what is the difference to the other account.
Does the Get-ChildItem command access a resource that need admin privileges?
Do you run the script as admin when it works, does it work without it and so forth.
What does the scheduled task run as?

Another thing to look out for is kerberos double hop issue.
If you just google it you’ll find a number of articles about it.

But before checking if it’s a double hop issue start with checking under which circumstances it work.
E.g. with or with admin, account differences etc.

As a scheduled task and also manually, it’s being run by an administrator. I’ll have a look at kerberos double hop issue too.
Something else i’ve been trying is seeing if i can remote into Server01 and then Start-scheduledtask.

Enter-PSSession bbc-interfaces2
Start-ScheduledTask -TaskName "Transfer to FTP Server"
Exit-PSSession

If I execute each line separately it works as desired but if I run the whole script, it produces this error.

Start-ScheduledTask : The system cannot find the file specified.
At line:2 char:1
+ Start-ScheduledTask -TaskName "Transfer to FTP Server"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Start-ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x80070002,Start-ScheduledTask

IS this a better approch to take?

You may need to provide the whole path to the task.

Check the section “Using the path to a scheduled task” in the following link:
https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell/

Try putting a delay in what you are showing here.
The error is just saying it can’t hit the Task Name, meaning, the session is not completed yet for your target by the time that task line is called.

Example:

Enter-PSSession bbc-interfaces2
Start-Sleep -Seconds 3
Start-ScheduledTask -TaskName "Transfer to FTP Server"
Start-Sleep -Seconds 3
Exit-PSSession

Or use a Remote session vs an interactive one to the target server.
Something like

$sess = New-PSSession -ComputerName bbc-interfaces2 -Credential (Get-Credential)
Invoke-Command -Session $sess -ScriptBlock {(Get-ScheduledTask).TaskName -match "Transfer to FTP Server" | Start-ScheduledTask }