1

I need to modify the preferences file for Chrome for users.

This is the wanted outcome:

Wanted outcome Image

This is what I have so far, but it's failing:

get-childitem -path "$env:systemdrive\Users" | foreach-object {

  $path = "$env:systemdrive\Users\$_\appdata\local\google\chrome\user data\default\preferences"

  if(test-path $path){
    $prefs = get-content $path | convertfrom-json 
    if($prefs){

      if(!($prefs.protocol_handler.excluded_schemes.alfaoffline.swe)){
        $prefs.protocol_handler.excluded_schemes.alfaoffline | add-member -name "swe" -value false -MemberType NoteProperty #-Force
      } else {
        if(!($prefs.protocol_handler.excluded_schemes.alfaoffline.swe -match "false")){
          write-host "Alfaoffline swe not set to false"
          $prefs.protocol_handler.excluded_schemes.alfaoffline.swe = $prefs.protocol_handler.excluded_schemes.alfaoffline.swe + ":false"
        } else {
          write-host "alfaoffline already set to false, ignoring."
        }
      }
      $prefs | convertto-json -depth 100 | out-file $path -encoding utf8
    }
  }
}

Error that I receive:

PS C:\WINDOWS\system32> C:\Temp\Chrome_Alfa.ps1
Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Temp\Chrome_Alfa.ps1:11 char:64
+ ... faoffline | add-member -name "swe" -value false -MemberType NotePrope ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-Member], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand
Pingolin
  • 3,161
  • 6
  • 25
  • 40
Karl W
  • 11
  • 2
  • The error message is telling you that `$prefs.protocol_handler.excluded_schemes.alfaoffline` evaluates to `$null`, and you can't add a member to `$null`. – mklement0 Dec 09 '19 at 18:41

2 Answers2

0

You are double entering the C:\Users Path. Also The parent level JSON object needs to be created first. This can be done by using a PSCustomObject as the Value. Change to this;

get-childitem -path "$env:systemdrive\Users" | foreach-object {

  $path = "$_\appdata\local\google\chrome\user data\default\preferences"

  if(test-path $path){
    $prefs = get-content $path | convertfrom-json
    if($prefs){

      if(!($prefs.protocol_handler.excluded_schemes.alfaoffline.swe)){
        $prefs.protocol_handler.excluded_schemes | add-member -membertype NoteProperty -name "alfaoffline" -value ([PSCustomObject]@{swe=$false})
      } else {
        if(!($prefs.protocol_handler.excluded_schemes.alfaoffline.swe -match "false")){
          write-host "Alfaoffline swe not set to false"
          $prefs.protocol_handler.excluded_schemes.alfaoffline.swe = $prefs.protocol_handler.excluded_schemes.alfaoffline.swe + ":false"
        } else {
          write-host "alfaoffline already set to false, ignoring."
        }
      }
      $prefs | convertto-json -depth 100 | out-file $path -encoding utf8
    }
  }
}
Kirk
  • 13
  • 4
  • If the file dosen't have the "excluded_schemes.alfaoffline.swe" how do i iterate through that hierarchy and validate each "." step of that hierarchy – Karl W Dec 12 '19 at 08:44
0

I think your original path address is correct. But it looks like you're going to need to iterate through that hierarchy and validate each "." step of that hierarchy:

$prefs.protocol_handler.excluded_schemes.alfaoffline.swe

check for existence, create it if it doesn't exist, continue. I looked to see if there was a built-in -Force command of some kind that would create the whole hierarchy if you try to create a member, but I don't see one.

Once you have the context set correctly, this is a great solution for the actual add you want to accomplish:

Proper formating of JSON using powershell

Feel free to comment or correct my assumptions and I'll be glad to help further.

adamt8
  • 308
  • 1
  • 7
  • Yes, that's the issue i'm facing. I only have the protocol_handler but i need to create excluded_schemes.alfaoffline.swe if it's not there. I'm not sure how to do it. – Karl W Dec 10 '19 at 10:56