2016-02-26 57 views
0

我正在爲我們的客戶在我的空閒時間的磁盤空間腳本。我只是用ISE對它進行了測試,看起來它一直在工作,直到我查看了成績單。調試磁盤空間清理腳本 - 刪除它不應該是的文件

在第32行的第一個刪除週期中,有些部分刪除C:\ Windows \ System32 \中的文件,這當然不是我想要的。我確信我做錯了,但我檢查了錯別字,並且我不明白它如何從用戶目錄中獲得%system32%。

If (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) 
    { 
     $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + ' (Elevated)' 
     $Host.UI.RawUI.BackgroundColor = 'DarkBlue' 
     Clear-Host 
    } 
Else 
    { 
     $newProcess = New-Object Diagnostics.ProcessStartInfo 'PowerShell' 
     $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'" 
     $newProcess.Verb = 'runas' 
     [Diagnostics.Process]::Start($newProcess) | Out-Null 
     exit 
    } 
If ((Test-Path "C:\DiskSpaceCleanupLog\") -eq $False) 
    { 
     New-Item -ItemType Directory -Path "C:\DiskSpaceCleanupLog\" 
    } 
$Date = [string]::Format("{0:dd-MM-yyyy}", [datetime]::Now.Date) 
$LogName = "C:\DiskSpaceCleanupLog\" + $Date + "Log.txt" 
Start-Transcript $LogName 
$Path = @() 
$Array = @(Get-ChildItem C:\Users | Select-Object Name) 
Read-Host -Verbose "Removing User Account temp files..." 
Foreach ($Name IN $Array) 
    { 

     $Path = ("C:\Users\" + $Name.Name + "\AppData\Local\Temp\") 
    }    
Foreach ($Path IN $Array) 
    { 
     Get-ChildItem | Remove-Item -Recurse -WhatIf 
    } 
Remove-Variable Path 
Read-Host -Verbose "Removing User Account crash dumps..." 
Foreach ($Name IN $Array) 
    { 
     $Path = ("C:\Users\" + $Name.Name + "\AppData\Local\CrashDumps\") 
    } 
Foreach ($Path IN $Array) 
    { 
     Get-ChildItem | Remove-Item -Recurse -WhatIf 
    } 
Remove-Variable Path 
Read-Host -Verbose "Removing User Account reporting files..." 
Foreach ($Name IN $Array) 
    { 
     $Path = ("C:\Users\" + $Name.Name + "\AppData\Local\Microsoft\Windows\WER\ReportArchive\") 
    }    
Foreach ($Temp IN $Path) 
    { 
     Get-ChildItem | Remove-Item -Recurse -WhatIf 
    } 
Remove-Variable Path 
Read-Host -Verbose "Removing User Account temp files from Internet Explorer..." 
Foreach ($Name IN $Array) 
    { 
     $Path = ("C:\Users\" + $Name.Name + "\AppData\Local\Microsoft\Windows\Temporary Internet Files\") 
    }    
Foreach ($Temp IN $Path) 
    { 
     Get-ChildItem | Remove-Item -Recurse -WhatIf 
    } 
Read-Host -Verbose "Removing Recycle Bin files..." 
Remove-Item -LiteralPath 'C:\$Recycle.Bin\' -Recurse -WhatIf 
Read-Host -Verbose "Removing global crash dumps..." 
Remove-Item "C:\ProgramData\Microsoft\Windows\WER\ReportQueue" -Recurse -WhatIf 
Remove-Item "C:\ProgramData\Microsoft\Windows\WER\ReportArchive" -Recurse -WhatIf 
Read-Host -Verbose "Removing Windows Update cached files..." 
Stop-Service -DisplayName 'Windows Update' 
Remove-Item "C:\Windows\SoftwareDistribution\Download\*" -Recurse -WhatIf 
Start-Service -DisplayName 'Windows Update' 
Remove-Variable Array, Path 
Read-Host -Verbose "Cleaning base image of update cache..." 
DISM.exe /Online /Cleanup-Image /SPSuperseded 
Read-Host -Verbose "Running Windows Clean Manager..." 
$OSVersion = Get-WMIObject -Class Win32_OperatingSystem | Format-Table Version 
If ($OSVersion -le 6.1) 
    { 
     cleanmgr.exe /verylowdisk 
    } 
Read-Host -Verbose "Removal is complete. Sending logs..." 
Stop-Transcript 
$SecurePassword = ConvertTo-SecureString "InsertPasswordHere" -AsPlainText -Force 
$emailcredential = New-Object System.Management.Automation.PSCredential ("[email protected]", $SecurePassword) 
Send-MailMessage -To "Name Here <[email protected]>" -From "Name Here <[email protected]>" -Subject ("Disk Space Cleanup Log - " + $Date) -Body "Attached is the log from the script." -Attachments $LogName -SmtpServer "smtp.office365.com" -Credential $emailcredential -UseSSL -Port "587" -DeliveryNotificationOption OnFailure 

線32 Get-ChildItem | Remove-Item -Recurse -WhatIf

+0

也許用戶有一個C:\ Windows \ System32目錄中的某個快捷方式? –

+0

我正在測試我的電腦,並且我沒有任何鏈接。或者連接點。 雖然我沒有想到。 – Moridn

回答

1

這是在你的代碼中應該調整的幾件事情,但現在正在出現的問題是你沒有指定-Path。因此Get-ChildItem將返回工作目錄中的項目

Get-ChildItem | Remove-Item -Recurse -WhatIf 

應改用

Get-ChildItem $path | Remove-Item -Recurse -WhatIf 

就像我說的,雖然有幾個潛在的缺陷和需要改進的地方還有待解決。你使用相同的循環5次。一對是完全一樣的。

+0

Doh!那是一個。你能看到其他人嗎? 使用這些作爲學習經驗。 – Moridn

+0

@Moridn如果你得到腳本工作,那麼我會建議去CodeReview.SE。 StackOverlflow更適合於特定的問題和答案。 – Matt

+0

太棒了。我會試一試,謝謝。 – Moridn

0

我相信這個問題是在第23行,其中的代碼不填充用完整的路徑名數組。有關如何獲取完整路徑名的建議,請參閱Get full path of the files in PowerShell

+0

我在路徑中添加了全名修飾符,它仍然試圖刪除system32目錄中的文件。 澄清,現在#32看起來像這樣。 Get-ChildItem | %{$ _。FullName} | Remove-Item -Recurse -WhatIf – Moridn