Powershell delete folder

Ivaylo Stefanov 136 Reputation points
2021-03-08T11:21:49.597+00:00

Hi, i need a help with this script

generell viel mehr mit Variablen arbeiten. Also nicht ganze Pfade komplett definieren sondern eher:

[Array] $Services = 'vmicvmsession','vmictimesync','vmicrdv';

$las_path = "D:\LAS"
$tomcat_path = "$las_path\tomcat_"
$log_folder = "D:\LAS\tomcat_
\logs"
$temp_folder = "D:\LAS\temp*"
$localhost_folder = "D:\LAS\tomcat_web\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 -eq 'Stopped') 
{
    Remove-Item 'C:\Users\Ivaylo\Desktop\test123\Neuer Ordner' -Force -Recurse -Verbose

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"
        #}

}

}

The script must Delete a folders, when 3 or more services all are stopped the same time. When one is started, must write this message. -To delete Logs, Temporary Datas etc. must all serveices be stopped. Please start script Stopservices.ps1 first". And this not work for me. Can anybody help me, where i make error?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-03-08T15:26:55.517+00:00

    Is this what you want?
    EDIT: changed the conditional statement to use Get-Service

    [Array] $Services = 'vmicvmsession', 'vmictimesync', 'vmicrdv';
    #loop through each service, if all are stopped, delete some folders
    foreach ($ServiceName in $Services) {
        if ( (Get-Service $ServiceName).Status -ne "Stopped"){
            Write-Host "To delete Logs, Temporary Datas etc. must all serveices be stopped. Please start script Stopservices.ps1 first"
            exit
        }
    }
    # all three services are stopped, okay to continue
    Remove-Item 'C:\Users\Ivaylo\Desktop\test123\Neuer Ordner' -Force -Recurse -Verbose
    
    0 comments No comments

  2. Ivaylo Stefanov 136 Reputation points
    2021-03-08T16:33:08.14+00:00

    Thank you RichMatheisen for answare,

    I tested your script with this 3 Services 'vmicshutdown', 'vmictimesync', 'vmicrdv'. They are stopped. And the answare was:
    "To delete Logs, Temporary Datas etc. must all serveices be stopped. Please start script Stopservices.ps1 first".

    I want only when all services (never metter how many tgey are) are stopped, to remove the datas. When one service is running and another are stopped to write a message
    "To delete Logs, Temporary Datas etc. must all serveices be stopped. Please start script Stopservices.ps1 first"

    Thank you again.75487-service.jpg


  3. Ivaylo Stefanov 136 Reputation points
    2021-03-08T16:51:45.46+00:00

    You can see, that 3 services are stopped.
    75543-stopped-services.jpg

    0 comments No comments