1 Reply Latest reply on Feb 11, 2016 11:36 AM by dropkin

    Adding Trusted Sites from a text file

    gregf SupportEmployee
      function Add-PolicyTrustedSite
          $site = $site -replace '^\*\.'
          $valueName = '*'
          if ($site -match '(.*)\://(.*)') {
              $valueName = $Matches[1]
              $site = $Matches[2]
          $keyName = $site.Split('.')[-2] + '.' + $site.Split('.')[-1]
          $subKey = $site.Replace(".$keyName",'')
          if (($subKey -eq $keyName) -or $subKey -eq '*') {$subKey = ""}
          if ($subKey -ne "") {$subKey = "\\$subKey"}
          $newKey = 'HKCU:\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\' + $keyName + $subKey
          New-Item ($newKey) -Force
          New-ItemProperty -Path $newKey -Name $valueName -Value '2' -PropertyType "DWord" -Force
      Get-Content -Path C:\temp\urls.txt | % { Add-PolicyTrustedSite -site $_ }

      As a test I used a urls.txt file containing a bunch of differently formatted options – some with protocols, some with *, some with subdomains etc.:

      Here’s the result:

      The bulk of the code is used to split the string into protocol (http, https, or anything else before ://, defaulting to *), domain (the key to create under Domains, and subdomain (the subkey). The first thing it does is strips any superfluous *. prefix, then it makes the assumption the protocol is * unless it finds some protocol:// string at the start, then it derives the key name from the last something.somethingelse dotted couplet at the end of the string and subtracts that from the remaining string to get the subkey.