Error in Script

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Dominique Hofmann Dominique Hofmann 3 years, 4 months ago.

  • Author
    Posts
  • #11758
    Profile photo of Dominique Hofmann
    Dominique Hofmann
    Participant

    Hello together

    I need help.
    I wrote a script, which goes into a directory and there it filtered out every folder which is older than 30 days or are in a csv-file.
    incidentally it calculate the size of all the folders and generate a pdf-file with Informations about the folders.

    My Problem is now that this script doesn't work anymore and a few day ago it worked fine.

    The error is that it delete all the folders or only a few

    So please can you read my script and tell me if you find any error

    [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    write-host "Einlesen der Ordner in Q:\Service_Desk..."
    $d = "N:\Test"
    $dirs = gci -Path $d | Where-Object -FilterScript {($_.LastWriteTime -lt (Get-Date).AddDays(1))} | foreach-object -process { $_.FullName }
    $dirswhitelist = gci -Path $d | foreach-object -process { $_.FullName }
    $sizeFolderOld = (Get-ChildItem $d -recurse | Measure-Object -property length -sum)
    $sizeOld = "{0:N2}" -f ($sizeFolderOld.sum / 1MB) + " MB"

    write-host "Done."
    write-host ""
    write-host "Einlesen der Whitelist..."
    $csv = import-csv N:\Service_Desk_Bereinigung\whitelist2.csv
    $deletedfolders = @()
    $errorfolders = @()
    $whitelistfolders = @()
    write-host "Done."
    write-host ""
    write-host "Beginnen der Löschung... Das kann mehrere Minuten dauern..."
    foreach($di in $dirs){
    $whitelist = $false
    foreach($eintrag in $csv){

    if ($eintrag.Ordnernamen -eq [System.IO.Path]::GetFileNameWithoutExtension($di) + [System.IO.Path]::GetExtension($di)){
    $whitelist = $true
    }
    }
    if ($whitelist -ne $true ){
    Try{
    $deletedfolders += $di
    remove-item -recurse -force $di -ErrorAction Stop

    }
    Catch [System.Management.Automation.ItemNotFoundException] {
    $alreadydeletedfolders += $di
    }
    Catch {
    $errorfolders += $di
    }

    }

    else{
    #Do Nothing
    }
    }
    foreach($diwhite in $dirswhitelist){
    $whitelist = $false
    foreach($eintrag in $csv){

    if ($eintrag.Ordnernamen -eq [System.IO.Path]::GetFileNameWithoutExtension($diwhite) + [System.IO.Path]::GetExtension($diwhite)){
    $whitelist = $true
    }
    }
    if($whitelist -eq $true){

    if ([System.IO.Path]::GetExtension($diwhite) -eq ""){
    $whitelistfolders += $diwhite +"\"
    }
    else{
    $whitelistfolders += $diwhite
    }
    }
    else{
    #Do Nothing
    }
    }
    $sizeFolderNew = (Get-ChildItem $d -recurse | Measure-Object -property length -sum)
    $sizeNew = "{0:N2}" -f ($sizeFolderNew.sum / 1MB) + " MB"
    write-host "Done."
    write-host ""
    write-host "Erstellen des Berichts..."
    # Vorbereitung für Speicherort, sowie Konvertierung zu einer .pdf Datei #
    $date = "{0:dd.MM.yyyy_HH.mm.ss}" -f (Get-Date)
    $dateDays = "{0:dd.MM.yyyy}" -f (Get-Date)
    $dateHours = "{0:HH.mm.ss}" -f (Get-Date)
    $saveaspath = "N:\Service_Desk_Bereinigung"
    $destiny = $saveaspath
    $saveaspath = [ref] $destiny
    $formatPDF = [ref] 17

    #Schreiben der Word Datei#
    $word=new-object -ComObject "Word.Application"
    $doc=$word.documents.Add("O:\fd_ict\50_Service_betreiben\501_Operation_Management\5012_Service_Desk\Diverses\Q_ServiceDesk_Bereinigung\Vorlage.dotx")
    $word.Visible=$false
    $selection=$word.Selection

    $selection.Font.size=21
    $selection.Font.Bold=$True
    $selection.TypeText("Bericht der Bereinigung von Q:\Service_Desk")
    $selection.TypeParagraph()
    $selection.Font.size=10
    $selection.Font.Bold=$False
    $selection.TypeText("Erstellt von Ramon Marti")
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.Font.size=12
    $stringUsername = $env:UserName
    $stringUsername.toUpper()
    $selection.TypeText("Der Batch wurde ausgeführt am $dateDays um $dateHours Uhr von $stringUsername .")
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.TypeText("Grösse von Q:\Service_Desk vor der Bereinigung: ")
    $selection.TypeText($sizeOld)
    $selection.TypeParagraph()
    $selection.TypeText("Grösse von Q:\Service_Desk nach der Bereinigung: ")
    $selection.TypeText($sizeNew)
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.Font.size=14
    $selection.Font.Color="wdColorGreen"
    $selection.Font.Bold=$True
    $selection.TypeText("Gelöschte Ordner/Dateien:")
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.Font.Bold=$False
    $selection.Font.size=8
    $selection.Font.Color="wdColorAutomatic"
    foreach ($folder in $deletedfolders){
    $selection.TypeText($folder)
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    }
    $selection.Font.size=14
    $selection.Font.Color="wdColorOrange"
    $selection.Font.Bold=$True
    $selection.TypeText("Diese Ordner/Dateien wurden nicht gelöscht, da sie sich in der Whitelist befinden:")
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.Font.Bold=$False
    $selection.Font.size=8
    $selection.Font.Color="wdColorAutomatic"
    foreach ($folder in $whitelistfolders){
    $selection.TypeText($folder)
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    }
    $selection.Font.size=14
    $selection.Font.Color="wdColorRed"
    $selection.Font.Bold=$True
    $selection.TypeText("Diese Ordner/Dateien konnten aufgrund von fehlenden Berechtigungen nicht gelöscht werden:")
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    $selection.Font.Bold=$False
    $selection.Font.size=8
    $selection.Font.Color="wdColorAutomatic"

    foreach ($folder in $errorfolders){
    $selection.TypeText($folder)
    $selection.TypeParagraph()
    $selection.TypeParagraph()
    }
    write-host "Done."
    write-host ""
    write-host "Speichern des Berichts als $date.pdf ..."
    write-host "Done."
    #Speichern des Journals#
    $doc.SaveAs($saveaspath, $formatPDF)
    $word.quit()
    ps winword | kill

  • #11766
    Profile photo of Dominique Hofmann
    Dominique Hofmann
    Participant

    that with the $_.LastWriteTime -lt (Get-Date).AddDays(1) was for testing and i could find the error..
    i wrote something wrong but thank you for the help

  • #11759
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    I haven't stepped through all of the code, but near the beginning, this jumps out at me. This part doesn't match your description of what the code should be doing:

    $_.LastWriteTime -lt (Get-Date).AddDays(1)
    

    That means basically "Everything that was modified before tomorrow." Unless you're somehow travelling to the future to create files, that filter will just select every item. You said you wanted to delete only folders older than 30 days, so I would expect the filter to look like this:

    $_.LastWriteTime -lt (Get-Date).AddDays(-30)
    

You must be logged in to reply to this topic.