情境:
我对Powershell有点新意,我希望在这里能有所帮助。运行此脚本后,看起来Powershell会话内存泄漏。它在尝试删除第58个日志位置后停止(如果我使用批处理文件运行此脚本,则为56),并且应该总共删除93个。在做了一些研究后,我发现它是微软的一个已知错误,在使用当前会话执行后,powershell不会完全关闭命令。
目标:
目标是编写一个自动化脚本,在执行命令时删除日期早于用户输入日期的文件夹和文件(使用天数)。
test1.ps1内容:
# Gets number of days input by user as a variable
param($NumberOfDays)
If ($NumberOfDays -eq $null) {
Write-Output "Please enter number of days you want to keep logs for"
Write-Output "Example: test1.ps1 30"
Exit
}
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"
运行命令后输出“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)
问题:
我想知道如何将此dir命令发送到:
P.S。如果有比步骤1-4更好的算法,请随意添加有用的输入。
答案 0 :(得分:0)
你如何以命令运行 powershell invoke-command(http://technet.microsoft.com/en-us/library/hh849719.aspx)
查看关于在背景上执行的-AsJob的部分