-----provide ad\exchange\lync\sharepoint\crm\sc\o365 and other Microsoft product implementation and outsourcing, qq:185426445. Phone 18666943750
Before opening, first say a little New year sentiment, 2010 Open Bo 5 years, the previous two years not how to write a blog, really started blogging time is 2013, when Exchange Server 2013 just came out soon, new software, new interface, new experience, of course, also bring new challenges, That is, we follow the footsteps of Microsoft engineers to follow the footsteps of the Giants again, from Exchange Server 2003, to the back of the 2007,2010, in the latest 2013,it people are more painful is this, and always continue to learn, and constantly replenish themselves. Busy and full, of course, today's efforts are also for tomorrow's greater success, hope and everyone to share together.
At that time saw a lot of fragmented Exchange Server 2013 deployment and architecture documents, feel compared to the forest chaos, not too systematic, patchwork, and then came up with an idea, I want to put Exchange Server 2013 the entire deploy and architecture into a series, To those who need to learn, at that time, good memory than rotten pen, write down after their own use of time is also can learn from, write a blog is a principle is truth-seeking, truth, and then build a set of basic and real production environment the same scene, functions are basically the same, the basic is reference to TechNet, And the content of some authoritative articles in foreign languages. Blog post, slowly also have a lot of users to find me consulting related issues, but also in the group to see that exchange operation is watching my blog slowly learned, this I feel very gratified, knowledge is used to communicate and spread, I also sincerely hope that the blog can be more people see, learn, Of course, also want to hear the reader's voice and reply, rather than just praise.
At the end of 2014, I moved on to the idea of writing the PowerShell feature series blog, as it is well known that PowerShell is being used in all of the new Microsoft mainstream products, and that management is basically done through PowerShell, Then began to write a management series of PowerShell, to record some of their own management process of a bit. Nonsense to speak so much, the following began to formally talk about the PowerShell operation periodically delete obsolete files.
1. Unblock the system against PowerShell script execution
Because the PowerShell script in the default system does not have sufficient permissions to run, we need to manually modify and give it permission to perform the following steps: Control Panel-Administrative Tools-Windows PowerShell Modules
PS C:\Users\administrator. Contoso> Get-executionpolicy
RemoteSigned
PS C:\Users\administrator. Contoso> Set-executionpolicy remotesigned
Executing policy changes
Execution policies can prevent you from executing untrusted scripts. Changing the execution policy may cause you to face the security risks described in the About_execution_policies Help topic. Do you want to change the execution policy?
[Y] is (y) [n] No (n) [s] Hangs (s) [?] Help (default = "Y"):
PS C:\Users\administrator. Contoso>
This allows the system to disable the PowerShell script execution.
2. Write a PowerShell script, open the PowerShell ISE Script Editing Tool, Windows Server 2008r2 system, add the required functionality to the Server Manager---feature "Windows PowerShell integrated Scripting environment (ISE )"。
Create a new text file named "Delete expired file. PS1" The content is as follows:
echo "The following expired files will be deleted: "
$all = get-childitem "C:\files backup"
#替换这里的 "C:\files Backup" actually stores the directory for your backup files, and if you want to find content within the subfolders of all the directories, you need to modify the
#$all = get-childitem "C:\files backup" -recurse
foreach ($delete in $all)
{
$deldate = ((get-date)-$delete. LastWriteTime). Days
if ($deldate-gt 180-and $delete. Psiscontainer-ne $True)
#替换这里的180天为你需要保留的实际天数, and exclude folders that are included
{
$delete. Delete () #删除过期文件
Echo $delete #显示删除文件
}
}
650) this.width=650; "title=" QQ picture 20150209095532.png "alt=" wkiol1tyfjicbvu5aalqubnfl1u190.jpg "src="/http S3.51cto.com/wyfs02/m01/59/8b/wkiol1tyfjicbvu5aalqubnfl1u190.jpg "/>
3. Use Windows Scheduled tasks to execute scripts on a regular basis [specific Settings method reference blog : collect server daily reports with PowerShell, and send messages to Administrators, HTTP.// yuntcloud.blog.51cto.com/1173839/1605561]
Control Panel-Administrative Tools-Task Scheduler-Create tasks
The configuration is as follows:
General: Enter name, description-tick "Run regardless of user login"-Enter password-tick "run with maximum privileges"
Trigger: New-Select "Schedule"-Select execution time as "7:30:00"-select the execution period such as "Every 1 days per day"-tick "enable", or you can choose to run the script every hour, every half hour or higher as needed.
Action: New-select "Start Program"-"powershell-file" C: \ To delete expired files. PS1 " -click" OK "
650) this.width=650; "title=" QQ picture 20150209095532.png "alt=" wkiol1tyfyuy7r5waaeal1fv1aw407.jpg "src="/http S3.51cto.com/wyfs02/m00/59/8b/wkiol1tyfyuy7r5waaeal1fv1aw407.jpg "/>
At this point, the scheduled task is configured, and if you want to test whether the scheduled task is performing properly, run it immediately, manually, right away.
This article is from the "Zhou Ping Microsoft Technology Exchange Platform" blog, please be sure to keep this source http://yuntcloud.blog.51cto.com/1173839/1612891
PowerShell Management Series (17) PowerShell operation timing Delete obsolete files