Script separado de Powershell con múltiples sesiones


I'm a bit new to Powershell and I was hoping for a little help here. It looks like Powershell has a session memory leak after running this script. It stops after trying to delete the 58th log location (56 if I run this script with a batch file), and there should be 93 total up for deletion. After doing some research, I found it's a known bug with Microsoft, where powershell will not completely close out of the command after it is executed with the current session.

La meta:

The goal is to write up an automation script that will delete folders and files with an older date than what the user has input (using number of days) while executing the command.

test1.ps1 contents:

# Gets number of days input by user as a variable

If ($NumberOfDays -eq $null) {
    Write-Output "Please enter number of days you want to keep logs for"
    Write-Output "Example: test1.ps1 30"
elseif ($NumberOfDays -ge 0) {
    Write-Output "You have entered $NumberOfDays days to keep logs" 

#*Note* I use the -whatif switch to see if it will delete the folders and files without actually deleting them.
dir \\server\dir1\dir2\logs -recurse | where { ((get-date)-$_.creationTime).days -ge $NumberOfDays} | remove-item -force -recurse -whatif

Write-Output "Exiting Script"

Output After Running Command "test1.ps1 18":

What if: Performing operation "Remove Directory" on Target "\\server\dir1\dir2\logs\192716".
What if: Performing operation "Remove Directory" on Target "\\server\dir1\dir2\logs\192981".
What if: Performing operation "Remove Directory" on Target "\\server\dir1\dir2\logs\193046".
What if: Performing operation "Remove Directory" on Target "\\server\dir1\dir2\logs\193063".
What if: Performing operation "Remove Directory" on Target "\\server\dir1\dir2\logs\193065".
What if: Performing operation "Remove Directory" on Target "\\server\dir1\dir2\logs\191335".
Freezes once it gets to the 58th folder (should be a total of 93 folders)

La pregunta:

I want to know how I can get this dir command to:

  1. execute a max number of 20 delete commands
  2. run a check to see if there was more folders/files to delete
  3. if there was more folders/files to delete, close the powershell session and open a new one, rerun the dir/del command with the check.
  4. elsif no more folders/files to delete, exit script.

P.S. if there is a better algorithm than steps 1-4, please feel free to add useful input.

preguntado el 09 de septiembre de 13 a las 22:09

Why after 20 delete's open a new shell and keep deleting, why not just continue on in the current shell? -

because powershell has a memory leak and does not close out a command everytime it is executed. Please read the scenario portion of the post for an explination. -

Not Clear for me two. why do you use import-pssession ? You are removing files on a network drive. What have PSSESSION to do with that? -

sorry for the confusion, that was just 1 example of how powershell has this memory leak with executing a series of commands. I remember dealing with this same thing when I had to write stability testcases, and had to seperate the testcases. It works with telnet, but not with powershell. This is because of the memory leaks. But the server I am deleting logs on will keep telnet unavailable for security purposes. I Removed the pssession sentance to clean from the original post to clean any confusion. -

Maybe get the filelist as suggedted here will help… -

1 Respuestas

How about you run as command powershell invoke-command (

Check out the part about -AsJob which execute on background

Respondido 07 Oct 13, 10:10

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.