Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am trying to get the meta data from a directory and I am getting an error that A positional parameter cannot be found that accepts argument '\'. Not sure how to correct this?

$FileMetadata = Get-FileMetaData -folder (Get-childitem $Folder1 + "\" + $System.Name + "\Test" -Recurse -Directory).FullName
like image 367
user1342164 Avatar asked Aug 07 '14 12:08

user1342164


People also ask

What is a positional parameter in PowerShell?

A positional parameter requires only that you type the arguments in relative order. The system then maps the first unnamed argument to the first positional parameter. The system maps the second unnamed argument to the second unnamed parameter, and so on. By default, all cmdlet parameters are named parameters.

What are the positional parameters?

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0 . Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command.

How do you declare a parameter in PowerShell?

Long description. The name of the parameter is preceded by a hyphen ( - ), which signals to PowerShell that the word following the hyphen is a parameter name. The parameter name and value can be separated by a space or a colon character. Some parameters do not require or accept a parameter value.


2 Answers

You need to do the concatenation in a subexpression:

$FileMetadata = Get-FileMetaData -folder (Get-childitem ($Folder1 + "\" + $System.Name + "\Test") -Recurse -Directory).FullName

or embed the variables in a string like this:

$FileMetadata = Get-FileMetaData -folder (Get-childitem "$Folder1\$($System.Name)\Test" -Recurse -Directory).FullName
like image 156
Ansgar Wiechers Avatar answered Sep 21 '22 17:09

Ansgar Wiechers


The most robust way in Powershell to build a path when parts of the path are stored in variables is to use the cmdlet Join-Path.

This also eliminate the need to use "\".

So in your case, it would be :

$FoldersPath = Join-Path -Path $Folder1 -ChildPath "$System.Name\Test"

$FileMetadata = Get-FileMetaData -folder (Get-ChildItem $FoldersPath -Recurse -Directory).FullName
like image 35
Mathieu Buisson Avatar answered Sep 17 '22 17:09

Mathieu Buisson