PowerShell Remove-Item无法删除已填充的文件夹

时间:2014-07-10 15:08:44

标签: powershell

我正在尝试删除一些文件:

C:\的Inetpub \ wwwroot的\ my_site \

my_site下,有许多文件和文件夹,包括包含一些.dll文件的bin文件夹。一旦我运行脚本,我就会收到一条错误消息,指出某些.dll文件无法被访问,因为它们被另一个进程使用,而在某些dll上,我收到了一个权限被拒绝的错误。

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

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

remove-item $_.FullName -recurse -force

}

2 个答案:

答案 0 :(得分:0)

以管理员身份运行PowerShell

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

答案 1 :(得分:0)

这是我正在使用的脚本

   $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
相关问题