powershell - 访问被拒绝的 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 中,第一个没有通过 .
这是整个代码。
解决方案
发生这种情况是因为后一个循环试图移动源目录本身。通过添加-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".
推荐阅读
- sql-server - 无法筛选 SQL Server 查询中的空记录
- android-livedata - SingleLiveEvent post,如果多次调用,则仅调度最后一个事件(但我需要视图中的所有事件)
- html - 如何停止保证金崩溃
- javascript - 反应如何更改列表中的单个按钮
- python - 如何显示来自狗和猫数据集的随机照片?
- elixir - 如何枚举获取ecto成员elixir phoenix的索引
- shell - 等效于 for ((c=1; c<=5; c++)) for /bin/sh
- python - Python VSCode“移动”导入
- java - 我如何使用通用方法使用 gson 序列化/反序列化
- python - Blender 空闲导入