looking for automation of disk clean up on selected VM's when needed, without manually logging into VM in different subscriptions

Venkat karidi 81 Reputation points Microsoft Vendor
2022-11-22T17:10:01.647+00:00

Hi,iam looking for automation of disk clean up on selected VM's when needed, without manually reaching Virtual machines.

Presently, we had 50+ vm's that we need to do disk clean up(temp file removal) manually by logging into all vm's which taking lot of time sometimes exhaustive. looking for any script process that can be loaded onto vm's and can place task schedule to run this

Please help with powershell scripting onthis .it will be so helpful.

Thanking you

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,559 questions
0 comments No comments
{count} votes

Accepted answer
  1. Manu Philip 17,351 Reputation points MVP
    2022-11-22T18:26:04.453+00:00

    If your Virtual machines are of Windows operating system, you may setup a scheduled task in each VM to clean up the temp files in regular intervals. An example procedure I saw in TechNet Wiki as follows:
    36429.schedule-task-to-empty-windows-temp-folder.aspx

    ----------

    --please don't forget to upvote and Accept as answer if the reply is helpful--

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Venkat karidi 81 Reputation points Microsoft Vendor
    2022-11-25T06:52:11.137+00:00

    Hi Philip,

    when iam running disk cleanup script it is showing error cannot access the path while deleting temporary files ,manually i can perform in folder.
    script:-

    Set-Location “C:\abcd\abcdBTHostAgent\system32”
    Remove-Item * -recurse -force

    one more i have tried it also shows same error

    Variables ####

    $objShell = New-Object -ComObject Shell.Application   
    $objFolder = $objShell.Namespace(0xA)   
    
    $temp = get-ChildItem "env:\system32"   
    $temp2 = $temp.Value      
    
    $WinTemp = "C:\abcd\abcdBTHostAgent\system32"   
    

    Remove temp files located in "C:\abcd\abcdBTHostAgent\system32"

    write-Host "Removing Junk files in $temp2." -ForegroundColor Magenta    
    Remove-Item -Recurse  "$temp2\*" -Force -Verbose   
    
     write-Host "Clean Up Task Finished !!!"  
    
    End of the Script #####

    can i know what is the issue in script and how to overcome the access issue
    do i need to add any cmdlet to override it

    Thanks much