Reputation: 3
I'm trying to deploy our company URLs in Google Chrome as "Managed Bookmarks" using PowerShell. In order to accomplish the same thing using IE favorites I created a CSV file with 2 columns containing a Name and URL respectively. I import that CSV then I have a foreach statement that will go through each line and create a .url file in the user's favorites. To minimize effort for my staff I want to use this same CSV file for the Chrome Bookmarks so we only have one file to maintain. Then it wouldn't be necessary for us to modify and redeploy the script, and we could publish the CSV file on a network share to be updated as needed.
Chrome has a registry value that allows me to do what I need. Using 3 search engines as an example, I know how to "hard code" this and make it work.
$PolicyPath = 'Registry::HKEY_LOCAL_MACHINE\Software\Policies'
$GoogleKey = 'Google'
$ChromeKey = 'Chrome'
$ManagedBookmarks = '[ { "name": "Bing", "url": "http://www.bing.com" }, { "name": "Google", "url": "http://www.google.com" },{ "name": "Yahoo", "url": "http://www.yahoo.com" } ]'
Set-ItemProperty -Path "$($PolicyPath)\$($GoogleKey)\$($ChromeKey)" -Name 'ManagedBookmarks' -Value "$ManagedBookmarks" -Type String
Is there a way to do a foreach and concatenate the results into a single variable? That will result in the following format:
$ManagedBookmarks = "[ { "name:" "$($line.name)", "url": "$($line.url)"}, { "name:" "$($line+n.name)", "url": "$($line+n.url)"} ]"
Upvotes: 0
Views: 144
Reputation: 25001
If you have a CSV (call it csv.csv) file like the following, you can just import the CSV to create an object array and then convert the whole thing to a JSON object.
Name,URL
Bing,http://www.bing.com
Google,http://www.google.com
Yahoo,http://www.yahoo.com
$ManagedBookmarks = Import-Csv csv.csv | ConvertTo-Json
Per LotPings Recommendation, if you dislike the line feeds and/or carriage returns and extra spacing in that output, you can use the -Compress
switch.
$ManagedBookmarks = Import-Csv csv.csv | ConvertTo-Json -Compress
Upvotes: 1