dapScript()

in components/appstream-image-builder/packages/workflow-steps/lib/steps/install-dynamic-catalog-script/install-dynamic-catalog-script-logic.js [125:227]


  dapScript({ dapConfigBucketName, installerHostWorkBucketName }) {
    return `
      $currentuser = (get-wmiobject Win32_ComputerSystem).UserName.Split('\\')[1]
      Start-Transcript C:\\Users\\$currentuser\\Documents\\Logs\\log.log

      $bucket = "${dapConfigBucketName}"
      Write-Host $bucket
      $repo_bucket = "${installerHostWorkBucketName}"
      Write-Host $repo_bucket

      Add-Type -Path C:\\temp\\dyncat\\ClassLibrary.dll #Define the full path to the DAF DLL
      Add-Type -Path C:\\temp\\dyncat\\Thrift.dll #Define the full path to the Thrift DLL
      Write-Host "loaded libraries"

      #Establish a connection to the Thirft server, exposed via Named Pipes
      $transport = New-Object -TypeName Thrift.Transport.TNamedPipeClientTransport('D56C0258-2173-48D5-B0E6-1EC85AC67893')
      $protocol = New-Object -TypeName Thrift.Protocol.TBinaryProtocol($transport)
      $client = New-Object -TypeName AppStream.ApplicationCatalogService.Model.ApplicationCatalogService+Client($protocol)
      $transport.open()
      Write-Host "connected transport"

      #Get the current user's SID
      $userSid = (New-Object System.Security.Principal.NTAccount(($currentuser))).Translate([System.Security.Principal.SecurityIdentifier]).value
      Write-Host "sid: $userSid"

      $groups = Get-ADPrincipalGroupMembership -Identity $currentuser
      Write-Host $groups

      # the hardcoded fallback here is for development purposes on an image builder host.
      $stack = if (!$env:AppStream_Resource_Name) { "test1" } else { $env:AppStream_Resource_Name }
      Write-Host "stack: $stack"

      # Sort group apps into normal and magic DC lists. Choose which one to use based on whether
      # there are any normal ones. If so, use those. If none are found, fall back to magic.
      $useMagic = $true
      $normalDcs = @()
      $magicDcs = @()

      $groups | % {
        $group = $_.distinguishedName
        $path = "$stack-$group"
        Write-Host "path: $path"

        # list all dynamic catalog configurations that apply to the current group
        Get-S3Object -BucketName $bucket -Prefix $path -ProfileName appstream_machine_role | % {
          Write-Host $_.Key

          if (!($_.Key -match "magic\\|\\|\\|\\|")) {
            $useMagic = $false
            $normalDcs += $_.Key
          } else {
            $magicDcs += $_.Key
          }
        }
      }

      $dcs = @()
      if ($useMagic) {
        $dcs = $magicDcs
      } else {
        $dcs = $normalDcs
      }

      $apps = @{}

      # Fetch info for each app in each DC
      $dcs | % {
        Read-S3Object -BucketName $bucket -Key $_ -File conf.json -ProfileName appstream_machine_role
        $group = Get-Content -Path conf.json | ConvertFrom-JSON

        $group.applications | % {
          if ($apps[$_.id]) {
            Write-Host "already seen: " $_.id
          } else {
            Read-S3Object -BucketName $repo_bucket -Key $_.infoPath -File $_.id -ProfileName appstream_machine_role
            $info = Get-Content -Path $_.id | ConvertFrom-JSON
            Write-Host $info

            $iconPath = $_.iconUrl | % {$_ -replace '\\\\','/'}
            Write-Host "Fetching icon: $iconPath"
            Read-S3Object -BucketName $repo_bucket -Key $iconPath -File icon -ProfileName appstream_machine_role
            $iconData = [Convert]::ToBase64String((Get-Content -Path icon -Encoding Byte))

            $appList = New-Object -TypeName AppStream.ApplicationCatalogService.Model.Application($_.name, $_.displayName, $info.applicationExe, $iconData)

            $apps[$_.id] = New-Object -TypeName Appstream.ApplicationCatalogService.Model.AddApplicationsRequest($userSid, $appList)
          }
        }
      }

      $apps.Keys | % {
        $r = $client.AddApplications($apps[$_])
        Write-Host $r
      }

      $transport.close()

      Write-Host "added applications"

      Stop-Transcript
      Exit
    `;
  }