You can see, that 3 services are stopped.
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?
3 answers
Sort by: Newest
-
Ivaylo Stefanov 136 Reputation points
2021-03-08T16:51:45.46+00:00 -
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.
-
Rich Matheisen 45,091 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