Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3360354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:58:56+00:00 2026-05-18T02:58:56+00:00

I’m writing a Powershell script that will extract a set of data files from

  • 0

I’m writing a Powershell script that will extract a set of data files from a ZIP file and will then attach them to a server. I’ve written a function that takes care of the unzip and since I need to grab all of the files so that I know what I’m attaching I return that from the function:

function Unzip-Files
{
    param([string]$zip_path, [string]$zip_filename, [string]$target_path, [string]$filename_pattern)

    # Append a \ if the path doesn't already end with one
    if (!$zip_path.EndsWith("\")) {$zip_path = $zip_path + "\"}
    if (!$target_path.EndsWith("\")) {$target_path = $target_path + "\"}

    # We'll need a string collection to return the files that were extracted
    $extracted_file_names = New-Object System.Collections.Specialized.StringCollection

    # We'll need a Shell Application for some file movement
    $shell_application = New-Object -com shell.Application

    # Get a handle for the target folder
    $target_folder = $shell_application.NameSpace($target_path)

    $zip_full_path = $zip_path + $zip_filename

    if (Test-Path($zip_full_path))
    {
        $target_folder = $shell_application.NameSpace($target_path)
        $zip_folder = $shell_application.NameSpace($zip_full_path)

        foreach ($zipped_file in $zip_folder.Items() | Where {$_.Name -like $filename_pattern})
        {
            $extracted_file_names.Add($zipped_file.Name) | Out-Null
            $target_folder.CopyHere($zipped_file, 16)
        }
    }

    $extracted_file_names
}

I then call another function to actually attach the database (I’ve removed some code that checks for existence of the database, but that shouldn’t affect things here):

function Attach-Database
{
    param([object]$server, [string]$database_name, [object]$datafile_names)

    $database = $server.Databases[$database_name]

    $server.AttachDatabase($database_name, $datafile_names)

    $database = $server.Databases[$database_name]

    Return $database
}

I keep getting an error though, “Cannot convert argument “1”, with value: “System.Object[]”, for “AttachDatabase” to type “System.Collections.Specialized.StringCollection””.

I’ve tried declaring the data types explicitly at various points, but that just changes the location where I get the error (or one similar to it). I’ve also changed the parameter declaration to use the string collection instead of object with no luck.

I’m starting with a string collection and ultimately want to consume a string collection. I just don’t seem to be able to get Powershell to stop trying to convert it to a generic Object at some point.

Any suggestions?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:58:57+00:00Added an answer on May 18, 2026 at 2:58 am

    It looks like you should return the names using the comma operator:

      ...
      , $extracted_file_names
    }
    

    to avoid “unrolling” the collection to its items and to preserve the original collection object.

    There were several questions like this, here is just a couple:

    Strange behavior in PowerShell function returning DataSet/DataTable

    Loading a serialized DataTable in PowerShell – Gives back array of DataRows not a DataTable

    UPDATE:

    This similar code works:

    Add-Type @'
    using System;
    using System.Collections.Specialized;
    
    public static class TestClass
    {
        public static string TestMethod(StringCollection data)
        {
            string result = "";
            foreach (string s in data)
            result += s;
            return result;
        }
    }
    '@
    
    function Unzip-Files
    {
        $extracted_file_names = New-Object System.Collections.Specialized.StringCollection
    
        foreach ($zipped_file in 'xxx', 'yyy', 'zzz')
        {
            $extracted_file_names.Add($zipped_file) | Out-Null
        }
    
        , $extracted_file_names
    }
    
    function Attach-Database
    {
        param([object]$datafile_names)
    
        # write the result
        Write-Host ([TestClass]::TestMethod($datafile_names))
    }
    
    # get the collection
    $names = Unzip-Files
    
    # write its type
    Write-Host $names.GetType()
    
    # pass it in the function
    Attach-Database $names
    

    As expected, its output is:

    System.Collections.Specialized.StringCollection
    xxxyyyzzz
    

    If I remove the suggested comma, then we get:

    System.Object[]
    Cannot convert argument "0", with value: "System.Object[]",
    for "TestMethod" to type "System.Collections.Specialized.StringCollection"...
    

    The symptoms look the same, so the solution presumably should work, too, if there are no other unwanted conversions/unrolling in the omitted code between Unzip-Files and Attach-Database calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.