0

I got a script which should write inside the json preferences data to change the auto opening from different extensions.

The first part I got from here but this script uses ConvertFrom-Json and ConvertTo-Json which is only supported in Powershell >= 3.0.

Then I found a function that imitates this Cmdlet here. Now my problem is that it is not working at all and I don't know why, because I don't get any Errors or something.

$neededFileExt = "2er"
$path = $env:LOCALAPPDATA + "\Google\Chrome\User Data\Default\Preferences"
$prefContent = Get-Content $path -Encoding utf8

I think the next two functions are the reason why it doesn't work properly.

function ConvertTo-Json([object] $prefcontent) {
    add-type -assembly system.web.extensions
    $prefs = new-object system.web.script.serialization.javascriptSerializer
    return $prefs.Serialize($prefcontent)
}

function ConvertFrom-Json([object] $prefcontent) { 
    add-type -assembly system.web.extensions
    $prefs = new-object system.web.script.serialization.javascriptSerializer
    return , $prefs.DeserializeObject($prefcontent)
}

$prefs = ConvertFrom-Json $prefContent
if (($prefs | gm).name -contains "download") {
    if (($prefs.download | gm).name -contains "extensions_to_open") {
        if ($prefs.download.extensions_to_open) { #if it has value, grab the contents
            [string[]]$existingFileExt = 
            $prefs.download.extensions_to_open.tostring().split(":")
        }
        else {
            [string[]]$existingFileExt = $null
        }
    }
    else {
        #if extensions_to_open doesn't exist, create it
        $prefs.download | Add-Member -MemberType NoteProperty -Name extensions_to_open -Value ""
        [string[]]$existingFileExt = $null
    }
    foreach ($ext in $neededFileExt) {
        if ($existingFileExt -notcontains $ext) { #only add the necessary extension if it isn't already there
            [string[]]$existingFileExt += $ext
        }
    }
    $prefs.download.extensions_to_open = $existingFileExt -join ":" 
    ConvertTo-Json $prefs -Compress -depth 100 | Out-File $path -Encoding utf8 
}

Would appreciate any suggestions or help.

  • my question is not a duplicate of this post because i used the function from this post but my code is not relatable
kaesedipp
  • 1
  • 3
  • Welcome to StackOverflow! It is good that you added your code. However as it was formatted, it was broken because you added line breaks in comment lines. I fixed it and reformatted it. Hope it is now more readable. – Clijsters Sep 06 '18 at 13:17
  • Please be more specific about "not working at all". What does not work? Have you tried executing parts of your script to narrow down the problem? I tried most of your code and it seemed to work fine, but I have PS 5. Find out which part of your code is not giving the expected result. – marsze Sep 06 '18 at 13:24
  • To beginn with everything, worked while being in powershell 3.0 but then i needed to exchange the convertfrom-json and convertto-json into a function so im pretty sure that the other code lines are working properly. And with not working i mean that there are no file types added to the json file without giving any response back. – kaesedipp Sep 06 '18 at 13:30
  • @marsze, it shouldn't be an issue that you don't have PowerShell 2 installed. You can simulate the issue by opening a PowerShell Version 2 prompt with command `PowerShell -Version 2` – iRon Sep 06 '18 at 14:43

1 Answers1

0

PowerShell version 2 doesn't allow to expand properties directly from an object list like you do:

($prefs | gm).name -contains "download"

For PowerShell version 2 you should use the Select-Object -ExpandProperty cmdlet/parameter:

($prefs | gm | Select -ExpandProperty Name) -contains "download"
iRon
  • 20,463
  • 10
  • 53
  • 79
  • ty alot right know im not at the office i write tomorrow if it works – kaesedipp Sep 06 '18 at 17:46
  • after reediting both lines with the code that you said would not be supported by Ps2.0 , it did not give back a error as before but it still didnt write into the json file it self. – kaesedipp Sep 07 '18 at 09:12
  • It should be a matter of troubleshooting and narrowing down the issue. I suggest to use the same system for testing PSv5 and PSv2 (by using the command `PowerShell -Version 2` (to exclude that the `Preferences` file simply doesn't have `Name` property containing `download`). Then ask yourself things like: does `$prefs` contain any data? Does it pass the first `if` statement (by e.g. adding a `Write-Host 'ok'`)? Does `Write-Host $prefs.download` return anything? etc. – iRon Sep 07 '18 at 09:52