我试图使用具有参数集的另一个PowerShell函数来调用具有参数集的PowerShell函数。在下面的代码中,我想使用Entry-Point
或-ABC
开关(不是同时不是)来调用-DEF
。但是,当我使用任意数量的参数运行Entry-Point
时,下面将得到AmbiguousParameterSet
异常。
function My-Function {
[CmdletBinding(DefaultParameterSetName='ABC')]
Param(
[Parameter(Mandatory=$false, ParameterSetName='ABC', Position=1)]
[switch] $ABC,
[Parameter(Mandatory=$false, ParameterSetName='DEF', Position=1)]
[switch] $DEF,
[switch] $Extra
)
echo $ABC
echo $DEF
echo $Extra
}
function Entry-Point {
[CmdletBinding(DefaultParameterSetName='ABC')]
Param(
[Parameter(Mandatory=$false, ParameterSetName='ABC', Position=1)]
[switch] $ABC,
[Parameter(Mandatory=$false, ParameterSetName='DEF', Position=1)]
[switch] $DEF,
[switch] $Extra
)
My-Function -ABC:$ABC -DEF:$DEF -Extra:$Extra
}
My-Function : Parameter set cannot be resolved using the specified named parameters. At line:24 char:1 + My-Function -ABC:$ABC -DEF:$DEF -Extra:$Extra + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [My-Function], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,My-Function
如何将参数-ABC
和-DEF
从功能Entry-Point
传递到功能My-Function
(无论是否已指定)?
答案 0 :(得分:1)
快速的答案是喷溅。 PowerShell使您可以将哈希表作为函数的参数来呈现(使用@代替$),并使用哈希表的键作为参数名称,这些值就是参数值。
此外,每个高级功能都会自动将$ PSBoundParameters设置为传入参数的哈希表。
所以...您可以说:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
typedef struct MATRIX {
unsigned int row = 0, column = 0;
int data[0][0];
};
void generateMatrixsParallel(struct MATRIX matrixA, struct MATRIX matrixB) {
srand((unsigned int) time(NULL));
printf("\n%d\n", matrixB.row);
#pragma omp for
for(int row = 0; row < matrixA.row; row++) {
for(int column = 0; column < matrixA.column; column++) {
matrixA.data[row][column] = rand();
}
}
printf("\n%d\n", matrixB.row);
#pragma omp for
for(int row = 0; row < matrixB.row; row++) {
for(int column = 0; column < matrixB.column; column++) {
matrixB.data[row][column] = rand();
//printf("MATRIX B[%d][%d]: [%d]\n", row, column, matrixB.data[row][column]);
}
}
}
int main() {
static struct MATRIX matrixA;
static struct MATRIX matrixB;
printf("BETWEEN WITH QUANTITY OF LINES [VECTOR A]: ");
scanf("%d", &matrixA.row);
printf("BETWEEN WITH QUANTITY OF COLUMN [VECTOR A]: ");
scanf("%d", &matrixA.column);
printf("BETWEEN WITH QUANTITY OF LINES [VECTOR B]: ");
scanf("%d", &matrixB.row);
printf("BETWEEN WITH QUANTITY OF COLUMN [VECTOR B]: ");
scanf("%d", &matrixB.column);
#pragma omp parallel firstprivate(matrixA, matrixB) num_threads(3)
{
#pragma omp master
printf("AGUARDE... A THREAD MASTER ESTÁ GERANDO AS MATRIZES!", omp_get_thread_num());
generateMatrixsParallel(matrixA, matrixB);
}
return 0;
}