【发布时间】:2018-11-25 04:45:19
【问题描述】:
我想访问在 C# 中的命令行开关中设置的所有 PSVariable。 因此,我的一个 C# Commandlet 将在 sessionState 中设置来自 powershell 脚本的变量,而另一个 commandlet 将枚举所有 sessionState 变量及其在 commandlet 中设置的类型。我不想枚举脚本变量。
例如
[string[]]$ListOfEmployees = "EmployeeA","EmployeeA","EmployeeC"
Set-Parameters "Employees" $ListOfEmployees
[string[]]$ListOfDepartments = "Communication","Mathematics","ComputerScience"
Set-Parameters "Departments" $ListOfDepartments
[string[]]$ListOfCoursesInAllDepts = "CourseA","CourseB","CourseC"
Set-Parameters "Courses" $ListOfCoursesInAllDepts
稍后在脚本中的某个位置,我想准确访问上面的变量及其类型。
即
$availableVariables = Enumerate-AllAvailableVariables
上面Commandlet的输出应该是
Name Type
------------- ------
Employees String[]
Departments String[]
Courses String[]
这怎么可能?
【问题讨论】:
-
如果您的输出仅显示属性名称及其类型,为什么还要用值填充变量?
-
因为上面 PSComndlet 的输出只是枚举可用变量。基本上对于我的业务用例来说,需要方便用户随时查看powershell中的可用变量。
-
我的一个商业案例是通过从磁盘读取外部文件来填充 powershell 变量。 (这可能包含 1000 个带值的变量)并且用户可能对获取其中几个变量的值感兴趣。因此,一个命令行开关只能枚举已加载变量的名称和类型
-
我想你想使用自动变量 $PSBoundParameters
-
不幸的是,不是。 $PSBoundParameters 仅包含传递给脚本或函数的参数及其当前值的字典。正如我清楚地写下我想要的那样。
标签: c# powershell cmdlets runspace