我正在努力自学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-Custom
cmdlet:
# 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
类实例的复杂性。