简体   繁体   English

用 Powershell 删除多个文件夹

[英]Delete multiple folder with Powershell

i need a script that delete a multiple folder, when windows services was stopped.当 windows 服务停止时,我需要一个删除多个文件夹的脚本。 I writed this scrit:我写了这个脚本:

[Array] $Services = 'LAS_LMB','LAS_LWS','LAS_CORE';

$las_path = "D:"
$tomcat_path = @('tomcat_core', 'tomcat_lmb', 'tomcat_web')
$log_folder = "$las_path\tomcat_*\logs"
$temp_folder = "$las-path\temp\*"
$localhost_folder = "$las_path$tomcat_path\work\Catalina\localhost"

# loop through each service, if its stopped, delete some folders
foreach($ServiceName in $Services)
{
    $arrService = Get-Service -Name $ServiceName
    
    if ($arrService.Status -eq 'Stopped')
    {
        Start-Sleep -s 5
        Remove-Item $localhost_folder, $temp_folder -Force -Recurse -Verbose
        Get-ChildItem -Path  $log_folder -Recurse -include * | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item -Verbose
        if ($arrService.Status -eq 'Running')
            {
              Write-Host "To delete Logs, Temporary Datas etc. must all serveices be stopped. Please start script Stopservices.ps1 first"
            }
     }
 }

When i started the script, the skript check that service is stopped, but only one.当我启动脚本时,脚本检查服务是否停止,但只有一个。 I need when all of services are stoped, to delete the folders.我需要在所有服务停止时删除文件夹。 For example 1. service -and 2.service -and 3.service.例如 1.service -and 2.service -and 3.service。 I don't know how to use this funktion: By start the script must to ask that write a name a service.我不知道如何使用这个功能:通过启动脚本必须要求写一个名称服务。 For example: $Dienste = Read-Host "Please enter the Services" I write las1, las2, las3 and this values must be enter in:例如: $Dienste = Read-Host "Please enter the Services" 我写的是 las1, las2, las3,这个值必须输入:

[Array] $Services = $Dienste in this format 
[Array] $Services = 'las1','las2','las3';

Thx a lot.多谢。

I need when all of services are stoped, to delete the folders.我需要在所有服务停止时删除文件夹。

Get-Service accepts an array of service names, so you could do the following to wait for all services to be stopped: Get-Service接受一组服务名称,因此您可以执行以下操作来等待所有服务停止:

while( Get-Service $Services | Where-Object Status -ne 'Stopped' ) { Sleep 3 }

# Now delete the folders...

The Get-Service call retrieves all services that are defined in the $Services array. Get-Service调用检索在$Services数组中定义的所有服务。 This can be wildcard patterns like Name* .这可以是通配符模式,例如Name*

From these services we select all services that are not stopped, using the Where-Object cmdlet.从这些服务中,我们使用Where-Object cmdlet select 所有未停止的服务。 The while loops condition will be fullfilled, as long as the pipeline returns something , meaning at least one service is not stopped.只要管道返回somethingwhile循环条件就会被满足,这意味着至少一个服务没有停止。 If all services are stopped, then Where-Object won't output anything and the while loop exits.如果所有服务都停止,那么Where-Object将不会 output 任何东西并且while循环退出。

The Sleep 3 (seconds) is there so we don't use more system resources than necessary. Sleep 3 (秒)在那里,所以我们不会使用比必要更多的系统资源。 The 3 doesn't have a special significance, you could use 1 or 5 , it doesn't really matter. 3没有特殊意义,你可以使用15 ,这并不重要。 Just don't busy-loop, to give the system a chance to do other things while your script is waiting.只是不要忙循环,让系统在脚本等待时有机会做其他事情。

Thank you for explanation.谢谢你的解释。 I wrote this.我写了这个。 It's a syntaxis a right writed?它的语法是正确的吗?

[Array] $Services = 'service1','service2','service3';

$las_path = "D:\LAS"
$tomcat_path = "$las_path\tomcat_*"
$log_folder = "$las_path\tomcat_*\logs"
$temp_folder = "$las-path\temp\*"
$localhost_folder = "$las_path\tomcat_path\work\Catalina\localhost"

# loop through each service, if its stopped, delete some folders
foreach($ServiceName in $Services)
{
    $arrService = Get-Service -Name $ServiceName
    while( Get-Service $Services | Where-Object Status -ne 'Stopped') {Sleep 3 }
{
        Remove-Item $localhost_folder, $temp_folder -Force -Recurse -Verbose
        Get-ChildItem -Path  $log_folder -Recurse -include * | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item -Verbose
        
        if ($arrService.Status -eq 'Running')
            {
              Write-Host "To delete Logs, Temporary Datas etc. must all serveices be stopped. Please start script Stopservices.ps1 first"
            }
    
    }
}

Thx.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM