Assistance improving Batch/PS script

chipsexperts
chipsexperts used Ask the Experts™
on
Hi all,

First of all I'm don't know a lot about scripting and that's the reason I'm here requesting some assistance. So here I go:

We do have a GPO that runs a logon script (batch file that then runs a PS1). The whole purpose is to create shortcuts on the folder redirected desktop of such users (on a Citrix server environment) if they belong to those groups.
The problem we are facing is that sometimes the shortcut will not get created. Based on troubleshooting it seems this script is not the most efficient way to accomplish this task and basically the reason why it doesn't get created is because it times-out. Every day we get calls that the shortcut was not created and most of the time running a GPUPDATE or login the user off/on corrects the problem.

So here I'm asking is a better way to accomplish this task? Please let me know.

REM ---------------
REM -Version Check-
REM ---------------
echo f |XCOPY "\\company-file1\apps\ClientApp\CA\rtclient.exe" "%appdata%\Clientapp\CA\caclient.exe" /D /y /f /e
echo f |XCOPY "\\company-file1\apps\ClientApp\CA\ClientAppUpgradeFix.bat" "%appdata%\ClientApp\CA\ClientAppUpgradeFix.bat" /D /y /f /e
REM -------------------
REM -End Version Check-
REM -------------------

Powershell.exe -executionpolicy remotesigned -file \\company.corp\SysVol\company.corp\Policies\{F144FEB7-BEEA-49C8-B421-3A0F1A089776}\User\Scripts\Logon\UserValidate-Profile.ps1


It then runs that PS1 powershell

Import-Module ActiveDirectory
$username = [Environment]::UserName
$group = "ClientAppProdIcon"
$CurrentDetails = Get-ADUser -Identity $username
$userDN = $CurrentDetails.DistinguishedName
$folderredirect = "\\company.corp\profiles\folderredirection"
$appdata = $env:APPDATA
$rtapppath = "\\company-file1\apps\ClientApp"

$membercorp = Get-ADGroupMember -Identity "ProfileCorp" -Recursive | Select -ExpandProperty DistinguishedName
If ($membercorp -contains $userDN) {
                $folderredirect = "\\ppt-profilecorp\folderredirection$"
}
$memberct = Get-ADGroupMember -Identity "ProfileAA" -Recursive | Select -ExpandProperty DistinguishedName
If ($memberct -contains $userDN) {
                $folderredirect = "\\ppt-profileAA\folderredirection$"
}
$memberindia = Get-ADGroupMember -Identity "ProfileBB" -Recursive | Select -ExpandProperty DistinguishedName
If ($memberindia -contains $userDN) {
                $folderredirect = "\\ppt-profileBB\folderredirection$"
}
$memberli = Get-ADGroupMember -Identity "ProfileCC" -Recursive | Select -ExpandProperty DistinguishedName
If ($memberli -contains $userDN) {
                $folderredirect = "\\ppt-profileCC\folderredirection$"
}
$membernj = Get-ADGroupMember -Identity "ProfileDD" -Recursive | Select -ExpandProperty DistinguishedName
If ($membernj -contains $userDN) {
                $folderredirect = "\\ppt-profileDD\folderredirection$"
}
$membernyc = Get-ADGroupMember -Identity "ProfileEE" -Recursive | Select -ExpandProperty DistinguishedName
If ($membernyc -contains $userDN) {
                $folderredirect = "\\ppt-profileEE\folderredirection$"
}
$memberNE = Get-ADGroupMember -Identity "ProfileFF" -Recursive | Select -ExpandProperty DistinguishedName
If ($memberNE -contains $userDN) {
                $folderredirect = "\\ppt-profileFF\folderredirection$"
}

$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty DistinguishedName
    If ($members -contains $userDN) {
      If (test-path $appdata\ClientApp\CA) {
      echo f| XCOPY "\\company-file1\apps\ClientApp\CA\caclient.exe" "$appdata\ClientApp\CA\caclient.exe" /D /y /f /e
      echo f| XCOPY "\\company-file1\apps\ClientApp\CA\ClientAppUpgradeFix.bat" "$appdata\ClientApp\CA\ClientAppUpgradeFix.bat" /D /y /f /e
            } Else {
      ROBOCOPY $rtapppath\CA $appdata\ClientApp\CA /r:0 /w:0 /E /SEC /SECFIX /XO
      ROBOCOPY $rtapppath\CA_ENV $appdata\ClientApp\CA_ENV /r:0 /w:0 /E /SEC /SECFIX /XO
      }
      If (test-path "$folderredirect\$username\desktop\ClientApp_Prod.lnk" ) {
            } Else {
      $WshShell = New-Object -ComObject WScript.Shell
      # This is where the shortcut will be created
      $Shortcut = $WshShell.CreateShortcut("$folderredirect\" + $username + "\Desktop\ClientApp_Prod.lnk")

      # This is the program the shortcut will open
      $Shortcut.TargetPath = "$appdata\ClientApp\CA\caclient.exe"

      # This is any extra parameters that the shortcut may have. For example, opening to a google.com when internet explorer opens
      $Shortcut.Arguments = "/host:company.clientapp.com /config:ClientApp_Live"

      # This is the working directory
      $Shortcut.WorkingDirectory = "$appdata\ClientApp\CA"

      # This command will save all the modifications to the newly created shortcut.
      $Shortcut.Save()
}

 } Else {
      If (test-path "$folderredirect\$username\desktop\ClientApp_Prod.lnk" ) {
      Remove-item -path "$folderredirect\$username\desktop\ClientApp_Prod.lnk"
      }

}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Adam BrownSenior Systems Admin
Top Expert 2010

Commented:
You can try deploying the icons with a group policy preference. https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/dn581922(v%3Dws.11) explains the concepts, but you basically just need to set up a policy that deploys the icons to the users but is targeted to only apply on the Citrix desktops.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial