A positional parameter cannot be found that accepts argument '\*'

15,498

I think it's because you're using a sub-expression $() inside the loop, but not outside. Try these:

"$($stagingArray[$i])\*"

Surrounding it in quotes makes it evaluate the whole thing as a string.

$($stagingArray[$i]) + '\*'

This will concatenate the result of the sub-expression and the string containing the \*.

Share:
15,498
NealR
Author by

NealR

Updated on June 04, 2022

Comments

  • NealR
    NealR almost 2 years

    I have a number of web services that I'm attempting to use PowerShell to install on a DEV server. I want to include, in the script, a section that copies all files and folders from a staging area to the respective folder on the DEV server.

    In order to do this I've created two parallel string arrays - one containing the direct file paths of the staging folders and the other with the names of each folder on the DEV server (I use a couple other variables to store the rest of the directory).

    I'd like to create a for loop and simply iterate over both arrays to complete the transfer of files/folders from staging to dev server. However when the loop runs the copy-item command gives this error each time

    Copy-Item : A positional parameter cannot be found that accepts argument '*'.

    If I decide not to use the loop, the error goes away and the files transfer properly. Only a couple days new to PowerShell so I'm not sure what's going on/haven't been able to find anything specific to this on the web.

    Variables

    #web services
    $periscopeWebServices = "periscopeWebServices"
    $periscopeRetrieveComments = "periscopeRetrieveComments"
    $periscopeRetrieveGeneral = "periscopeRetrieveGeneral"
    $periscopeRetrieveMasterSubs = "periscopeRetrieveMasterSubs"
    $periscopeRetrieveProducers = "periscopeRetrieveProducers"
    $periscopeRetrieveProfiles = "periscopeRetrieveProfiles"
    $periscopeRetrieveRelationships = "periscopeRetrieveRelationships"
    $periscopeSearch = "periscopeSearch"
    $periscopeServicesArray = @($periscopeRetrieveComments, $periscopeRetrieveGeneral, 
                                $periscopeRetrieveMasterSubs, $periscopeRetrieveProducers, 
                                $periscopeRetrieveProfiles, $periscopeRetrieveRelationships, 
                                $periscopeSearch)
    
    
    #staging areas
    $stagingComments = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveComments"
    $stagingGeneral = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveGeneral"
    $stagingMasterSubs = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveMasterSubs"
    $stagingProducers = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveProducers"
    $stagingProfiles = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveProfiles"
    $stagingRelationships = "\\install\PeriscopeServices\periscopeWebServices\periscopeRetrieveRelationships"
    $stagingSearch = "\\install\PeriscopeServices\periscopeWebServices\periscopeSearch"
    $stagingArray = @($stagingComments, $stagingGeneral, $stagingMasterSubs, $stagingProducers
                      $stagingProfiles, $stagingRelationships, $stagingSearch)
    
    #server variables
    $domain = "InsideServices.dev.com"
    $directory = "E:\webcontent"
    $domainpath = "$directory\$domain"      
    

    Loop - throws error on 'copy-item'

    #copy files
    Write-Host "Copying Files" -ForegroundColor Yellow
    For ($i=0; $i -lt $periscopeServicesArray.Length; $i++)
    {
        Write-Host "Copying $($periscopeServicesArray[$i])" -ForegroundColor Yellow
        if(Test-Path -path "$domainpath\$periscopeWebServices\$($periscopeServicesArray[$i])")
        {
            copy-item -Path $($stagingArray[$i])\* -Destination $domainpath\$periscopeWebServices\$($periscopeServicesArray[$i]) -recurse -Force
        }
    }
    

    Succesful copy-item command

    copy-item -Path $stagingComments\* -Destination $domainpath\$periscopeWebServices\$periscopeRetrieveComments -recurse -Force
    
  • NealR
    NealR about 9 years
    So now it's saying that it cannot accept argument '+'. Do I need to quote the sub-expression?
  • NealR
    NealR about 9 years
    Nevermind, enclosing the variables/string in parenthesis solved the problem: ($($stagingArray[$i]) + '\*')