Quite or Restart Procedure Howto

Ask your Windows 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.
Post Reply
plautodfl
Posts: 24
Joined: Tue Apr 16, 2019 8:16 pm

Quite or Restart Procedure Howto

Post by plautodfl » Wed Jan 29, 2020 12:25 pm

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. <#
  4.  
  5. This script is a comprehensive cleaning tool that can clean one or many machines.
  6.  
  7. If you enter in a single machine name the machine will be cleaned
  8.  
  9.  
  10.  
  11. #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
  12.  
  13.            inputs into multple inputs. This has been removed in favor of a single input using the * at strategic points.
  14.  
  15. $BaseDrive - hard coded to C drive
  16.  
  17. $ComputerName - the name of the computer being cleaned.
  18.  
  19. $EmployeeID the primary user of the Computer
  20.  
  21.  
  22.  
  23. 29-JAN-2020
  24.  
  25. Work on Provision to Restart the Job or Quit
  26.  
  27.  
  28.  
  29.  
  30.  
  31. 16-JAN-2020
  32.  
  33. Add Clean SWSetup Folder - Complete
  34.  
  35. Add a Provision to Restart the Job on Another Computer or Quit
  36.  
  37. Create Individual Log Files for Each Job
  38.  
  39. Launch Windows Explorer <MachineName>\c$\users - for users to delete any user profiles
  40.  
  41.  
  42.  
  43. 13-NOV-2019
  44.  
  45. Use code from the Comprehensive Terminal Cleaner to clean
  46.  
  47. TCA, MyUser, ThinPC
  48.  
  49. Added a note that if a thin client device add svcthinpc as the primary user.
  50.  
  51.  
  52.  
  53. 09-SEP-2019
  54.  
  55. Put an notice the app has completed. Scroll Up to Review Log. Click File Exit when done.
  56.  
  57. Compiled for Release to Service Desk
  58.  
  59. Added DelProf to clean up any unused profiles
  60.  
  61. Added Get Free Disk Space to provide available disk space after cleaning.
  62.  
  63.  
  64.  
  65. 17-SEP-2019
  66.  
  67. Begin work to integrate the terminal cleaning tool into the overall computer clean tool.
  68.  
  69.  
  70.  
  71.  
  72.  
  73. 06-SEP-2019
  74.  
  75. Adding Delpfrof to clean up rogue profiles
  76.  
  77. 05-SEP-2019  
  78.  
  79. Saved V9 as V10
  80.  
  81. Put a forEach Loop in solved the problem
  82.  
  83. $deleteFiles = Get-Childitem $fullTargetPath -Recurse |
  84.  
  85.     Where {$_.LastWriteTime -lt (Get-Date).AddDays(-10)} |
  86.  
  87.     Foreach { Remove-Item $_.FullName -Force}
  88.  
  89. https://stackoverflow.com/questions/20640747/powershell-remove-item-does-not-delete-all-items
  90.  
  91.  
  92.  
  93.  
  94.  
  95. 22-Aug-2019
  96.  
  97. Cleaned up v6 saved as v8
  98.  
  99. Renaming some procedures to make them more clear.
  100.  
  101. Importing the prodecures from v7 which validate the user has a folder on c:\users\<username>
  102.  
  103.  
  104.  
  105. 28-JUN-2019
  106.  
  107. 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.
  108.  
  109. Pullin gout PSSession stuff...
  110.  
  111.  
  112.  
  113.  
  114.  
  115. 21-JUN-2019
  116.  
  117. Create and Work on Function PSSession and Enter PSSEssion - This will speed up performance for the deletes.
  118.  
  119. Modified the clean procedure name to Clean-Junk
  120.  
  121. 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.
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. 11-Jun-2019
  130.  
  131. Got Rid of the Input Function - broke each one of the three inputs down into it's own function.
  132.  
  133. Makes things much more modular.
  134.  
  135.  
  136.  
  137. 07-Jun-2019
  138.  
  139. The Function Seems to Work Correctly - however - not from within a function..
  140.  
  141. Need to Validate CleanDays Variable is a numberic value
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. #>
  152.  
  153.  
  154.  
  155.  
  156.  
  157. <#
  158.  
  159. Define Variables Here
  160.  
  161. $script:ComputerName - The computer name we are checking
  162.  
  163. $script:EmployeeId  - The Employee ID That we are checking against the computer
  164.  
  165. $script:ProfileLocation - \\Computername\c$\users\EmployeeID
  166.  
  167. $Script:PreCleanSize - Use This to Calculate the size of the area to be cleaned -
  168.  
  169. $Script:PostCleanSize - Use This To Calculate the size after cleaning -
  170.  
  171. $script:CleanAgeDays - pass this along to make sure we don't clean files less than a certain age.
  172.  
  173. $Script:session - Assigns a variable to the Remote Session so the Remote Session is easier to work with.
  174.  
  175. $script:delprofloc - adding delprof to clean up inactive windows profiles
  176.  
  177. $script:FreeDiskpsace - Get the availble Diskspace After Clean
  178.  
  179. #>
  180.  
  181.  
  182.  
  183. # Location Variables
  184.  
  185. $script:delprofloc = "\\dc1-p-a-scsmss1\portal-tools$\Citrix Utilities\WindowsCleanUpTool\delprof\"
  186.  
  187.  
  188.  
  189.  
  190.  
  191. Function PreCleanSize { $Script:PreCleanSize = "{0:N2}" -f ((Get-ChildItem -Path $Script:FolderToClean -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB) }
  192.  
  193. Function PostCleanSize { $Script:PostCleanSize = "{0:N2}" -f ((Get-ChildItem -Path $Script:FolderToClean -Recurse | Measure-Object -Property Length -Sum -ErrorAction Stop).Sum / 1MB) }
  194.  
  195.  
  196.  
  197.  
  198.  
  199. Function Get-ComputerName
  200.  
  201. {
  202.  
  203.    
  204.  
  205.     $script:ComputerName = Read-Host -Prompt "Input The Machine Name That Needs Cleaning"
  206.  
  207.     Write-Host "Checking Computer Name Against Active Directory.. Hang ..."
  208.  
  209.    
  210.  
  211.     TRY
  212.  
  213.     {
  214.  
  215.         (Get-ADComputer -Identity $script:ComputerName) |
  216.  
  217.         clear-host
  218.  
  219.         Write-Host "Congratulations Computer $script:ComputerName Successfully Found In Active Directory - Moving On"
  220.  
  221.         clear-host
  222.  
  223.         Confirm-ComputerOnline
  224.  
  225.     }
  226.  
  227.     Catch
  228.  
  229.     {
  230.  
  231.         Clear-Host
  232.  
  233.         Write-Host "Computer $script:ComputerName Not Found In Active Directory Try Again."
  234.  
  235.         Get-ComputerName
  236.  
  237.        
  238.  
  239.     }
  240.  
  241. }
  242.  
  243.  
  244.  
  245. Function Get-EmployeeID
  246.  
  247. {
  248.  
  249.    
  250.  
  251.     $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#."
  252.  
  253.            
  254.  
  255.    
  256.  
  257.     Write-Host "Checking EmployeeID Against Active Directory.. Hang ..."
  258.  
  259.    
  260.  
  261.     TRY
  262.  
  263.     {
  264.  
  265.         (Get-ADuser -Identity $script:EmployeeId)
  266.  
  267.         clear
  268.  
  269.         Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  270.  
  271.         Write-Host "Computer $script:ComputerName was online at last test."
  272.  
  273.         Write-Host "Congratulations Employee $script:EmployeeId Successfully Found In Active Directory - Moving On"
  274.  
  275.        
  276.  
  277.     }
  278.  
  279.     Catch
  280.  
  281.     {
  282.  
  283.         Clear-Host
  284.  
  285.         Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  286.  
  287.         Write-Host "Employee $script:EmployeeId Not Found In Active Directory Try Again."
  288.  
  289.         If ($script:EmployeeId -eq "tca") { break
  290.  
  291.         Match-EmployeeWithComputer }
  292.  
  293.         Get-EmployeeID
  294.  
  295.        
  296.  
  297.     }
  298.  
  299. }
  300.  
  301.  
  302.  
  303.  
  304.  
  305. function Get-CleanDays
  306.  
  307. {
  308.  
  309.     $script:CleanAgeDays = Read-Host -Prompt "Clean Files Older than (Recomended Value Between 60 and 90)"
  310.  
  311.     if ($script:CleanAgeDays -match '^\d+$') { write-host "Congratulations You Entered A Numeric Value" }
  312.  
  313.     Else
  314.  
  315.     {
  316.  
  317.         Clear-Host
  318.  
  319.         $script:CleanAgeDays = Read-Host -Prompt "Clean Files Older than (Recomended Value Between 60 and 90)"
  320.  
  321.         CleanAge-Validation
  322.  
  323.     }
  324.  
  325. }
  326.  
  327.  
  328.  
  329. Function Perform-Validation
  330.  
  331. {
  332.  
  333.     Clear-Host
  334.  
  335.     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."
  336.  
  337.     $YesNo = Read-Host -Prompt "Please confirm you want to proceed. Enter Y to continue or N to start over"
  338.  
  339.    
  340.  
  341.    
  342.  
  343.     switch ($YesNo)
  344.  
  345.     {
  346.  
  347.         "Y" {
  348.  
  349.             Write-Host "Confirming $script:EmployeeId has a user profile directory on $script:ComputerName.";
  350.  
  351.             Match-EmployeeWithComputer
  352.  
  353.            
  354.  
  355.         }
  356.  
  357.        
  358.  
  359.         "N" {
  360.  
  361.             echo "You Need To Start Over";
  362.  
  363.             Clear-Host
  364.  
  365.             Get-ComputerName
  366.  
  367.             Get-EmployeeID
  368.  
  369.             Get-CleanDays
  370.  
  371.         }
  372.  
  373.         default { echo "You Need to Enter Y or N"; Perform-Validation }
  374.  
  375.     }
  376.  
  377.    
  378.  
  379. }
  380.  
  381.  
  382.  
  383. Function Confirm-ComputerOnline
  384.  
  385. {
  386.  
  387.     clear-host
  388.  
  389.     Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  390.  
  391.     Write-Host "Checking to see if $script:ComputerName is online Testing, testing,  1...2...3...."
  392.  
  393.     TRY
  394.  
  395.     {
  396.  
  397.         (test-connection -computername $script:ComputerName -quiet)
  398.  
  399.         Clear-Host
  400.  
  401.         Write-Host "Computer $script:ComputerName Successfully Found In Active Directory"
  402.  
  403.         Write-Host "Computer $script:ComputerName is online. Moving On To The Next Step."
  404.  
  405.        
  406.  
  407.     }
  408.  
  409.     Catch
  410.  
  411.     {
  412.  
  413.         Write-Host "This Computer is Offline. Abort Script"
  414.  
  415.        
  416.  
  417.     }
  418.  
  419. }
  420.  
  421.  
  422.  
  423.  
  424.  
  425. function Match-EmployeeWithComputer
  426.  
  427. {
  428.  
  429.     $script:ProfileLocation = "\\" + $script:Computername + "\c$\users\" + $script:EmployeeId
  430.  
  431.    
  432.  
  433.     If (test-path -path $script:ProfileLocation)
  434.  
  435.     {
  436.  
  437.         Clear-Host
  438.  
  439.         Write-Host "Employee ID $script:EmployeeId has a user profile on $script:ComputerName."
  440.  
  441.         Write-Host "Time to Start Cleaning $script:Computername."
  442.  
  443.        
  444.  
  445.        
  446.  
  447.     }
  448.  
  449.     Else
  450.  
  451.     {
  452.  
  453.         clear-host
  454.  
  455.         Write-Host "Employee ID $script:EmployeeId does not Exist on the C Drive."
  456.  
  457.         Write-Host "We will not be able to clean this area as it does not exist."
  458.  
  459.         Write-Host "Please check the c:\users folder on " $script:Computername ". We will clean other areas of the computer"
  460.  
  461.     }
  462.  
  463.    
  464.  
  465. }
  466.  
  467.  
  468.  
  469.  
  470.  
  471. function Clean-Temp
  472.  
  473. {
  474.  
  475.    
  476.  
  477.     $Script:FolderToClean = "\\$Script:ComputerName\c$\temp\"
  478.  
  479.     Write-Host "Testing for a temp folder of the root of c: drive"
  480.  
  481.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  482.  
  483.             Catch { $false }))
  484.  
  485.     {
  486.  
  487.        
  488.  
  489.         #   Clear-Host
  490.  
  491.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  492.  
  493.         PreCleanSize
  494.  
  495.        
  496.  
  497.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  498.  
  499.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  500.  
  501.        
  502.  
  503.         #Updated Code to V10
  504.  
  505.         cd $Script:FolderToClean
  506.  
  507.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  508.  
  509.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  510.  
  511.         PostCleanSize
  512.  
  513.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  514.  
  515.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  516.  
  517.        
  518.  
  519.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  520.  
  521.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  522.  
  523.        
  524.  
  525.        
  526.  
  527.     }
  528.  
  529.     Else
  530.  
  531.     {
  532.  
  533.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  534.  
  535.     }
  536.  
  537. }
  538.  
  539.  
  540.  
  541. function Clean-WindowsTemp
  542.  
  543. {
  544.  
  545.    
  546.  
  547.     $Script:FolderToClean = "\\$Script:ComputerName\c$\Windows\temp\"
  548.  
  549.     Write-Host "Testing for a c:\windows\temp folder of the root of c: drive"
  550.  
  551.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  552.  
  553.             Catch { $false }))
  554.  
  555.     {
  556.  
  557.        
  558.  
  559.         #   Clear-Host
  560.  
  561.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  562.  
  563.         CD $Script:FolderToClean
  564.  
  565.         PreCleanSize
  566.  
  567.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  568.  
  569.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  570.  
  571.         #Updated Code to V10
  572.  
  573.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -recurse -ErrorAction SilentlyContinue }
  574.  
  575.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  576.  
  577.         PostCleanSize
  578.  
  579.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  580.  
  581.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  582.  
  583.        
  584.  
  585.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  586.  
  587.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  588.  
  589.        
  590.  
  591.        
  592.  
  593.     }
  594.  
  595.     Else
  596.  
  597.     {
  598.  
  599.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  600.  
  601.     }
  602.  
  603. }
  604.  
  605.  
  606.  
  607.  
  608.  
  609. function Clean-ccmcache
  610.  
  611. {
  612.  
  613.     # v10 updated with the foreach update
  614.  
  615.     $Script:FolderToClean = "\\$Script:ComputerName\c$\Windows\ccmcache\"
  616.  
  617.     Write-Host "Testing for a c:\windows\ccmcache folder of the root of c: drive"
  618.  
  619.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  620.  
  621.             Catch { $false }))
  622.  
  623.     {
  624.  
  625.        
  626.  
  627.         #   Clear-Host
  628.  
  629.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  630.  
  631.         CD $Script:FolderToClean
  632.  
  633.         PreCleanSize
  634.  
  635.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  636.  
  637.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  638.  
  639.         #Updated to V10 --- Note: Delete ALL files from this directory - so the date parameter is removed.        
  640.  
  641.         Get-Childitem $Script:FolderToClean -Recurse | Foreach { Remove-Item $_.FullName -Force -Recurse }
  642.  
  643.        
  644.  
  645.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  646.  
  647.         PostCleanSize
  648.  
  649.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  650.  
  651.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  652.  
  653.        
  654.  
  655.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  656.  
  657.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  658.  
  659.        
  660.  
  661.        
  662.  
  663.     }
  664.  
  665.     Else
  666.  
  667.     {
  668.  
  669.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  670.  
  671.     }
  672.  
  673. }
  674.  
  675.  
  676.  
  677.  
  678.  
  679. function Clean-SWSetup
  680.  
  681. {
  682.  
  683.     # v10 updated with the foreach update
  684.  
  685.     $Script:FolderToClean = "\\$Script:ComputerName\c$\SWSetup\"
  686.  
  687.     Write-Host "Testing for a c:\SWSetup folder of the root of c: drive"
  688.  
  689.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  690.  
  691.             Catch { $false }))
  692.  
  693.     {
  694.  
  695.        
  696.  
  697.        
  698.  
  699.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  700.  
  701.         CD $Script:FolderToClean
  702.  
  703.         PreCleanSize
  704.  
  705.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  706.  
  707.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  708.  
  709.         #Updated to V10 --- Note: Delete ALL files from this directory - so the date parameter is removed.        
  710.  
  711.         Get-Childitem $Script:FolderToClean -Recurse | Foreach { Remove-Item $_.FullName -Force -Recurse }
  712.  
  713.        
  714.  
  715.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  716.  
  717.         PostCleanSize
  718.  
  719.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  720.  
  721.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  722.  
  723.        
  724.  
  725.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  726.  
  727.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  728.  
  729.        
  730.  
  731.        
  732.  
  733.     }
  734.  
  735.     Else
  736.  
  737.     {
  738.  
  739.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  740.  
  741.     }
  742.  
  743. }
  744.  
  745.  
  746.  
  747.  
  748.  
  749. function Clean-usertemp
  750.  
  751. {
  752.  
  753.    
  754.  
  755.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\" + $script:EmployeeId + "\AppData\Local\Temp"
  756.  
  757.     Write-Host "Testing for a user's temp folder of the root of c: drive"
  758.  
  759.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  760.  
  761.             Catch { $false }))
  762.  
  763.     {
  764.  
  765.        
  766.  
  767.         #   Clear-Host
  768.  
  769.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  770.  
  771.         CD $Script:FolderToClean
  772.  
  773.         PreCleanSize
  774.  
  775.         Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  776.  
  777.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  778.  
  779.        
  780.  
  781.         #Updated to V10
  782.  
  783.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  784.  
  785.        
  786.  
  787.        
  788.  
  789.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  790.  
  791.         PostCleanSize
  792.  
  793.         Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  794.  
  795.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  796.  
  797.        
  798.  
  799.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  800.  
  801.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  802.  
  803.        
  804.  
  805.        
  806.  
  807.     }
  808.  
  809.     Else
  810.  
  811.     {
  812.  
  813.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  814.  
  815.     }
  816.  
  817. }
  818.  
  819.  
  820.  
  821.  
  822.  
  823. function Clean-TCAusertemp
  824.  
  825. {
  826.  
  827.    
  828.  
  829.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\tca" + "\AppData\Local\Temp"
  830.  
  831.     Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
  832.  
  833.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  834.  
  835.             Catch { $false }))
  836.  
  837.     {
  838.  
  839.        
  840.  
  841.         #   Clear-Host
  842.  
  843.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  844.  
  845.         CD $Script:FolderToClean
  846.  
  847.         PreCleanSize
  848.  
  849.         # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  850.  
  851.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  852.  
  853.        
  854.  
  855.         #Updated to V10
  856.  
  857.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  858.  
  859.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  860.  
  861.         PostCleanSize
  862.  
  863.         # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  864.  
  865.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  866.  
  867.        
  868.  
  869.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  870.  
  871.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  872.  
  873.        
  874.  
  875.        
  876.  
  877.     }
  878.  
  879.     Else
  880.  
  881.     {
  882.  
  883.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  884.  
  885.     }
  886.  
  887. }
  888.  
  889.  
  890.  
  891.  
  892.  
  893. function Clean-SVCThinpcUserTemp
  894.  
  895. {
  896.  
  897.    
  898.  
  899.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\svcthinpc" + "\AppData\Local\Temp"
  900.  
  901.     Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
  902.  
  903.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  904.  
  905.             Catch { $false }))
  906.  
  907.     {
  908.  
  909.        
  910.  
  911.         #   Clear-Host
  912.  
  913.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  914.  
  915.         CD $Script:FolderToClean
  916.  
  917.         PreCleanSize
  918.  
  919.         # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  920.  
  921.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  922.  
  923.        
  924.  
  925.         #Updated to V10
  926.  
  927.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  928.  
  929.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  930.  
  931.         PostCleanSize
  932.  
  933.         # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  934.  
  935.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  936.  
  937.        
  938.  
  939.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  940.  
  941.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  942.  
  943.        
  944.  
  945.        
  946.  
  947.     }
  948.  
  949.     Else
  950.  
  951.     {
  952.  
  953.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  954.  
  955.     }
  956.  
  957. }
  958.  
  959.  
  960.  
  961. function Clean-MyUsertemp
  962.  
  963. {
  964.  
  965.    
  966.  
  967.     $Script:FolderToClean = "\\$Script:ComputerName\c$\users\MyUser" + "\AppData\Local\Temp"
  968.  
  969.     Write-Host "Testing for a TCA user's temp folder of the root of c: drive"
  970.  
  971.     if ($(Try { Test-Path $Script:FolderToClean.trim() }
  972.  
  973.             Catch { $false }))
  974.  
  975.     {
  976.  
  977.        
  978.  
  979.         #   Clear-Host
  980.  
  981.         Write-Host "We have a $Script:FolderToClean folder. Checking the Size. This Could Take Awhile."
  982.  
  983.         CD $Script:FolderToClean
  984.  
  985.         PreCleanSize
  986.  
  987.         # Write-Host "Folder Size Before Cleaning is $script:PreCleanSize mb."
  988.  
  989.         Write-Host "Checking for files older than $script:CleanAgeDays days in $Script:FolderToClean."
  990.  
  991.        
  992.  
  993.         #Updated to V10
  994.  
  995.         Get-Childitem $Script:FolderToClean -Recurse | Where { $_.LastWriteTime -lt (Get-Date).AddDays(-$script:CleanAgeDays) } | Foreach { Remove-Item $_.FullName -Force -Recurse -ErrorAction SilentlyContinue }
  996.  
  997.         Write-Host "Checking $Script:FolderToClean Folder After Scrubbing."
  998.  
  999.         PostCleanSize
  1000.  
  1001.         # Write-Host "Folder Size After Cleaning is $script:PostCleanSize mb."
  1002.  
  1003.         $Script:NetCleanSize = $PreCleanSize - $PostCleanSize
  1004.  
  1005.        
  1006.  
  1007.         If ($PreCleanSize -eq $PostCleanSize) { Write-Host "There were no files in this folder older than $script:CleanAgeDays days. Moving On." }
  1008.  
  1009.         Else { Write-Host "You Cleaned $Script:NetCleanSize MB of Data From This Folder" }
  1010.  
  1011.        
  1012.  
  1013.        
  1014.  
  1015.     }
  1016.  
  1017.     Else
  1018.  
  1019.     {
  1020.  
  1021.         write-host "$Script:FolderToClean Path not found.  No worries! Moving On."
  1022.  
  1023.     }
  1024.  
  1025. }
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033. function Run-DelProf2
  1034.  
  1035. {
  1036.  
  1037.     #http://www.edugeek.net/forums/scripts/170719-quick-dirty-powershell-script-delprof2.html
  1038.  
  1039.     Write-Host "Removing any inactive Windows profiles from $script:ComputerName"
  1040.  
  1041.     & '\\dc1-p-a-scsmss1\portal-tools$\Citrix Utilities\WindowsCleanUpTool\delprof\DELPROF2.EXE' /u /ed:$script:EmployeeId /q /C:\\$script:ComputerName
  1042.  
  1043.     Write-Host "Inactive Profiles Removed from $script:ComputerName"
  1044.  
  1045. }
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051. Function Get-FreeDiskSpace
  1052.  
  1053. {
  1054.  
  1055.     Write-Host "Getting available disk space on $script:ComputerName"
  1056.  
  1057.     $FreediskSpace = Get-WmiObject Win32_LogicalDisk -ComputerName $script:ComputerName -Filter "DeviceID='C:'" |
  1058.  
  1059.     Foreach-Object { [Math]::Round($_.FreeSpace /1GB) }
  1060.  
  1061.     Write-Host "$script:ComputerName has $FreediskSpace gigabytes available after cleaning."
  1062.  
  1063. }
  1064.  
  1065.  
  1066.  
  1067. Function Display-FinalInstructions {
  1068.  
  1069. Write-Host "Contratulations! You have successfully cleaned $script:ComputerName. Scroll up to review the Log. "
  1070.  
  1071. Write-Host "Click File | Exit to close the application if you are satisfied with the cleanup results."
  1072.  
  1073. }
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079. Function RestartOrQuit-ComputerClean {
  1080.  
  1081. Write-Host "To clean another computer without exiting the application type Y at the prompt."
  1082.  
  1083. Write-Host "To close the application and do something else type N at the prompt."
  1084.  
  1085. $yesno = Read-Host -Prompt "Do you want to clean another computer or thin client device or Quit the Application?"
  1086.  
  1087.  
  1088.  
  1089. $yesno = switch ($quitornot) {
  1090.  
  1091.    "Y"     {"Restarting  to clean another computer."; break}
  1092.  
  1093.    "N"     {"We are quitting the application."; break}
  1094.  
  1095.    default {Write-Host "To clean another computer without exiting the application type Y and the Prompt"
  1096.  
  1097.             Write-Host "To close the application and do something else typy N" ; break}
  1098.  
  1099. }
  1100.  
  1101.  
  1102.  
  1103. $quitornot
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111. }
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117. Import-Module ActiveDirectory
  1118.  
  1119. Clear-Host
  1120.  
  1121. Get-ComputerName
  1122.  
  1123. Get-EmployeeID
  1124.  
  1125. Match-EmployeeWithComputer
  1126.  
  1127. Get-CleanDays
  1128.  
  1129. Perform-Validation
  1130.  
  1131. Clean-Temp
  1132.  
  1133. Clean-WindowsTemp
  1134.  
  1135. Clean-ccmcache
  1136.  
  1137. Clean-SWSetup
  1138.  
  1139. Clean-usertemp
  1140.  
  1141. Clean-TCAusertemp
  1142.  
  1143. Clean-SVCThinpcUserTemp
  1144.  
  1145. Clean-MyUsertemp
  1146.  
  1147. Run-DelProf2
  1148.  
  1149. Get-FreeDiskSpace
  1150.  
  1151. # Display-FinalInstructions
  1152.  
  1153. RestartOrQuit-ComputerClean
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  


DO NOT POST LICENSES, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

User avatar
brittneyr
Site Admin
Posts: 307
Joined: Thu Jun 01, 2017 7:20 am

Re: Quite or Restart Procedure Howto

Post by brittneyr » Wed Jan 29, 2020 1:23 pm

[Topic moved to Windows PowerShell forum by moderator]
Brittney Ryn
SAPIEN Technologies, Inc.

jvierra
Posts: 14155
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Quite or Restart Procedure Howto

Post by jvierra » Wed Jan 29, 2020 1:41 pm

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: 24
Joined: Tue Apr 16, 2019 8:16 pm

Re: Quite or Restart Procedure Howto

Post by plautodfl » Thu Jan 30, 2020 8:49 am

Thanks!

Post Reply