Kaseya Community

Script to cleanup old windows profiles

  • SInce Windows creates temp user profiles on a regular basis, I am looking for a way to cleanup the growing mess on each XP & W7 machine.

     

     IE:

    C:\Documents and Settings\admin.PC120.000

    C:\Documents and Settings\admin.PC120.001

    C:\Documents and Settings\admin.PC120.002

    C:\Documents and Settings\admin.PC120.003

    C:\Documents and Settings\admin.PC120.004

    ----

    C:\Documents and Settings\admin.MyDomain.000

    C:\Documents and Settings\admin.MyDomain.001

    C:\Documents and Settings\admin.MyDomain.002

    C:\Documents and Settings\admin.MyDomain.003

    C:\Documents and Settings\admin.MyDomain.004

    I have tried :

    del "C:\Documents and Settings\admin.*.0*" /f /q /s and variations there of, but the scripts will not allow wild cards.

    Also, the pathing uses a DOT instead of any slashes. Not sure if that plays into the problem.

    Does anyone have a handle on how deal with the PCname/DOMAINNAME variable?

     

  • Procedure Fix Current User Profile.xml

    You can use Windows environmental variables in most agent procedure steps.  To find the Domain of the Current User it would be %userdomain%.  You could also query the Registry, possibly.  I'd have to dig around for the specific Key to read.

    I deal mainly with a non-domain kiosk environment, so the script I have (attached) doesn't take the Domain into account.  In a Workgroup or stand alone Windows system the temp profile names would be %username%.%computername% instead of %username%.%userdomain%.  Oddly, the %userdomain% for a Workgroup computer is WORKGROUP.  Windows seems to treat the %computername% as the Domain for some things, and not for others.

    Anyways I've attached the script I use.  Its a bit complicated.  Part of this is due to needing the SID of the user in question, as extra Registry Keys are made when a temp profile is loaded as well.

    <?xml version="1.0" encoding="utf-8"?>
    <ScriptExport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.kaseya.com/vsa/2008/12/Scripting">
    <Procedure name="Fix Current User Profile" treePres="3" id="296401573" folderId="61472212451291624815329252">
    <Body description="NOTE: You must be logged in to the user account that has the blown user profile.&#xA;&#xA;phewson 3/21/2012">
    <If description="">
    <Condition name="UserIsLoggedIn">
    <Parameter xsi:type="StringParameter" name="UserName" value="" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Querying WMIC for current user SID." />
    </Statement>
    <Statement description="Use WMIC to retrieve the SID of the current user." name="ExecuteShellCommand" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Command" value="WMIC USERACCOUNT WHERE NAME='%USERNAME%' GET SID &gt;&gt; #vAgentConfiguration.agentTempDir#\sid1.txt" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="WMIC can sometimes take a bit to run, so pause the procedure until it completes." name="PauseScript" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="IntegerParameter" name="Seconds" value="30" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Converting WMIC's output from Unicode to ASCII for parsing..." />
    </Statement>
    <Statement description="Convert the output from WMIC from Unicode to ASCII so FOR can parse the text file." name="ExecuteShellCommand" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Command" value="TYPE #vAgentConfiguration.agentTempDir#\sid1.txt &gt;&gt; #vAgentConfiguration.agentTempDir#\sid2.txt" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Parsing WMIC's converted output to retrieve the current user SID." />
    </Statement>
    <Statement description="Parse the converted WMIC output to strip out everything but the actual SID." name="ExecuteShellCommand" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;skip=1 tokens=1 delims= &quot; %S IN (#vAgentConfiguration.agentTempDir#\sid2.txt) DO @ECHO (.|SET /P=&quot;%S&quot;) &gt;&gt; #vAgentConfiguration.agentTempDir#\sid3.txt" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Set the SID to a named procedure variable." name="GetVariable" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="EnumParameter" name="VariableType" value="FileContent" />
    <Parameter xsi:type="StringParameter" name="SourceContent" value="#vAgentConfiguration.agentTempDir#\sid3.txt" />
    <Parameter xsi:type="StringParameter" name="VariableName" value="usersid" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: The current user's SID is #usersid#." />
    </Statement>
    <Statement description="Create a named procedure variable and assign a value retrieved from the managed machine by the agent." name="GetVariable" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="EnumParameter" name="VariableType" value="ConstantValue" />
    <Parameter xsi:type="StringParameter" name="SourceContent" value="#vMachine.LoginName#" />
    <Parameter xsi:type="StringParameter" name="VariableName" value="username" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: The current user is '#username#'." />
    </Statement>
    <Statement description="If the wrong profile is found in the ProfileImagePath, set the bad location to a named procedure variable." name="GetVariable" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="EnumParameter" name="VariableType" value="RegistryValue" />
    <Parameter xsi:type="StringParameter" name="SourceContent" value="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#\ProfileImagePath" />
    <Parameter xsi:type="StringParameter" name="VariableName" value="userprofile" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: The ProfileImagePath Registry Key for the current user is set to '#userprofile#'." />
    </Statement>
    <Statement description="Make sure UPHClean is installed on Windows XP so all hooks to the Registry are released on Logoff." name="ExecuteScript" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="ScriptID" value="732286212" />
    <Parameter xsi:type="StringParameter" name="ScriptName" value="UPHClean 1.6d" />
    <Parameter xsi:type="StringParameter" name="TimeDelay" value="" />
    <Parameter xsi:type="EnumParameter" name="TimeUnit" value="Immediate" />
    </Statement>
    <Statement description="Execute the specified procedure. - NOTE: this procedure does not complete until the called procedure completes." name="ExecuteScript" continueOnFail="false">
    <Parameter xsi:type="StringParameter" name="ScriptID" value="623957549" />
    <Parameter xsi:type="StringParameter" name="ScriptName" value="Logoff Current User" />
    <Parameter xsi:type="StringParameter" name="TimeDelay" value="" />
    <Parameter xsi:type="EnumParameter" name="TimeUnit" value="Immediate" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Verifying OS and testing the current value of the ProfileImagePath key with the expected value." />
    </Statement>
    <If description="Verify that the OS is Windows 7 before running the following commands.">
    <Condition name="CheckVariable">
    <Parameter xsi:type="StringParameter" name="VariableName" value="#vMachine.OSType#" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="Equals" />
    <Parameter xsi:type="StringParameter" name="Value" value="7" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: OS is Windows 7." />
    </Statement>
    <If description="Check the ProfileImagePath setting for the correct profile value.">
    <Condition name="CheckRegistryValue">
    <Parameter xsi:type="StringParameter" name="RegistryPath" value="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#\ProfileImagePath" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="NotEquals" />
    <Parameter xsi:type="StringParameter" name="Value" value="C:\Users\#username#" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Comment" value="Warning: The user profile is blown! The ProfileImagePath Registry Key does not match the expected value!" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Correcting the ProfileImagePath Registry Key." />
    </Statement>
    <Statement description="Set the ProfileImagePath to the correct user profile." name="ExecuteShellCommand" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Command" value="REG ADD &quot;HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#&quot; /V ProfileImagePath /T REG_EXPAND_SZ /D &quot;C:\Users\#username#&quot; /F" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Removing temporary profiles." />
    </Statement>
    <Statement description="Clean up the bad profile from the system." name="ExecuteShellCommand" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;TOKENS=* DELIMS= &quot; %D IN ('DIR &quot;C:\Users&quot; /B /AD ^| find &quot;TEMP&quot;') DO (RMDIR /S /Q &quot;C:\Users\%D&quot;)" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Clean up the bad profile from the system." name="ExecuteShellCommand" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;TOKENS=* DELIMS= &quot; %D IN ('DIR &quot;C:\Users&quot; /B /AD ^| find &quot;#username#.&quot;') DO (RMDIR /S /Q &quot;C:\Users\%D&quot;)" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    </Then>
    <Else>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="7">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: The user profile is not blown. The ProfileImagePath Registry Key matches the expected value." />
    </Statement>
    </Else>
    </If>
    </Then>
    </If>
    <If description="Verify that the OS is Windows Vista before running the following commands.">
    <Condition name="CheckVariable">
    <Parameter xsi:type="StringParameter" name="VariableName" value="#vMachine.OSType#" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="Equals" />
    <Parameter xsi:type="StringParameter" name="Value" value="Vista" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: OS is Windows Vista." />
    </Statement>
    <If description="Check the ProfileImagePath setting for the correct profile value.">
    <Condition name="CheckRegistryValue">
    <Parameter xsi:type="StringParameter" name="RegistryPath" value="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#\ProfileImagePath" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="NotEquals" />
    <Parameter xsi:type="StringParameter" name="Value" value="C:\Users\#username#" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Comment" value="Warning: The user profile is blown! The ProfileImagePath Registry Key does not match the expected value!" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Correcting the ProfileImagePath Registry Key." />
    </Statement>
    <Statement description="Set the ProfileImagePath to the correct user profile." name="ExecuteShellCommand" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Command" value="REG ADD &quot;HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#&quot; /V ProfileImagePath /T REG_EXPAND_SZ /D &quot;C:\Users\#username#&quot; /F" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Removing temporary profiles." />
    </Statement>
    <Statement description="Clean up the bad profile from the system." name="ExecuteShellCommand" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;TOKENS=* DELIMS= &quot; %D IN ('DIR &quot;C:\Users&quot; /B /AD ^| find &quot;#username#.&quot;') DO (RMDIR /S /Q &quot;C:\Users\%D&quot;)" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Clean up the bad profile from the system." name="ExecuteShellCommand" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;TOKENS=* DELIMS= &quot; %D IN ('DIR &quot;C:\Users&quot; /B /AD ^| find &quot;TEMP&quot;') DO (RMDIR /S /Q &quot;C:\Users\%D&quot;)" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    </Then>
    <Else>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="Vista">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: The user profile is not blown. The ProfileImagePath Registry Key matches the expected value." />
    </Statement>
    </Else>
    </If>
    </Then>
    </If>
    <If description="Verify that the OS is Windows 7 before running the following commands.">
    <Condition name="CheckVariable">
    <Parameter xsi:type="StringParameter" name="VariableName" value="#vMachine.OSType#" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="Equals" />
    <Parameter xsi:type="StringParameter" name="Value" value="XP" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: OS is Windows XP." />
    </Statement>
    <If description="Check the ProfileImagePath setting for the correct profile value.">
    <Condition name="CheckRegistryValue">
    <Parameter xsi:type="StringParameter" name="RegistryPath" value="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#\ProfileImagePath" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="NotEquals" />
    <Parameter xsi:type="StringParameter" name="Value" value="&quot;%SystemDrive%\Documents and Settings\&quot;#username#" />
    </Condition>
    <Then>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Comment" value="Warning: The user profile is blown! The ProfileImagePath Registry Key does not match the expected value!" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Correcting the ProfileImagePath Registry Key." />
    </Statement>
    <Statement description="Set the ProfileImagePath to the correct user profile." name="ExecuteShellCommand" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Command" value="REG ADD &quot;HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#&quot; /V ProfileImagePath /T REG_EXPAND_SZ /D &quot;%SystemDrive%\Documents and Settings\#username#&quot; /F" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Removing temporary profiles." />
    </Statement>
    <Statement description="Clean up the bad profile from the system." name="ExecuteShellCommand" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;TOKENS=* DELIMS= &quot; %D IN ('DIR &quot;C:\Documents and Settings&quot; /B /AD ^| find &quot;#username#.&quot;') DO (RMDIR /S /Q &quot;C:\Documents and Settings\%D&quot;)" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    </Then>
    <Else>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="XP">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: The user profile is not blown. The ProfileImagePath Registry Key matches the expected value." />
    </Statement>
    </Else>
    </If>
    </Then>
    </If>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Testing for backup Registry Keys and removing them." />
    </Statement>
    <If description="Check for any backup instances of the user's profile keys.">
    <Condition name="TestRegistryKey">
    <Parameter xsi:type="StringParameter" name="Path" value="HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#.bak" />
    <Parameter xsi:type="EnumParameter" name="Condition" value="Exists" />
    </Condition>
    <Then>
    <Statement description="Remove any backup instances of the user's registry keys." name="ExecuteShellCommand" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Command" value="REG DELETE &quot;HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\#usersid#.bak&quot;/F" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    </Then>
    </If>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Cleaning up WMIC's output files used to created procedure variables." />
    </Statement>
    <Statement description="Clean up text files used to create named procedure variables earlier." name="ExecuteShellCommand" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Command" value="FOR /F &quot;TOKENS=* DELIMS= &quot; %F IN ('DIR &quot;#vAgentConfiguration.agentTempDir#&quot; /B ^| find &quot;sid&quot;') DO (DEL #vAgentConfiguration.agentTempDir#\%F&quot; /F /Q&quot;)" />
    <Parameter xsi:type="EnumParameter" name="ExecuteAccount" value="System" />
    <Parameter xsi:type="BooleanParameter" name="Is64Bit" value="False" />
    </Statement>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Informational: Issuing reboot." />
    </Statement>
    <Statement description="Reboot the machine this procedure is running on. - WARNING: Reboot must be the last step in a procedure or the procedure will start again from step 1 after the reboot." name="Reboot" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008" />
    </Then>
    <Else>
    <Statement description="Write an Entry into the Procedure Log" name="WriteScriptLogEntry" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="Comment" value="Warning: No user is logged in! Re-scheduling Agent Procedure &quot;#scriptIdTab.scriptName#&quot; and notifying admin #adminDefaults.adminName#." />
    </Statement>
    <Statement description="Schedule a procedure to run on a specified machine." name="ScheduleScript" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="ScriptName" value="296401573" />
    <Parameter xsi:type="StringParameter" name="TimeDelay" value="15" />
    <Parameter xsi:type="StringParameter" name="MachineID" value="" />
    </Statement>
    <Statement description="Send an email from the server (address specified in the Configure function in the System module)." name="SendEmail" continueOnFail="false" osType="NT4|2000|XP|2003|Vista|2008">
    <Parameter xsi:type="StringParameter" name="To" value="#adminDefaults.adminEmail#" />
    <Parameter xsi:type="StringParameter" name="Subject" value="Blown User Profile on #vMachine.machName#" />
    <Parameter xsi:type="StringParameter" name="Body" value="Hello #adminDefaults.adminName#,&#xA;&#xA;The computer #vMachine.machName# running Windows #vMachine.OSType# #vMachine.OSInfo# has a blown user profile.&#xA;&#xA;In order for the Agent Procedure #scriptIdTab.scriptName# to run successfully, please make sure the system is logged in to the user account with the blown profile.&#xA;&#xA;Regards, &#xA;&#xA;The KServer" />
    </Statement>
    </Else>
    </If>
    </Body>
    </Procedure>
    </ScriptExport>

    Basically what the procedure does is query WMIC (Windows Management Instrumentation Command-line) and output its results to a text file.  We then parse the text file until we just have the SID of the Current User and throw it into a variable.  Then I grab the Current User name and throw that into a variable as well.  I'll also grab the ProfileImagePath out of the Registry and store it.

    Now that I have all the important pieces identified, we install UPHClean on Windows XP if its not already installed (this is a separate agent procedure).  This is built into Windows Vista/7 and is absolutely necessary to make sure we can delete the temporary profiles (which may be in use).  We then Log Off the Current User.  This is another separate script, all it does is execute Logoff.exe as the Current User.

    Once the system is logged out, none of the profile files should be in use.  We then clean up the Profile List in the Registry and the temporary profiles.  For Vista, 7, and XP this will be a little different (I think Vista/7 are the same actually, but I have them broken out separately).

    Once everything is complete, we cleanup the files from when we parsed the SID.

    If there is not a Current User, we schedule the script to run in 15 minutes and e-mail whomever tried running the script so they know what is up.

    This was adapted from an older batch file.  I prefer the feedback you get from Kaseya as opposed to just running .CMD/.BAT/.VBS scripts and having to debug them manually.  So much nicer to see it fail in Step X and it tell you why it failed (usually).  So forgive the Execute Shell command steps!

    If you fire up the script in Notepad++ you can Find/Replace all the instances of %computername% with %userdomain% very easily.  Hope its helpful!

  • I apparently overlooked the obvious: #vkadComputers.DomainName# would work if you have Kaseya integrated with Active Directory.  #vAgentConfiguration.workgroupDomainName# should work regardless.

  • You can use the DELPROF2 tool at helgeklein.com/.../delprof2-user-profile-deletion-tool and write a simple script to copy the file from your VSAsharedfiles to the client, then run DELPROF2 to purge all unused profiles. If you need a script, just holler and I'll post mine. It's pretty simple to put together

  • I agree, we use DelProf2 with a 30 day time limit on workstations, 180 day on servers. We also use CCleaner to remove downloads older than 30 days

  • There is also a GPO to do this