Running multiple sets of similar commands in one ps script

Welcome Forums General PowerShell Q&A Running multiple sets of similar commands in one ps script

Viewing 3 reply threads
  • Author
    Posts
    • #197390
      Participant
      Topics: 2
      Replies: 3
      Points: 28
      Rank: Member

      Hi all,

      Our current automation tool has some built-in functionality to allow us to ‘cleanup’ our NAS folders. But we now have to move to a different tool that does not have that, so I have to use Powershell. The server has only PS ver 2 at this time. So, I have about 65 locations I monitor thru the automation to cleanup folders and files based on a certain age (how old they are). The first 2 in my test run are to delete entire directories, and the 2nd 2 affect only certain files types.

      The first -WhatIf works fine showing exactly what it will delete once -WhatIf is removed.

      But on the 2nd set of commands it stumbles with:

      “Remove-Item : Cannot bind argument to parameter ‘Path’ because it is null”

      The 2 sets of commands are identical with only the actual path being different. I literally did a copy\paste and changed that one literal string. Thus I am assuming I cannot execute multiple sets of commands like this in one script. My assumption is that I must use a different approach to somehow ‘separate’ these commands.

      Script:

      $HowOld = -30
      #Path to the root folder
      $Path = “\\WDCNAQ513\NAS_Path1\Process_In”
      $LastWrite = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
      write-output $Path
      Write-Output $LastWrite
      #Delete folders older than Threshold Date
      Get-ChildItem -Path $Path | Where-Object { $_.PSIsContainer -and ($_.LastWriteTime -lt $LastWrite)} | Remove-Item -Force -Recurse -WhatIf

      $HowOld = -30
      #Path to the root folder
      $Path = “\\WDCNAQ513\NAS_Path2\Process_In”
      $LastWrite = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
      write-output $Path
      Write-Output $LastWrite
      #Delete folders older than Threshold Date
      Get-ChildItem -Path $Path | Where-Object { $_.PSIsContainer -and ($_.LastWriteTime -lt $LastWrite)} | Remove-Item -Force -Recurse -WhatIf

      $HowOld = -365
      $Path = “\\WDCNAQ513\NAS_Path3\In”
      $CreateDate = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
      write-output $Path
      Write-Output $CreateDate
      #Get the files to delete
      $filesToDelete = Get-ChildItem -Path $Path | Where-Object {$_.Name -like “*.pdf”} | Where {($_.creationtime -lt $CreateDate)}
      $filesToDelete.Count
      $filesToDelete | Remove-Item -Force -WhatIf

      $HowOld = -180
      $Path = “\\WDCNAQ513\NAS_Path3\In”
      $CreateDate = (Get-Date).AddDays($HowOld).ToString(‘MM/dd/yyyy’)
      write-output $Path
      Write-Output $CreateDate
      #Get the files to delete
      $filesToDelete = Get-ChildItem -Path $Path | Where-Object {$_.Name -like “*.log”} | Where {($_.creationtime -lt $CreateDate)}
      $filesToDelete.Count
      $filesToDelete | Remove-Item -Force -WhatIf

       

      Any assistance would be greatly appreciated.

    • #197411
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      The first thing that comes to mind is that there are no records found in your GET and then you pipe NULL to Remove. Typically, you shouldn’t just pipe from a GET to something performing an action. Something like your 3rd section:

      This approach we do a query, write output on the results and then if there is anything to process we pipe that to Remove.

    • #197441
      Participant
      Topics: 2
      Replies: 3
      Points: 28
      Rank: Member

      Thank you very much. That is a good thought. The 2nd directory was indeed empty. I can try adding that logic.

    • #197462
      Participant
      Topics: 2
      Replies: 3
      Points: 28
      Rank: Member

      That seemed to work! I also added an else statement:

      if ($filesToDelete) {
      $filesToDelete | Remove-Item -Force -WhatIf
      } else {
      Write-Output “Nothing to process”
      }

       

      Thanks again!

Viewing 3 reply threads
  • The topic ‘Running multiple sets of similar commands in one ps script’ is closed to new replies.