0

I'm creating a simple script to pull a chrome extension from a user's file path, search the chrome app store for the name of the extension, and enter it into the csv:

# Open file
$file = Import-Csv 'filepath'

# Loop through each line of CSV
foreach ($line in $file)
{       
    # Assign file path, and regex just the extension
    $path = $line.path
    $extension = $path -replace '.*\\Extensions\\'

    # Open chrome webstore for specific extension
    $result = Invoke-webrequest -Uri "https://chrome.google.com/webstore/detail/$extension" -Method Get
    $resultTable = @{}  
    
    # Grab title of extension and place in CSV
    $title = $result.ParsedHtml.title
    Add-Content -Path 'filepath' -Value "$path,$extension,$title"
    
    # Create table and return as an object
    $resultTable.$extension = $title
    Write-Output $resultTable
}

Doing this works fine, but appends the results to the bottom of the table instead of in it's neighbouring column, as shown: enter image description here

How would I go about simply placing the output into the fields beside, instead of adding to the bottom?

Any help would be much appreciated, and thank you in advance.

EDIT: To make things a little clearer. The file originally looks like this:

enter image description here

My desired output would be to have the extension, and title in the same row, but neighbouring columns, as such:

enter image description here

1
  • Slightly different, but thank you. I've added an edit to the original post to clarify what I'm asking for Commented Nov 6, 2020 at 21:30

2 Answers 2

1

Since you are using Import-Csv to read your file, which I assume is a proper CSV, you can use Export-Csv to output custom objects.

# Open file
$file = Import-Csv 'filepath'

# Loop through each line of CSV
# store results in $resultTable
$resultTable = foreach ($line in $file)
{       
    # Assign file path, and regex just the extension
    $path = $line.path
    $extension = $path -replace '.*\\Extensions\\'

    # Open chrome webstore for specific extension
    $result = Invoke-WebRequest -Uri "https://chrome.google.com/webstore/detail/$extension" -Method Get
    
    # Grab title of extension and place in CSV
    $title = $result.ParsedHtml.title
    
    # Create and output custom object with path,extension,title properties
    [pscustomobject]@{
        Path = $path
        Extension = $extension
        Title = $title
    }
}
# Export to CSV overwriting 'filepath'
$resultTable | Export-Csv 'filepath' -NoType

Export-Csv converts an input object into a CSV. Each property of the object becomes a column and each property value is output under those respective columns. Each object becomes its own row in the case of an array of objects.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly, thank you. I'll have to read up more on custom objects because that seems super useful.
1

Instead of writing the file directly, you can construct each row with [PSCustomObject]'s and then export with Export-Csv

Import-Csv 'filepath' | ForEach-Object {
    $extension = $_.path -replace '.*\\Extensions\\'
    $result = Invoke-webrequest -Uri "https://chrome.google.com/webstore/detail/$extension" -Method Get
    $title = $result.ParsedHtml.title
    [PSCustomObject]@{
        Path      = $_.path
        Extension = $extension
        Title     = $title
    }
} | Export-Csv 'filepath' -NoTypeInformation

You could also do it all with calculated properties.

$SelectProps = @(
    "Path",
    @{n="Extension";e={$_.path -replace '.*\\Extensions\\'}},
    @{n="Title";e={(Invoke-webrequest -Uri "https://chrome.google.com/webstore/detail/$($_.path -replace '.*\\Extensions\\')" -Method Get).ParsedHtml.title}}
)

Import-Csv 'filepath' |
    Select-Object $SelectProps |
        Export-Csv 'filepath' -NoTypeInformation

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.