首页 > 解决方案 > 访问被拒绝的 powershell 移动文件

问题描述

ht = @{}
$o = new-object PSObject -property @{ 
    from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\BCC"
    to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatiON" }
$ht.Add($o.from, $o) 

$o = new-object PSObject -property @{ 
    from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\BBB"
    to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatiON" }
$ht.Add($o.from, $o) 

$o = new-object PSObject -property @{ 
    from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\BAA"
    to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatiON" }
$ht.Add($o.from, $o) 

#  
foreach($server in $ht.keys  ) {

            copy-item $ht.Item($server).from -destination $ht.Item($server).to -Recurse ;

             }

sleep 5
$ht = $null
sleep 5

$ht = @{}
$o = new-object PSObject -property @{ 
    from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BCC"
    to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BCC\Processed" }
$ht.Add($o.from, $o) 

$o = new-object PSObject -property @{ 
    from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BBB"
    to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BBB\Processed" }
$ht.Add($o.from, $o) 

$o = new-object PSObject -property @{ 
    from = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BAA"
    to = "C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BAA\Processed" }
$ht.Add($o.from, $o) 

# the loop
foreach($server in $ht.keys  ) {

        gci |   Move-Item $ht.Item($server).from -Destination $ht.Item($server).to -Force   ;

             }

最后一个循环不起作用,我不断收到 access denied 错误,在一切运行之后,我有最后一部分实际上从一个地方移动到另一个地方。移动项目:拒绝访问路径“C:\Users\nicolae.calimanu\Documents\scripturi copiere\destinatie\BBB”。在 line:47 char:13 + Move-Item $ht.Item($server).from -Destination $ht.Item($s ... + ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\用户\nicola...\destination\BCC:DirectoryInfo) [Move-Item], IOException + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

我真的无法解决这个问题,为什么我不断收到错误消息。我也尝试过使用 -Force 选项,但是从 3 个项目的 ht 中,第一个没有通过 .

这是整个代码。

标签: powershellpowershell-3.0powershell-4.0

解决方案


发生这种情况是因为后一个循环试图移动源目录本身。通过添加-WhatIf开关,行为变得清晰。像这样,

foreach($server in $ht.keys  ) {
  Move-Item -whatif $ht.Item($server).from -Destination $ht.Item($server).to -Force   ;
}

# Line breaks added for readability
What if: Performing the operation "Move Directory" on target 
"Item: C:\temp\destinatie\BCC 
Destination: C:\temp\destinatie\BCC\Processed\BCC".

考虑修复gci零件,像这样,

foreach($server in $ht.keys  ) {
  gci $ht.Item($server).from |  % { Move-Item -whatif $_.FullName -Destination $ht.Item($server).to }
}

What if: Performing the operation "Move Directory" on target "Item: C:\temp\destinatie\BCC\Processed Destination: C:\temp\destinatie\BCC\Processed\Processed".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\1.txt Destination: C:\temp\destinatie\BCC\Processed\1.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\2.txt Destination: C:\temp\destinatie\BCC\Processed\2.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\3.txt Destination: C:\temp\destinatie\BCC\Processed\3.txt".

好多了。但是等等,它仍然在尝试移动Processed我们无法拥有的东西。让我们排除那个并再试一次。

foreach($server in $ht.keys  ) {
     gci $ht.Item($server).from -exclude "processed" |  % { Move-Item -whatif $_.FullName -Destination $ht.Item($server).to }
}

What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\1.txt Destination: C:\temp\destinatie\BCC\Processed\1.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\2.txt Destination: C:\temp\destinatie\BCC\Processed\2.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC\3.txt Destination: C:\temp\destinatie\BCC\Processed\3.txt".

推荐阅读