Welcome !

 Code always pulling "else"

Author Message

  • Total Posts : 1
  • Scores: 0
  • Reward points : 0
  • Joined: 11/28/2016
  • Status: offline
Code always pulling "else" Monday, November 28, 2016 11:32 AM (permalink)
I am trying to write a portion of our log on script that maps a shortcut if they are in the correct AD group, but if they are no longer in that group, it deletes the shortcut.

' Call Recorder Viewer Shorcut
With wshNetwork
For Each objGroup in colGroups              
    If LCase( objGroup.Name ) = LCase( "IT Helpdesk" ) Then
        blnMember = True
        Set objShellWScript = WScript.CreateObject("WScript.Shell")
usersDesktop = objShellWScript.SpecialFolders("Desktop")
            Set objShortCut = objShellWScript.CreateShortcut(usersDesktop & "\Shoretel Call Recorder.lnk")
                    objShortCut.TargetPath = "http://xxxx/STPSCallRecorderPlayer/Login.aspx?ReturnUrl=%2fSTPSCallRecorderPlayer%2fdefault.aspx"
                    objShortCut.Description = "Employment Status Change"
                    objShortCut.IconLocation = "C:\Windows\system32\SHELL32.dll,185"
Set Shell = CreateObject("WScript.Shell") 
Set FSO = CreateObject("Scripting.FileSystemObject") 
DesktopPath = Shell.SpecialFolders("Desktop") 
shortcut = DesktopPath & "\Shoretel Call Recorder.lnk"
If FSO.FileExists(shortcut) Then FSO.DeleteFile shortcut
    End if 
End With

If I use the first half, I am fine and it works, but as soon as I add the "else" statement, it will always no matter what I do run the else statement instead of the first portion.

Any ideas?

<message edited by duskhunter on Monday, November 28, 2016 11:33 AM>

    • Total Posts : 2626
    • Scores: 253
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:Code always pulling "else" Monday, November 28, 2016 12:37 PM (permalink)
    You're iterating over all groups the user has, and remove the shortcut whenever a group does not match. Hence, unless "IT Helpdesk" is the very last group in the user's group list, the shortcut is always removed even if it gets created somewhere along the way. Add an Exit For at the end of the Then branch to avoid this.

    A somewhat more elegant approach would be to change a boolean variable inside the loop if the user is member of the helpdesk group, and add/remove the shortcut in a second step depending on that boolean variable.
    isHelpdeskStaff = False
    For Each objGroup in colGroups
      If LCase(objGroup.Name) = "it helpdesk" Then
        isHelpdeskStaff = True
        Exit For
      End If
    If isHelpdeskStaff Then
      'create shortcut
      'remove shortcut
    End If

    A significantly more elegant approach would be to handle the shortcuts via Group Policy Preferences.
      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2017 ASPPlayground.NET Forum Version 3.9