none
Logging function for script RRS feed

  • Question

  • Hey scripting guys. I am having an issue where some users profiles get stuck when logging out of my virtual environment. I usually have to manually remove them from the C drive and from the registry so that there domain profile will be loaded properly when they log in again. I want to automate this with a script. I have written a script but I want to add a logging function that tracks what the script removes so I can see if one is causing the problem more than others and to catch any errors that the script may have since this will be run early in the morning by a task on the server rather than by human hands. Here is what I have. Any help is very much appreciated.

    [string] $Systemprofile = "Systemprofile"
    #This string is to relate to the profile image path for the registry key for %systemroot%\system32\config\systemprofile#
    [string] $LocalService = "LocalService"
    #This string is to relate to the profile image path for the registry key for C:\Windows\ServiceProfiles\LocalService#
    [string] $NetworkService = "NetworkService"
    #This string is to relate to the profile image path for the registry key for C:\Windows\ServiceProfiles\NetworkService#
    [string] $PatternSID = 'S-1-5-21-\d+-\d+\-\d+\-\d+$'
    [string] $ProfileList = @()
    
    query user /server:$SERVER
    
    if ($username = $false) {
        [boolean] $NoUsers = $true
        }
    
    if ($NoUsers=$true) {
        Get-ChildItem -Path  'C:\users' -Recurse -exclude "thumbs.txt", "Administrator", "Default", "Public" |
        Select -ExpandProperty FullName |
        Where {$_ -notlike 'C:\users\thumbs', 'Administrator', 'Default', 'Public'} |
        sort length -Descending |
        Remove-Item -force   |
        Set-Location 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
        $ProfileList = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\*' | Where-Object { $_.PSChildName -match $PatternSID } |
        Select  @{ name = "SID"; expression = { $_.PSChildName } }
        # Get SID of ntuser.dat for all users
        foreach ($item in $ProfileList) {Remove-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$ProfileList'}}


    • Edited by Workingsamworks Monday, October 22, 2018 7:49 PM
    • Moved by Bill_Stewart Thursday, December 20, 2018 9:56 PM User should not be doing this
    Monday, October 22, 2018 7:49 PM

Answers

  • Clear as mud all the way.

    Are you trying to say you want to remove unused profiles?  Look in the Gallery for many scripts that will do that.


    \_(ツ)_/

    Monday, October 22, 2018 8:36 PM

All replies

  • This is a very bad idea.  Profiles will unload when they are ready ad not before.  If this is not happening then you have issues with your deployment.  YOu need to fix the issue and not just hack around it.

    I recommend contacting MS support for help troubleshooting this issue.  There are many reasons why this might happen.  YOr suggested solution is just foolish.


    \_(ツ)_/

    Monday, October 22, 2018 8:00 PM
  • I have in the past addressed each one as one issue at a time but it seems to always be something different. Sometimes it is chrome, sometimes adobe, or citrix, or perhaps a microsoft issue either office or part of the OS and almost everytime the answer I find is to either update the software or uninstall reinstall the software. It never seems to completely fix the issue or even slow down this problem for me. Also these are roaming profiles and are supposed to be saved to the NAS and removed from the local machine when logged out but 1 out of everyone 200 or so seems to have this problem and never finishes logging out for one of the aforementioned reasons. I have never been able to lock down one single reason this happens it seems to always be something different stopping the profile from removing out completely. 

    Also since all I am removing is the user profiles and not the servers native profiles it doesnt affect the OS its just removing the users folders it took from the NAS and the registry key for users not native to the local machine.


    _Sam_


    Monday, October 22, 2018 8:21 PM
  • Clear as mud all the way.

    Are you trying to say you want to remove unused profiles?  Look in the Gallery for many scripts that will do that.


    \_(ツ)_/

    Monday, October 22, 2018 8:36 PM
  • Ok I will try that thank you.

    _Sam_

    Tuesday, October 23, 2018 12:37 PM