PowerShell to transfer files from one computer to another

I created a file called ITT430Copy.PS1 and put this in it:
param(
[string]$computerName,
[string]$filePath
)
Copy-Item -Path $computerName -Destination $filePath -Recurse
Then in PowerShell with admin rights the following

$Action = New-ScheduledTaskAction -Execute “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” -Argument “-NonInteractive -NoLogo -NoProfile -File C:\ITT430-Storage\ITT430Copy.ps1 -SourcePath C:\ITT430-Files* -DestinationPath \ASB-ITT430\ITT430-Storage

$Action = New-ScheduledTaskAction -Execute “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” -Argument “-NonInteractive -NoLogo -NoProfile -File C:\ITT430-Storage\ITT430Copy.ps1 -SourcePath C:\ITT430-Files* -DestinationPath \ASB-ITT430\ITT430-Storage

$Trigger = New-ScheduledTaskTrigger -Once -At ‘12AM’ -RepetitionInterval (New-TimeSpan -minutes 15) -RepetitionDuration (New-Timespan -hour 1000)

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings (New-ScheduledTaskSettingsSet)

$Task | Register-ScheduledTask -TaskName ‘File Transfer AutoSync’ -User ‘DomainAdmin’ -Password ‘password’ $Task

It creates the task and I can see it in Task Scheduler but when it runs I get a 0X1. I added the storage folder to the domain using Sever manager for sharing and also both folders for sharing. Not sure why I do not have permission or if something is wring with the script. Thanks for any help.

Ray,
Welcome to the forum. :wave:t3:

There are several issues with your code and maybe your approach.

But first of all … when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

(Sometimes the “preformatted text” button hides behind the settings gear symbol! :wink: )

Now … your code …

  • $Action = New-ScheduledTaskAction .... Why twice? :wink:

  • You name your variables for your script $ComputerName and $FilePath. But when you run your script you use $SourcePath and $DestinationPath?! Is that on purpose? :man_shrugging:t3:

  • registering your task you pipe the created task to the cmdlet AND you provide it as paramter?! Is that on purpose?

In general: I’d recommend to solve one issue at a time. Have you tried to run your scipt outside of the task scheduler? Does it run there without error?

Have you considered using robocopy instead? There is no better option for simple copy jobs like you try to do here. :point_up_2:t3:

Are you really using your “Domain Admin” for this task? You should really not. A Domain Admin is made to manage and administer your AD domain - not to do tasks!!! :point_up_2:t3:

Why do you actually want to use PowerShell to create a scheduled task? Have you tried creating it manually?

Thanks so much I found ChatGPT and it helped me resolve the issues along with your post. There were directory errors and quote errors within the file and the script. It was run successfully after many changes and I got an A in the course thanks so much. If I post here again I will use the proper protocols for displaying script. I was required to use PowerShell for the assignment. Sometimes I think they just make up script so we can fix it because there was so much wrong. That or the scripting requirements are changed for different systems. Really they just want us to understand the script as we are learning cybersecurity for incident planning but I want them to work correctly and understand them.