将cmdlet的结果值存储在Powershell中的变量中

Lgn*_*Lgn 13 powershell

我想运行一个cmdlet并将结果的存储在一个变量中.

例如

C:\PS>Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority
Run Code Online (Sandbox Code Playgroud)

它列出了标题的优先级.第一个例如:

Priority
--------
8
Run Code Online (Sandbox Code Playgroud)

我如何将它们存储在变量中?我试过了:

$var=Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority
Run Code Online (Sandbox Code Playgroud)

现在变量是:@{Priority=8}我希望它是8.

问题2:

我可以使用一个cmdlet存储两个变量吗?我的意思是将它存储在管道之后.

C:\PS>Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority, ProcessID
Run Code Online (Sandbox Code Playgroud)

我想避免这个:

$prio=Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority
$pid=Get-WSManInstance -enumerate wmicimv2/win32_process | select ProcessID
Run Code Online (Sandbox Code Playgroud)

man*_*lds 25

使用-ExpandProperty标志Select-Object

$var=Get-WSManInstance -enumerate wmicimv2/win32_process | select -expand Priority
Run Code Online (Sandbox Code Playgroud)

更新以回答其他问题:

请注意,您也可以访问该属性:

$var=(Get-WSManInstance -enumerate wmicimv2/win32_process).Priority
Run Code Online (Sandbox Code Playgroud)

所以要将其中的多个变为变量:

$var=Get-WSManInstance -enumerate wmicimv2/win32_process
   $prio = $var.Priority
   $pid = $var.ProcessID
Run Code Online (Sandbox Code Playgroud)