Problem deploying cmd script with snapin



  • Hi,

    I create a snapin to set a shortcut for some computers in “C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp”, which requires administrator privilege.
    I found a way to add some line to execute this cmd script with administrator privileges, and it works like a charm when I launch it form my computer.
    But when I deploy this script with fog snapin in another computer, snapin seems to deploy (o error in fog.log) but shortcut don’t appear…
    Any idea ?
    Here is the script (to clone display between monitor and videoprojector on teacher computers in classroom)

    REM --add the following to the top of your bat file--
    
    @echo off
    
    :: BatchGotAdmin
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        set params = %*:"=""
        echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
        exit /B
    
    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    
    @echo off
    Cls
    set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
    echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
    echo sLinkFile = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\Clone.lnk" >> %SCRIPT%
    echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
    echo oLink.TargetPath = "%windir%\System32\DisplaySwitch.exe" >> %SCRIPT%
    echo oLink.Arguments = "/clone" >> %SCRIPT%
    echo oLink.Save >> %SCRIPT%
    cscript /nologo %SCRIPT%
    del %SCRIPT%
    

    And the result in fog.log

    ------------------------------------------------------------------------------
    ---------------------------------SnapinClient---------------------------------
    ------------------------------------------------------------------------------
     14/09/2016 11:18 Client-Info Client Version: 0.11.5
     14/09/2016 11:18 Client-Info Client OS:      Windows
     14/09/2016 11:18 Client-Info Server Version: 1.3.0-RC-10
     14/09/2016 11:18 Middleware::Response Success
     14/09/2016 11:18 SnapinClient Snapin Found:
     14/09/2016 11:18 SnapinClient     ID: 1381
     14/09/2016 11:18 SnapinClient     Name: Clone
     14/09/2016 11:18 SnapinClient     Created: 2016-09-14 11:17:34
     14/09/2016 11:18 SnapinClient     Action: 
     14/09/2016 11:18 SnapinClient     Pack: False
     14/09/2016 11:18 SnapinClient     Hide: False
     14/09/2016 11:18 SnapinClient     Server: 
     14/09/2016 11:18 SnapinClient     TimeOut: 0
     14/09/2016 11:18 SnapinClient     RunWith: c:\windows\system32\cmd.exe
     14/09/2016 11:18 SnapinClient     RunWithArgs: /c
     14/09/2016 11:18 SnapinClient     Args: 
     14/09/2016 11:18 SnapinClient     File: Clone2.cmd
     14/09/2016 11:18 Middleware::Communication Download: http://192.168.10.60/fog/service/snapins.file.php?mac=E8:40:F2:3B:C7:AA&taskid=1381
     14/09/2016 11:18 SnapinClient C:\Program Files (x86)\FOG\tmp\Clone2.cmd
     14/09/2016 11:18 Bus {
      "self": true,
      "channel": "Notification",
      "data": "{\r\n  \"title\": \"Installing Clone\",\r\n  \"message\": \"Please do not shutdown until this is completed\"\r\n}"
    }
     14/09/2016 11:18 Bus Emmiting message on channel: Notification
     14/09/2016 11:18 SnapinClient Starting snapin...
     14/09/2016 11:18 SnapinClient Snapin finished
     14/09/2016 11:18 SnapinClient Return Code: 0
     14/09/2016 11:18 Bus {
      "self": true,
      "channel": "Notification",
      "data": "{\r\n  \"title\": \"Clone Installed\",\r\n  \"message\": \"Installation has finished and is now ready for use\"\r\n}"
    }
     14/09/2016 11:18 Bus Emmiting message on channel: Notification
     14/09/2016 11:18 Middleware::Communication URL: http://192.168.10.60/fog/service/snapins.checkin.php?taskid=1381&exitcode=0&mac=E8:40:F2:3B:C7:AA&newService&json
    ------------------------------------------------------------------------------
    


  • Thanks for help, finally I created a snapin which add registry key to duplicate display when someone log in, and if needed after deploying image I use Win + P key.


  • Senior Developer

    @Matthieu-Jacquart a couple things. SYSTEM sometimes won’t have the TEMP environment all variable set (yes, I know that’s weird). I also see you are using a batch script to offload to a vbs. Try removing that additional complexity by sticking to one language. Either write it in just batch or just vbs.

    Also, if its a snap in you can remove all of your UAC checks to further limit potentional points of failure.

    But as said before, it may just simply be that SYSTEM can’t clone displays. In which case you could use a snap in to write to a login script that gets run everytime a user logs in.


  • Moderator

    @Matthieu-Jacquart Windows Key + P is the shortcut to switch display mode, works on login screen too.

    Not sure about how to force it using snapins though.



  • @Quazz Oh, so it will be problematic…
    I absolutely need to force clone display on these computers, if not after deploying image principal screen is videoprojector so people can’t log on computer…


  • Moderator

    Fairly certain the problem is that Snapins run under the System account, which cannot interact with the desktop, and thus you can’t “elevate” to administrator.



  • Some other test :

    • double-clicking on script “Clone2.cmd” in my computer create shortcut
    • launching command “c:\windows\system32\cmd.exe /C Clone2.cmd” on my computer create shortcut
    • BUT deploying fog snapin with command “c:\windows\system32\cmd.exe /C Clone2.cmd” failed to create shortcut…

Log in to reply
 

497
Online

39003
Users

10720
Topics

101771
Posts

Looks like your connection to FOG Project was lost, please wait while we try to reconnect.