简体   繁体   中英

Start a folder copy and continue script using PowerShell

I am several layers deep into a problem with a PowerShell Script that we use to manage user accounts. When users move between locations, we move their ADUser to a new OU, and move their data between servers.

While researching how to copy their home folder, it was determined that there is no easy way to copy files using PowerShell and exclude certain directories. As a result we are calling robocopy.exe and using /XD. This also lets us start the copy, but have the script continue on to the next user.

We are attempting to enhance our script by moving or deleting the older folder depending on the user type. I created a PS1 file that I would like to call so that the script can continue to the next user immediately and not wait for the copy to finish. The PS1 file looks like this:

Param(
  [string]$source,
  [string]$destination,
  [string]$oldStaff
)

robocopy $source $destination
move $source $oldStaff

When I attempt to call this from our script in the following way:

start powershell.exe .\Move-WCFolders.ps1 $source $destination $oldStaff

I get the following error message:

Start-Process : A positional parameter cannot be found that accepts argument '<%Removed server name before posting to Stack Overflow%>'. At line:1 char:1 + start powershell.exe .\\Move-WCFolders.ps1 $source $Description + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

I'm sure that there is likely a far better way to achieve what I am trying to achieve. It doesn't feel 'clean' to need to start robocopy, or to have to call things in an external script. The end goal is to copy folders while excluding directories, move the folder when the copy is done, but not have to wait for the copy to complete to continue the script.

Try using Start-Job . It's easier than you think, and you'll be able to capture the results for later:

$job = Start-Job -FilePath '.\Move-WCFolder.ps1' -ArgumentList $source, $destination, $oldStaff

# do more processing here
# . . .

# wait for the job to finish and output the results
$job | Receive-Job

start is an alias for Start-Process

The error is because the arguments at the end.

AFAIK, you will most likely want to turn them into a single string to pass to powershell.

You may want to try something like this:

start powershell.exe -File .\Move-WCFolders.ps1 -ArgumentList "$source $destination $oldStaff"

You will want to make sure you have the escaping right, so it may end up looking like this:

start powershell.exe -File .\Move-WCFolders.ps1 -ArgumentList "`"$source`" `"$destination`" `"$oldStaff`""

You could also investigate using Start-Job.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM