Quite or Restart Procedure Howto

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
plautodfl
Posts: 30
Last visit: Thu Jun 09, 2022 6:09 am

Quite or Restart Procedure Howto

Post by plautodfl »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product: PowerShell Studio 2020 (64 Bit)
Build: v5.7.172
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.17763.0
*** Please add details and screenshots as needed below. ***

I have a script that upon completion the users want the option to start over from the top.
I am working on the procedure RestartOrQuit-ComputerClean at the last line of code.
From ordinary ISE, I can just call the file name to start over...
What is the code to get the script to start over if a user selects run again?
  1.  
  2. <#
  3. This script is a comprehensive cleaning tool that can clean one or many machines.
  4. If you enter in a single machine name the machine will be cleaned
  5.  
  6. #InputOne - either the machine name or HP-MXL4* If the * is used, the script will loop through all machines that match a like value. Note: the original script broke the
  7.            inputs into multple inputs. This has been removed in favor of a single input using the * at strategic points.
  8. $BaseDrive - hard coded to C drive
  9. $ComputerName - the name of the computer being cleaned.
  10. $EmployeeID the primary user of the Computer
  11.  
  12. 29-JAN-2020
  13. Work on Provision to Restart the Job or Quit
  14.  
  15.  
  16. 16-JAN-2020
  17. Add Clean SWSetup Folder - Complete
  18. Add a Provision to Restart the Job on Another Computer or Quit
  19. Create Individual Log Files for Each Job
  20. Launch Windows Explorer <MachineName>\c$\users - for users to delete any user profiles
  21.  
  22. 13-NOV-2019
  23. Use code from the Comprehensive Terminal Cleaner to clean
  24. TCA, MyUser, ThinPC
  25. Added a note that if a thin client device add svcthinpc as the primary user.
  26.  
  27. 09-SEP-2019
  28. Put an notice the app has completed. Scroll Up to Review Log. Click File Exit when done.
  29. Compiled for Release to Service Desk
  30. Added DelProf to clean up any unused profiles
  31. Added Get Free Disk Space to provide available disk space after cleaning.
  32.  
  33. 17-SEP-2019
  34. Begin work to integrate the terminal cleaning tool into the overall computer clean tool.
  35.  
  36.  
  37. 06-SEP-2019
  38. Adding Delpfrof to clean up rogue profiles
  39. 05-SEP-2019  
  40. Saved V9 as V10
  41. Put a forEach Loop in solved the problem
  42. $deleteFiles = Get-Childitem $fullTargetPath -Recurse |
  43.     Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} |
  44.     Foreach { Remove-Item $_.FullName -Force}
  45. https://stackoverflow.com/questions/20640747/powershell-remove-item-does-not-delete-all-items
  46.  
  47.  
  48. 22-Aug-2019
  49. Cleaned up v6 saved as v8
  50. Renaming some procedures to make them more clear.
  51. Importing the prodecures from v7 which validate the user has a folder on c:\users\<username>
  52.  
  53. 28-JUN-2019
  54. Setup Clean-Junk function to accept parameters.- need to decide if hard code paths to check.. or use a txt file to read and loop through.
  55. Pullin gout PSSession stuff...
  56.  
  57.  
  58. 21-JUN-2019
  59. Create and Work on Function PSSession and Enter PSSEssion - This will speed up performance for the deletes.
  60. Modified the clean procedure name to Clean-Junk
  61. Modified the procedure to be able to accept inputs. I have some other code that does this - Need to see if I can get it to work here.
  62.  
  63.  
  64.  
  65. 11-Jun-2019
  66. Got Rid of the Input Function - broke each one of the three inputs down into it's own function.
  67. Makes things much more modular.
  68.  
  69. 07-Jun-2019
  70. The Function Seems to Work Correctly - however - not from within a function..
  71. Need to Validate CleanDays Variable is a numberic value
  72.  
  73.  
  74.  
  75.  
  76. #>
  77.  
  78.  
  79. <#
  80. Define Variables Here
  81. $script:ComputerName - The computer name we are checking
  82. $script:EmployeeId  - The Employee ID That we are checking against the computer
  83. $script:ProfileLocation - \\Computername\c$\users\EmployeeID
  84. $Script:PreCleanSize - Use This to Calculate the size of the area to be cleaned -
  85. $Script:PostCleanSize - Use This To Calculate the size after cleaning -
  86. $script:CleanAgeDays - pass this along to make sure we don't clean files less than a certain age.
  87. $Script:session - Assigns a variable to the Remote Session so the Remote Session is easier to work with.
  88. $script:delprofloc - adding delprof to clean up inactive windows profiles
  89. $script:FreeDiskpsace - Get the availble Diskspace After Clean
  90. #>
  91.  
  92. # Location Variables
  93. $script:delprofloc = "\\dc1-p-a-scsmss1\portal-tools$\Citrix Utilities\WindowsCleanUpTool\delprof\"
  94.  
  95.  
  96. Function PreCleanSize { $Script:PreCleanSize = "{0:N2}" -f ((Get-ChildItem -Path $Script:FolderToClean -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB) }
  97. Function PostCleanSize { $Script:PostCleanSize = "{0:N2}" -f ((Get-ChildItem -Path $Script:FolderToClean -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB) }
  98.  
  99.  
  100. Function Get-ComputerName
  101. {
  102.    
  103.     $script:ComputerName = Read-Host -Prompt "Input The Machine Name That Needs Cleaning"
  104.     Write-Host "Checking Computer Name Against Active Directory.. Hang ..."
  105.    
  106.     TRY
  107.     {
  108.         (Get-ADComputer -Identity $script:ComputerName) |
  109.         clear-host
  110.         Write-Host "Congratulations Computer $script:ComputerName Successfully Found In Active Directory - Moving On"
  111.         clear-host
  112.         Confirm-ComputerOnline
  113.     }
  114.     Catch
  115.     {
  116.         Clear-Host
  117.         Write-Host "Computer $script:ComputerName Not Found In Active Directory Try Again."
  118.         Get-ComputerName
  119.        
  120.     }
  121. }
  122.  
  123. Function Get-EmployeeID
  124. {
  125.    
  126.     $script:EmployeeId = Read-Host -Prompt "Input The EmployeeID of Primary User Of $script:ComputerName Computer. Note: If a thin client device enter SVCTHINPC. If unsure of the primary user enter your E#."
  127.            
  128.    
  129.     Write-Host "Checking EmployeeID Against Active Directory.. Hang ..."
  130.    
  131.     TRY
  132.     {
  133.         (Get-ADuser -Identity $script:EmployeeId)
  134.         clear
  135.         Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  136.         Write-Host "Computer $script:ComputerName was online at last test."
  137.         Write-Host "Congratulations Employee $script:EmployeeId Successfully Found In Active Directory - Moving On"
  138.        
  139.     }
  140.     Catch
  141.     {
  142.         Clear-Host
  143.         Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  144.         Write-Host "Employee $script:EmployeeId Not Found In Active Directory Try Again."
  145.         If ($script:EmployeeId -eq "tca") { break
  146.         Match-EmployeeWithComputer }
  147.         Get-EmployeeID
  148.        
  149.     }
  150. }
  151.  
  152.  
  153. function Get-CleanDays
  154. {
  155.     $script:CleanAgeDays = Read-Host -Prompt "Clean Files Older than (Recomended Value Between 60 and 90)"
  156.     if ($script:CleanAgeDays -match '^\d+$') { write-host "Congratulations You Entered A Numeric Value" }
  157.     Else
  158.     {
  159.         Clear-Host
  160.         $script:CleanAgeDays = Read-Host -Prompt "Clean Files Older than (Recomended Value Between 60 and 90)"
  161.         CleanAge-Validation
  162.     }
  163. }
  164.  
  165. Function Perform-Validation
  166. {
  167.     Clear-Host
  168.     Write-Host "You want to clean machine $script:ComputerName and the primary user is $script:EmployeeID and you are cleaning files older than $script:CleanAgeDays Days."
  169.     $YesNo = Read-Host -Prompt "Please confirm you want to proceed. Enter Y to continue or N to start over"
  170.    
  171.    
  172.     switch ($YesNo)
  173.     {
  174.         "Y" {
  175.             Write-Host "Confirming $script:EmployeeId has a user profile directory on $script:ComputerName.";
  176.             Match-EmployeeWithComputer
  177.            
  178.         }
  179.        
  180.         "N" {
  181.             echo "You Need To Start Over";
  182.             Clear-Host
  183.             Get-ComputerName
  184.             Get-EmployeeID
  185.             Get-CleanDays
  186.         }
  187.         default { echo "You Need to Enter Y or N"; Perform-Validation }
  188.     }
  189.    
  190. }
  191.  
  192. Function Confirm-ComputerOnline
  193. {
  194.     clear-host
  195.     Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  196.     Write-Host "Checking to see if $script:ComputerName is online Testing, testing,  1...2...3...."
  197.     TRY
  198.     {
  199.         (test-connection -computername $script:ComputerName -quiet)
  200.         Clear-Host
  201.         Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  202.         Write-Host "Computer $script:ComputerName is online. Moving On To The Next Step."
  203.        
  204.     }
  205.     Catch
  206.     {
  207.         Write-Host "This Computer is Offline. Abort Script"
  208.        
  209.     }
  210. }
  211.  
  212.  
  213. function Match-EmployeeWithComputer
  214. {
  215.     $script:ProfileLocation = "\\" + $script:Computername + "\c$\users\" + $script:EmployeeId
  216.    
  217.     If (test-path -path $script:ProfileLocation)
  218.     {
  219.         Clear-Host
  220.         Write-Host "Employee ID $script:EmployeeId has a user profile on $script:ComputerName."
  221.         Write-Host "Time to Start Cleaning $script:Computername."
  222.        
  223.        
  224.     }
  225.     Else
  226.     {
  227.         clear-host
  228.         Write-Host "Employee ID $script:EmployeeId does not Exist on the C Drive."
  229.         Write-Host "We will not be able to clean this area as it does not exist."
  230.         Write-Host "Please check the c:\users folder on " $script:Computername ". We will clean other areas of the computer"
  231.     }
  232.    
  233. }
  234.  
  235.  
  236. function Clean-Temp
  237. {
  238.    
  239.     $Script:FolderToClean = "\\$Script:ComputerName\c$\temp\"
  240.     Write-Host "Testing for a temp folder of the root of c: drive"
  241.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  242.             Catch { $false }))
  243.     {
  244.        
  245.         #   Clear-Host
  246.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  247.         PreCleanSize
  248.        
  249.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  250.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  251.        
  252.         #Updated Code to V10
  253.         cd $Script:FolderToClean
  254.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  255.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  256.         PostCleanSize
  257.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  258.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  259.        
  260.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  261.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  262.        
  263.        
  264.     }
  265.     Else
  266.     {
  267.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  268.     }
  269. }
  270.  
  271. function Clean-WindowsTemp
  272. {
  273.    
  274.     $Script:FolderToClean = "\\$Script:ComputerName\c$\Windows\temp\"
  275.     Write-Host "Testing for a c:\windows\temp folder of the root of c: drive"
  276.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  277.             Catch { $false }))
  278.     {
  279.        
  280.         #   Clear-Host
  281.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  282.         CD $Script:FolderToClean
  283.         PreCleanSize
  284.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  285.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  286.         #Updated Code to V10
  287.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -recurse -ErrorAction SilentlyContinue }
  288.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  289.         PostCleanSize
  290.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  291.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  292.        
  293.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  294.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  295.        
  296.        
  297.     }
  298.     Else
  299.     {
  300.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  301.     }
  302. }
  303.  
  304.  
  305. function Clean-ccmcache
  306. {
  307.     # v10 updated with the foreach update
  308.     $Script:FolderToClean = "\\$Script:ComputerName\c$\Windows\ccmcache\"
  309.     Write-Host "Testing for a c:\windows\ccmcache folder of the root of c: drive"
  310.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  311.             Catch { $false }))
  312.     {
  313.        
  314.         #   Clear-Host
  315.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  316.         CD $Script:FolderToClean
  317.         PreCleanSize
  318.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  319.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  320.         #Updated to V10 --- Note: Delete ALL files from this directory - so the date parameter is removed.        
  321.         Get-Childitem $Script:FolderToClean -Recurse | Foreach { Remove-Item $_.FullName -Force -Recurse }
  322.        
  323.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  324.         PostCleanSize
  325.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  326.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  327.        
  328.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  329.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  330.        
  331.        
  332.     }
  333.     Else
  334.     {
  335.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  336.     }
  337. }
  338.  
  339.  
  340. function Clean-SWSetup
  341. {
  342.     # v10 updated with the foreach update
  343.     $Script:FolderToClean = "\\$Script:ComputerName\c$\SWSetup\"
  344.     Write-Host "Testing for a c:\SWSetup folder of the root of c: drive"
  345.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  346.             Catch { $false }))
  347.     {
  348.        
  349.        
  350.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  351.         CD $Script:FolderToClean
  352.         PreCleanSize
  353.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  354.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  355.         #Updated to V10 --- Note: Delete ALL files from this directory - so the date parameter is removed.        
  356.         Get-Childitem $Script:FolderToClean -Recurse | Foreach { Remove-Item $_.FullName -Force -Recurse }
  357.        
  358.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  359.         PostCleanSize
  360.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  361.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  362.        
  363.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  364.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  365.        
  366.        
  367.     }
  368.     Else
  369.     {
  370.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  371.     }
  372. }
  373.  
  374.  
  375. function Clean-usertemp
  376. {
  377.    
  378.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\" + $script:EmployeeId + "\AppData\Local\Temp"
  379.     Write-Host "Testing for a user's temp folder of the root of c: drive"
  380.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  381.             Catch { $false }))
  382.     {
  383.        
  384.         #   Clear-Host
  385.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  386.         CD $Script:FolderToClean
  387.         PreCleanSize
  388.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  389.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  390.        
  391.         #Updated to V10
  392.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  393.        
  394.        
  395.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  396.         PostCleanSize
  397.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  398.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  399.        
  400.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  401.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  402.        
  403.        
  404.     }
  405.     Else
  406.     {
  407.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  408.     }
  409. }
  410.  
  411.  
  412. function Clean-TCAusertemp
  413. {
  414.    
  415.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\tca" + "\AppData\Local\Temp"
  416.     Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
  417.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  418.             Catch { $false }))
  419.     {
  420.        
  421.         #   Clear-Host
  422.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  423.         CD $Script:FolderToClean
  424.         PreCleanSize
  425.         # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  426.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  427.        
  428.         #Updated to V10
  429.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  430.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  431.         PostCleanSize
  432.         # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  433.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  434.        
  435.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  436.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  437.        
  438.        
  439.     }
  440.     Else
  441.     {
  442.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  443.     }
  444. }
  445.  
  446.  
  447. function Clean-SVCThinpcUserTemp
  448. {
  449.    
  450.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\svcthinpc" + "\AppData\Local\Temp"
  451.     Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
  452.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  453.             Catch { $false }))
  454.     {
  455.        
  456.         #   Clear-Host
  457.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  458.         CD $Script:FolderToClean
  459.         PreCleanSize
  460.         # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  461.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  462.        
  463.         #Updated to V10
  464.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  465.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  466.         PostCleanSize
  467.         # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  468.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  469.        
  470.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  471.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  472.        
  473.        
  474.     }
  475.     Else
  476.     {
  477.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  478.     }
  479. }
  480.  
  481. function Clean-MyUsertemp
  482. {
  483.    
  484.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\MyUser" + "\AppData\Local\Temp"
  485.     Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
  486.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  487.             Catch { $false }))
  488.     {
  489.        
  490.         #   Clear-Host
  491.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  492.         CD $Script:FolderToClean
  493.         PreCleanSize
  494.         # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  495.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  496.        
  497.         #Updated to V10
  498.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  499.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  500.         PostCleanSize
  501.         # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  502.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  503.        
  504.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  505.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  506.        
  507.        
  508.     }
  509.     Else
  510.     {
  511.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  512.     }
  513. }
  514.  
  515.  
  516.  
  517. function Run-DelProf2
  518. {
  519.     #http://www.edugeek.net/forums/scripts/170719-quick-dirty-powershell-script-delprof2.html
  520.     Write-Host "Removing any inactive Windows profiles from $script:ComputerName"
  521.     & '\\dc1-p-a-scsmss1\portal-tools$\Citrix Utilities\WindowsCleanUpTool\delprof\DELPROF2.EXE' /u /ed:$script:EmployeeId /q /C:\\$script:ComputerName
  522.     Write-Host "Inactive Profiles Removed from $script:ComputerName"
  523. }
  524.  
  525.  
  526. Function Get-FreeDiskSpace
  527. {
  528.     Write-Host "Getting available disk space on $script:ComputerName"
  529.     $FreediskSpace = Get-WmiObject Win32_LogicalDisk -ComputerName $script:ComputerName -Filter "DeviceID='C:'" |
  530.     Foreach-Object { [Math]::Round($_.FreeSpace /1GB) }
  531.     Write-Host "$script:ComputerName has $FreediskSpace gigabytes available after cleaning."
  532. }
  533.  
  534. Function Display-FinalInstructions {
  535. Write-Host "Contratulations! You have successfully cleaned $script:ComputerName. Scroll up to review the Log. "
  536. Write-Host "Click File | Exit to close the application if you are satisfied with the cleanup results."
  537. }
  538.  
  539.  
  540. Function RestartOrQuit-ComputerClean {
  541. Write-Host "To clean another computer without exiting the application type Y at the prompt."
  542. Write-Host "To close the application and do something else type N at the prompt."
  543. $yesno = Read-Host -Prompt "Do you want to clean another computer or thin client device or Quit the Application?"
  544.  
  545. $yesno = switch ($quitornot) {
  546.    "Y"     {"Restarting  to clean another computer."; break}
  547.    "N"     {"We are quitting the application."; break}
  548.    default {Write-Host "To clean another computer without exiting the application type Y and the Prompt"
  549.             Write-Host "To close the application and do something else typy N" ; break}
  550. }
  551.  
  552. $quitornot
  553.  
  554.  
  555.  
  556. }
  557.  
  558.  
  559. Import-Module ActiveDirectory
  560. Clear-Host
  561. Get-ComputerName
  562. Get-EmployeeID
  563. Match-EmployeeWithComputer
  564. Get-CleanDays
  565. Perform-Validation
  566. Clean-Temp
  567. Clean-WindowsTemp
  568. Clean-ccmcache
  569. Clean-SWSetup
  570. Clean-usertemp
  571. Clean-TCAusertemp
  572. Clean-SVCThinpcUserTemp
  573. Clean-MyUsertemp
  574. Run-DelProf2
  575. Get-FreeDiskSpace
  576. # Display-FinalInstructions
  577. RestartOrQuit-ComputerClean
  578.  
  579.  
  580.  
  581.  


DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 9:28 am
Answers: 39
Been upvoted: 30 times

Re: Quite or Restart Procedure Howto

Post by brittneyr »

[Topic moved to Windows PowerShell forum by moderator]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Quite or Restart Procedure Howto

Post by jvierra »

To rerun a selection of code place the code in a loop and prompt the user to rerun or quit.

Code: Select all

do{
     # your code
}
until('Yes' -eq [system.Windows.Forms.MessageBox]::Show('Do you wish to run this script again?','Run Again?','YesNo'-))
plautodfl
Posts: 30
Last visit: Thu Jun 09, 2022 6:09 am

Re: Quite or Restart Procedure Howto

Post by plautodfl »

Thanks!
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked