Powershell options from file

has some problem when calling a function using named parameters. This function declaration is in a separate file (Security.ps1):

    function Add-SSRSItemSecurity
(
    [Parameter(Position=0,Mandatory=$false)]
    [Alias("SSRSrange")]
    [string]$range,[Parameter(Position=1,Mandatory=$false)]
    [Alias("path")]
    [string]$itemPath,

    [Parameter(Position=2,Mandatory=$false)]
    [Alias("grp")]
    [string]$groupUserName,

    [Parameter(Position=3,Mandatory=$false)]
    [Alias("SSRSrole")]
    [string]$role,

    [Parameter(Position=2)]
    [bool]$inherit=$true
)

      

Then I call this function in another Host.ps1 script like:

    Set-Location 'C:\SSRSJobs'
    . .\SSRSsecurity.ps1

      

This call works in the host file:

Add-SSRSItemSecurity -range "server1" -itemPath "/Test" -groupUserName "CN\Group" -role "Browser"

      

So, I tried to pass multiple parameters to the function as a loop, but calling new variables each time:

$securityArray = @()
$securityArray = Get-Content -Path "C\ReleaseSecurity.txt"
    foreach($line in $securityArray)
    {
        Add-SSRSItemSecurity $line;
    }

      

File having:

-range "server1" -itemPath "/Test" -groupUserName "CN\Group" -role "Browser"
-range "server2" -itemPath "/Test" -groupUserName "CN\Group" -role "Browser"
-range "server3" -itemPath "/Test" -groupUserName "CN\Group" -role "Browser"

      

The error I am getting:

Add-SSRSItemSecurity : Cannot bind positional parameters because no names were given.
At line:229 char:27
+                         Add-SSRSItemSecurity <<<<  $line;
    + CategoryInfo          : InvalidArgument: (:) [Add-SSRSItemSecurity], ParameterBindingExcepti 
   on
    + FullyQualifiedErrorId : AmbiguousPositionalParameterNoName,Add-SSRSItemSecurity

      

When checking the string, the $ line variable contains the correct names for the parameters. I have tried all sorts of error traps, but I cannot get a decent error message other than the one above. I've tried quoting forms as well, but I can't seem to get the function to see the name anchor.

Is it possible to call multiple variables in a function bound only to the PS variable name?

0


source to share


1 answer


You can use splatting . Save the parameters as CSV as follows:

"range","itemPath","groupUserName","role"
"server1","/Test","CN\Group","Browser"
"server2","/Test","CN\Group","Browser"
"server3","/Test","CN\Group","Browser"

      



and download it like this:

Import-Csv 'C:\ReleaseSecurity.txt' | % {
  Add-SSRSItemSecurity @_
}

      

+1


source







All Articles