none
Detection Method Using %UserProfile% in the Path RRS feed

  • Question

  • Hello,

    I have written a simple bat file that copies 2 shortcuts to the desktop and I created an application for it so it can be run from the Application Catalog.  Everything works like it should and the shortcuts get copied to the users desktop but I can't get the detection method to work so even though the shortcuts are there the user is told the installation failed.

    I assumed I could use %UserProfile%\Desktop and then the name of the shortcut as the file name in the Detection Method but that never detects the shortcut.

    If I use the full path of C:\User\username\Desktop and the same file name it does detect it and says the installation completed successfully.

    Is it possible to use system variables such as %UserProfile% as part of the path in the detection Methods?

    Thanks,

    Dustin

    Friday, May 25, 2012 1:29 AM

Answers

  • Yeah, I wouldn't think that would work for a detection method...  The reason being that "which" profile do you want to detect on?  All of them?

    Also, when the detection method runs, if it is like everything else that SCCM does, it will run in the SYSTEM context...at which point your %UserProfile% means nothing because the system is the "logged on" user.

    So, what you might want to do is to write a file somewhere on the file system in a shared location that can be your "flag" which tells you that you've done the job on that machine.  Then check for the existence of that file (or reg key if you like or whatever). 

    Basically I'm saying ditch the very narrowly targeted detection method in favor of one that is general in nature and "machine specific" rather than user specific...if your intent is that it only happen one time per machine.

    Otherwise...if you want every user of a machine to get this change, then stop doing it in SCCM completely and use GPO Preferences.


    Mike...

    Friday, May 25, 2012 12:03 PM
  • I thought I would share my solution to this.  I run the script below in a bat file and then use C:\Users\Public\Desktop as the location for the detection method and it works great.  Also if I delete the shortcuts form my user profile desktop they also get deleted in the public folder so if the application is run again it will recopy the shortcuts.


    @echo off

    copy %0\..\"Software Center.lnk" "%AllUsersProfile%\Desktop\Software Center.lnk"

    copy %0\..\"SMPortal - Service Catalog.url" "%AllUsersProfile%\Desktop\SMPortal - Service Catalog.url"

    Thanks,

    Dustin



    • Edited by D. Elliott Saturday, May 26, 2012 1:37 AM
    • Marked as answer by D. Elliott Saturday, May 26, 2012 1:37 AM
    Saturday, May 26, 2012 1:36 AM

All replies

  • Yeah, I wouldn't think that would work for a detection method...  The reason being that "which" profile do you want to detect on?  All of them?

    Also, when the detection method runs, if it is like everything else that SCCM does, it will run in the SYSTEM context...at which point your %UserProfile% means nothing because the system is the "logged on" user.

    So, what you might want to do is to write a file somewhere on the file system in a shared location that can be your "flag" which tells you that you've done the job on that machine.  Then check for the existence of that file (or reg key if you like or whatever). 

    Basically I'm saying ditch the very narrowly targeted detection method in favor of one that is general in nature and "machine specific" rather than user specific...if your intent is that it only happen one time per machine.

    Otherwise...if you want every user of a machine to get this change, then stop doing it in SCCM completely and use GPO Preferences.


    Mike...

    Friday, May 25, 2012 12:03 PM
  • Mike, thanks for the reply.  I actually orginally set it up so that when the 2 shortcuts were copied to the users desktop it would also create a text file in a c:\sccm_flags which works but the problem with that is 2 fold.  Someone could delete the shortcuts and then want to reinstall them but since the text file didn't get deleted they couldn't run the install again.  Also, if more than one users uses that same computer the second user won't be able to run the install either.

    I haven't tried putting the shortcuts in the Default profile desktop folder yet which should allow them to show for all users profiles.  I might try that next and then I can use the actual path for the detection method.  Not sure if the icons will show up on the logged in user's desktop right away though or if that would require a log off and on first.

    Oh, I would use GPO but that is less than reliable on our global network for one reason or another.

    Thanks,

    Dustin

    Friday, May 25, 2012 1:55 PM
  • Quick comment: don't use the default user profile, use the Public user profile.

    Jason | http://blog.configmgrftw.com | Twitter @JasonSandys

    Friday, May 25, 2012 1:59 PM
  • Cool, I will try that, thanks Jason!

    Friday, May 25, 2012 2:06 PM
  • I thought I would share my solution to this.  I run the script below in a bat file and then use C:\Users\Public\Desktop as the location for the detection method and it works great.  Also if I delete the shortcuts form my user profile desktop they also get deleted in the public folder so if the application is run again it will recopy the shortcuts.


    @echo off

    copy %0\..\"Software Center.lnk" "%AllUsersProfile%\Desktop\Software Center.lnk"

    copy %0\..\"SMPortal - Service Catalog.url" "%AllUsersProfile%\Desktop\SMPortal - Service Catalog.url"

    Thanks,

    Dustin



    • Edited by D. Elliott Saturday, May 26, 2012 1:37 AM
    • Marked as answer by D. Elliott Saturday, May 26, 2012 1:37 AM
    Saturday, May 26, 2012 1:36 AM
  • just had the same problem with the %userprofile% variable. it looks like a bug. the behaviour is certainly not expected this way. if i set the the install option to "user". the detection should run in the same context like the installation. i'm pretty sure that it does actually run in this context but the expanding of the environment variable happens before the contextual execution.

    and i can even confirm that registry detection runs in the user context. hkcu (current user) gets resolved to the logged in user and not to the service account/system.

    just tried with SCCM 2012 SP1 and it's still not fixed.

    Wednesday, March 6, 2013 9:41 AM
  • I am running into the same issue, but 1) I am trying to detect Desktop Shortcuts on the All Users Desktop, and 2) I am doing so in a mixed OS environment (Win XP and Win 7).  I use the below VBS to create the Desktop Shortcuts on the All Users Desktop.  It works wonderfully.  I found that the objWshShl.SpecialFolders ("AllUsersDesktop") will resolve properly on both Win XP and Win 7 (as opposed to using %ALLUSERSPROFILE%\Desktop).

    Option Explicit
    ' On Error Resume Next '
    ' Declare & Set Variables '
    	Dim objWshShl : Set objWshShl = WScript.CreateObject ("WScript.Shell")
    	Dim strDskFld : strDskFld     = objWshShl.SpecialFolders ("AllUsersDesktop")
    	Dim objShtCut
    ' Main Body '
    	Set objShtCut = objWshShl.CreateShortcut (strDskFld & Chr(92) & "Google.lnk")
    	With objShtCut
    		.TargetPath   = "http://www.google.com/"
    		.Description  = "Google"
    		.IconLocation = objWshShl.CurrentDirectory & Chr(92) & "Google.ico, 0"
    		.WindowStyle  = 1
    		.Save
    	End With

    However, when it comes to the Detection Method, I cannot simply Configure rules to detect the presence of this deployment type because the All Users Desktop is in different locations on Win XP and Win 7.  I could create 2 different Deployment Types (one for Win XP and one for Win 7), but I feel that is unnecessary.  Instead, I want to use the below VBS to simply Use a custom script to detect the presence of this deployment type. 

    Option Explicit

    ' On Error Resume Next ' ' Declare & Set Variables ' Dim objFSO : Set objFSO = CreateObject ("Scripting.FileSystemObject") Dim objWshShl : Set objWshShl = WScript.CreateObject ("WScript.Shell") Dim strDskFld : strDskFld = objWshShl.SpecialFolders ("AllUsersDesktop") Dim objDir : Set objDir = objFSO.GetFolder (strDskFld) Dim strFilNam : strFilNam = "Google.lnk" Dim objFil ' Main Body '

            For Each objFil In objDir.Files If InStr (1, LCase (objFil.Name), LCase (strFilNam), 1) Then WScript.Quit (0) Next WScript.Quit (101)

    When I run the above VBS from a CMD Prompt, it works.  If I run it from SCCM's Detection Method, it fails.  Does anyone know why this is failing?

    --- Update ---

    I created 2 Deployment Types (one for Win XP and one for Win 7) and explicitly defined the Configure rules to detect the presence of this deployment type clause for the Win XP Deployment Type to look in the C:\Documents and Settings\All Users\Desktop directory, and the Win 7 Deployment Type to look in C:\Users\Public\Desktop directory.  Everything works!!

    It may work, but I am still feeling a bit dissatisfied because I had to create 2 Deployment Types.  I am still wondering why my VBS worked when run from a CMD Prompt, but not from SCCM.

    Thursday, May 2, 2013 5:19 PM
  • I've used powershell script-based detection methods to successfully detect files or folders within the user profile.  I couldn't use %userprofile% exactly, but here's an example that detects a path 2 folders deep under %userprofile%\appdata\roaming:

    #Detects if C:\Users\%username%\Appdata\Roaming\folder_name\file_or_subfolder_name exists
    
    Function CurrentUser{
    #CurrentUser function converts the username object string "@{username=domain\user}" 
    #         to the exact logon string "user" like the example below
    #@{username=DOMAIN\USER}
    #@{username DOMAIN\USER}
    #DOMAIN\USER}
    #DOMAIN\USER
    #DOMAIN USER
    #USER
    $loggedInUserName = get-wmiobject win32_computersystem | select username
    $loggedInUserName = [string]$loggedInUserName
    $loggedinUsername = $loggedInUserName.Split("=")
    $loggedInUserName = $loggedInUserName[1]
    $loggedInUserName = $loggedInUserName.Split("}")
    $loggedInUserName = $loggedInUserName[0]
    $loggedInUserName = $loggedInUserName.Split("\")
    $loggedInUserName = $loggedInUserName[1]
    Return $loggedInUserName
    }
    $user = CurrentUser
    
    $appPath = "C:\Users\" + $user + "\Appdata\Roaming\folder_name\file_or_subfolder_name"
    If (Test-Path $appPath) {
        Write-Host "User Appdata detected successfully!"
    }

    The trick to script-based detection is to have a script exit code 0 plus some standard output like Write-Host when the installed condition is met, while simultaneously having no error output.  But, a more helpful explanation and in-depth illustration of those concepts can be found here:  http://blog.kloud.com.au/2014/08/12/powershell-detection-method-for-sccm-2012-application-compliance-management/

    • Proposed as answer by Stephen3J Friday, March 11, 2016 8:54 PM
    Friday, March 11, 2016 8:52 PM
  • The Username value is blank when run as the local system account. Testing as a normal user works well but when deployed we get the following error when invoking 'split' action as the username does not contain any values.

    "You cannot call a method on a null-valued expression."

    PS C:\WINDOWS\system32> whoamint authority\system<o:p></o:p>

    PS C:\WINDOWS\system32> get-wmiobject win32_computersystem | select CreationClassName,username<o:p></o:p>

    CreationClassName   username<o:p></o:p>

    -----------------   --------<o:p></o:p>

    Win32_ComputerSystem        
    <o:p></o:p>



    Nick Dorak

    Thursday, April 28, 2016 2:56 PM
  • PowerShell has an environment variable "$env:LOCALAPPDATA" that will link to the current users AppData\Local folder.  Below is my custom script for detection of a rule I made that makes it a little simpler.

    $Path = "$env:LOCALAPPDATA\Trusted Sites SCCM\v1.KAL"

    If (Test-Path $Path){Write-Host "$Path Exists"}

    Friday, September 9, 2016 1:30 PM
  • $loggedInUserName = get-wmiobject win32_computersystem | select username
    $loggedInUserName = [string]$loggedInUserName
    $loggedinUsername = $loggedInUserName.Split("=")
    $loggedInUserName = $loggedInUserName[1]
    $loggedInUserName = $loggedInUserName.Split("}")
    $loggedInUserName = $loggedInUserName[0]
    $loggedInUserName = $loggedInUserName.Split("\")
    $loggedInUserName = $loggedInUserName[1]

    Just in this context (as the detection can get quite complicated) this is the short form for getting to the username:

    $loggedInUserName = $(get-wmiobject win32_computersystem).username.split("\")[1]


    This is still an issue for me on the latest SCCM release...

    Also, if you happen to be using VBScript detection (because reasons...)

    ' strComputer = "." ' Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") ' Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) ' Set colItems = GetObject("winmgmts:\\" & strComputer & "\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem",,48) ' For Each objItem in colItems ' For Each objItem in GetObject("winmgmts:\\" & strComputer & "\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem",,48) For Each objItem in GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem",,48) strUsername = Split(objItem.UserName, "\")(1) Next Wscript.Echo strUserName



    • Edited by mick.regan Monday, October 29, 2018 2:12 AM inserted codeblock
    Monday, October 29, 2018 2:11 AM
  • This solution worked perfectly for me.  Deployed a shortcut via SCCM Application.  I had tried the %UserProfile% variable to no avail.  Thank you sir!!
    Thursday, October 31, 2019 2:02 PM
  • Hey just learning all this craziness, thanks for this
    Monday, February 24, 2020 9:02 PM