Start-job wait-job doesn't work with copy.here

丶灬走出姿态 提交于 2019-12-11 12:25:10

问题


I'm trying to copy files into a zip file. I used start-job and wait-job to wait for the copy process finish but it didnt work at all. This is the error when I call receive-job:

Method invocation failed because 
[Deserialized.System.__ComObject#{a7ae5f64-c4d7-4d7f-9307-4d24ee54b841}] does not contain a method 
named 'copyhere'.
    + CategoryInfo          : InvalidOperation: (copyhere:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
    + PSComputerName        : local

This is my code

$Shell = New-Object -com Shell.Application
$b = $shell.namespace($zippath.ToString())

$files = Get-ChildItem $fileDest -recurse -include *.*
foreach ($file in $files) {
    $job = Start-Job -scriptblock {$args[0].copyhere($args[1].fullname)} -Name copyfiles -argumentlist @($b, $file)
    Wait-Job -name copyfiles
    #Remove-Item -Path $file
}

Any idea?

Update

@PetSerAI, I also try to put that COM object into the job but it doesnt work too. Receive-job doesn't show anything. Below is the new code:

$func = {
    $zippath = "d:\nhl\test.zip"
    $Shell = New-Object -com Shell.Application
    $b = $shell.namespace($zippath.ToString())
    $b.CopyHere($args[0].tostring())
    #Remove-Item -Path $filedest
}

$file = "D:\nhl\abc\test.rar"
start-job -ScriptBlock $func -ArgumentList $file

来源:https://stackoverflow.com/questions/34237490/start-job-wait-job-doesnt-work-with-copy-here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!