question

IvayloStefanov avatar image
0 Votes"
IvayloStefanov asked ·

Powershell delete folder

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
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IvayloStefanov avatar image
0 Votes"
IvayloStefanov answered ·

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



service.jpg (166.5 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

My mistake. The conditional statement omitted the Get-Service cmdlet. I corrected the code I previously posted to include the necessary code.

0 Votes 0 ·
IvayloStefanov avatar image
0 Votes"
IvayloStefanov answered ·

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



stopped-services.jpg (372.5 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.