0
votes

i need a script that delete a multiple folder, when windows services was stopped. 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. 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:

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

Thx a lot.

2
"By start the script must to ask that write a name a service" -- This should be posted as a separate question (if an existing question that answers the problem cannot be found), so are the rules on StackOverflow. See How to Ask. Hint: Use the -split operator. - zett42
this >>> "$las-path\temp\*" <<< is apparently wrong. i think you need an underscore where you have a hyphen. - Lee_Dailey

2 Answers

0
votes

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:

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. This can be wildcard patterns like Name*.

From these services we select all services that are not stopped, using the Where-Object cmdlet. The while loops condition will be fullfilled, as long as the pipeline returns something, meaning at least one service is not stopped. If all services are stopped, then Where-Object won't output anything and the while loop exits.

The Sleep 3 (seconds) is there so we don't use more system resources than necessary. The 3 doesn't have a special significance, you could use 1 or 5, it doesn't really matter. Just don't busy-loop, to give the system a chance to do other things while your script is waiting.

0
votes

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.