将 cmdlet 的结果值存储在 Powershell 中的变量中 [英] Store a cmdlet's result value in a variable in Powershell

查看:60
本文介绍了将 cmdlet 的结果值存储在 Powershell 中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to run a cmdlet and store the result's value in a variable.

例如

C:\PS>Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority

它用标题列出优先级.以第一个为例:

It lists priorities with a header. The first one for example:

Priority
--------
8

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

How can i store them in a variable? I've tried:

$var=Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority

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

Now the variable is: @{Priority=8} and I wanted it to be 8.

问题 2:

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

Can I store two variables with one cmdlet? I mean store it after the pipeline.

C:\PS>Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority, ProcessID

我想避免这种情况:

$prio=Get-WSManInstance -enumerate wmicimv2/win32_process | select Priority
$pid=Get-WSManInstance -enumerate wmicimv2/win32_process | select ProcessID

推荐答案

使用 Select-Object

$var=Get-WSManInstance -enumerate wmicimv2/win32_process | select -expand Priority

更新回答另一个问题:

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

Note that you can as well just access the property:

$var=(Get-WSManInstance -enumerate wmicimv2/win32_process).Priority

因此要将其中的多个放入变量中:

So to get multiple of these into variables:

$var=Get-WSManInstance -enumerate wmicimv2/win32_process
   $prio = $var.Priority
   $pid = $var.ProcessID

这篇关于将 cmdlet 的结果值存储在 Powershell 中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆