简体   繁体   English

使用PowerShell启动文件夹副本并继续执行脚本

[英]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. 我深入探讨了用于管理用户帐户的PowerShell脚本的问题。 When users move between locations, we move their ADUser to a new OU, and move their data between servers. 当用户在位置之间移动时,我们将其ADUser移动到新的OU,并在服务器之间移动其数据。

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. 在研究如何复制其主文件夹时,确定没有简单的方法可以使用PowerShell复制文件并排除某些目录。 As a result we are calling robocopy.exe and using /XD. 结果,我们正在调用robocopy.exe并使用/ 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. 我创建了一个我想调用的PS1文件,以便脚本可以立即继续向下一个用户发送,而不必等待副本完成。 The PS1 file looks like this: PS1文件如下所示:

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%>'. 启动过程:找不到位置参数,该位置参数在发布到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 在第1行:char:1 +启动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. 不需要启动robocopy或必须调用外部脚本中的内容并不“干净”。 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 . 尝试使用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 start是启动过程的别名

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. AFAIK,您很可能希望将它们转换为单个字符串以传递给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. 您也可以使用“开始作业”进行调查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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