【问题标题】:PowerShell Remove-Item cannot delete populated folderPowerShell Remove-Item 无法删除填充的文件夹
【发布时间】:2014-07-12 03:15:15
【问题描述】:

我正在尝试删除以下文件:

C:\inetpub\wwwroot\my_site\

my_site下有很多文件和文件夹,包括bin文件夹,里面有一些.dll文件。运行脚本后,我收到一条错误消息,指出某些 .dll 文件无法访问,因为它们正被另一个进程使用,并且在某些 dll 上,我收到权限被拒绝错误。

我不知道问题是什么。我正在使用以下脚本,它适用于任何其他文件夹,但不适用于我要删除的文件夹。

get-childitem "c:\inetpub\wwwroot\my_site\" -recurse | % {

remove-item $_.FullName -recurse -force

}

【问题讨论】:

  • 您收到一条合法的错误消息...maybe this will help.
  • 奇怪的是,如果我进入该文件夹并全选并删除它们,它会删除所有内容,但使用脚本时出现错误,我不知道为什么
  • 你的脚本在 my_site 中吗?
  • 你应该去掉Remove-Item上的-recurse参数,因为你不能递归删除一个文件。虽然我不认为这会导致你的错误
  • 我的脚本在不同的文件夹中。基本上我正在尝试从最近的备份中恢复网站。要恢复备份,我首先需要删除 my_site 中的内容,然后用备份替换它

标签: powershell


【解决方案1】:

以管理员身份运行 PowerShell

C:\inetpub 目录具有特殊权限,需要您以管理员身份运行才能在文件夹内进行任何更改。

【讨论】:

  • 我以管理员身份运行 powershell。但仍然没有运气。奇怪的是,如果我从 powershell 窗口发出正常的 remove -item 命令,它会删除管理内容,但如果我在脚本中编写相同的东西,它会给我错误消息
【解决方案2】:

这是我正在使用的脚本

   $content = get-childitem 'C:\Backups\my_site'      
   $sortedContent = $content |  Sort-Object LastWriteTime -Descending  


   write-host "This is the list of all the backups for my_site :"

   $count = 1
   foreach ($item in $sortedContent)
   {

    Write-Host ("{0}: {1}" -f $count, $item.Name)
    $count++ 
    }



    # 2.Take input from user
    $itemNumber = Read-Host "Please select which backup you want to restore"
    $confirmation = Read-Host "Are you Sure You Want To Proceed:"
    # 2.Take input from user

    if ($confirmation -eq 'y') {

    # 3. BACKUP script
   ./bacup_mysite.ps1
    # 3. BACKUP 

    # 4. DELETE CONTENTS OF my_site


    get-childitem "C:\inetpub\wwwroot\my_site\" -recurse | % {

    remove-item $_.FullName -recurse -force

    }

    }


    # 4. DELETE CONTENTS OF APP

    # 5. COPY CONTENTS OF ZIP INTO APP DIRECTORY
    $itemNumber = $itemNumber -1
    if($sortedContent[$itemNumber].PSIsContainer -eq $true)
    { 
    $src = $sortedContent[$itemNumber].FullName + "\"


    $WebzipPath = $src + "my_site.zip"


    $Date = Get-Date
    $folder_date = $Date.ToString("yyyy-MM-dd_HHmm")
    $tempPath = 'C:\_Temp\Restore\my_site_restore_' + $folder_date
    if (!(Test-Path -path $tempPath)) 
    {
        New-Item $tempPath -type directory
    }   


    ./Library/unzip.ps1 $WebzipPath $tempPath



    $tempPathWeb = $tempPath + "/my_site/*" 

    Copy-Item  -Path $tempPat -Destination 'C:\inetpub\wwwroot\my_site\' -Recurse -
    force

【讨论】:

    猜你喜欢
    • 2020-11-17
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2021-08-15
    • 2018-08-03
    • 1970-01-01
    • 2023-04-07
    • 2012-07-18
    相关资源
    最近更新 更多