访问被拒绝的 powershell 移动文件

Access denied powershell moving files

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   ;

             }

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

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

这是完整的代码。

发生这种情况,因为后一个循环试图移动源目录本身。通过添加 -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.txt Destination: C:\temp\destinatie\BCC\Processed.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC.txt Destination: C:\temp\destinatie\BCC\Processed.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC.txt Destination: C:\temp\destinatie\BCC\Processed.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.txt Destination: C:\temp\destinatie\BCC\Processed.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC.txt Destination: C:\temp\destinatie\BCC\Processed.txt".
What if: Performing the operation "Move File" on target "Item: C:\temp\destinatie\BCC.txt Destination: C:\temp\destinatie\BCC\Processed.txt".