简体   繁体   English

如何使代码块在下一条命令运行之前完全完成-Powershell V5

[英]How to make a code block finish completely before next command runs - Powershell V5

I found a few posts recommending either using Start-Process -Wait or piping the .EXE to Out-Null , which works well for .EXE files but I have a foreach loop that contains built-in PowerShell cmdlets. 我发现了一些建议使用Start-Process -Wait或将.EXE传递到Out-Null帖子,这对.EXE文件效果很好,但是我有一个包含内置PowerShell cmdlet的foreach循环。 How do I go about making sure the loop finishes completely first before the next lines of code starts executing? 如何确保在下一行代码开始执行之前,循环首先完全完成?

For reference, I am using a tool called rclone to copy files/folders into our team drives but it currently does not support copying empty directories. 作为参考,我正在使用一个名为rclone的工具将文件/文件夹复制到我们的团队驱动器中,但是它目前不支持复制空目录。 The work around is to create a temp file in all empty directories, run the tool's sync command, run another powershell command to delete the temp files from the local directories, then re-run the tool's sync command to remove it from the team drive. 解决方法是在所有空目录中创建一个临时文件,运行该工具的sync命令,运行另一个powershell命令以从本地目录中删除该临时文件,然后重新运行该工具的sync命令以将其从团队驱动器中删除。 Code reference below: 下面的代码参考:

$shares = "Office Operations","Training"

# iterate through each share and output full path name to empty shares
$emptyDirs = foreach ($share in $shares) {
    Get-ChildItem -Path "\\servername\e`$\rootshare\$share" -Recurse -Directory | Where-Object {$_.GetFileSystemInfos().Count -eq 0} | select FullName 
}

# iterate through each empty directory and create a temp.txt file with a random string
$emptyDirs | % {new-item $_.FullName -ItemType file -name temp.xyz -value "This is a test string"}

# initial rclone sync to copy over directory with temp.txt file
foreach ($share in $shares) {
    Start-Process "C:\program files\rclone\rclone.exe" -ArgumentList "sync `"\\servername\e`$\rootshare\$share`" `"$($share + ':')`"" -Wait -NoNewWindow
}

# iterate through each empty folder, cd accordingly and delete all .txt files
$emptyDirs | % {   
    Set-Location $_.FullName
    Remove-Item * -Include *.xyz
}

# second rclone sync to remove temp.txt file while maintaining folder structure
foreach ($share in $shares) {
    Start-Process "C:\program files\rclone\rclone.exe" -ArgumentList "sync `"\\servername\e`$\rootshare\$share`" `"$($share + ':')`"" -Wait -NoNewWindow
}

Code works, but I have to manually run the 2nd sync command otherwise some temp.xyz files are not deleted from the team drives. 代码可以工作,但是我必须手动运行2nd sync命令,否则某些temp.xyz文件不会从团队驱动器中删除。 It seems like it should work because it is removed from the local shares. 似乎应该起作用,因为它已从本地共享中删除。 Not sure if this is an issue with PowerShell or the app. 不确定这是PowerShell还是应用程序问题。

Running the application as a normal PowerShell command should work just fine for you, unless the application does something unusual: 除非应用程序执行异常操作,否则以正常的PowerShell命令运行该应用程序对您来说应该可以正常工作:

rclone.exe sync "\\servername\e`$\rootshare\$share" "$($share + ':')"

If you need the full path to the application and it has spaces in the path, then use the invoke special character: 如果您需要应用程序的完整路径,并且路径中有空格,请使用invoke特殊字符:

& "C:\program files\rclone\rclone.exe" sync "\\servername\e`$\rootshare\$share" "$($share + ':')"

This will make PowerShell wait for the execution of the application just like it would for any native PowerShell command. 这将使PowerShell像对待任何本机PowerShell命令一样,等待应用程序的执行。 You can redirect the output if you need to. 您可以根据需要重定向输出。

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

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