Select attributes or parameter with variable in Po

2019-07-27 11:23发布

Using this code I get the desired result:

Get-Service | select Name,Status

But the following code will not work, do you know why? I want the user to choose his own selection of attributes. I store the attributes in a variable like shown below. But it won't work:

$param = "Name,Status"
Get-Service | select $param

2条回答
叛逆
2楼-- · 2019-07-27 12:02

You have to create an array of the properties you want to select:

$param = "Name","Status"
Get-Service | select $param

Or you can split the string yourself to create an array:

$param = "Name,Status"
Get-Service | select ($param -split ',')
查看更多
萌系小妹纸
3楼-- · 2019-07-27 12:10

You could also create a hash table, like this:

$params = @{Property=@('Name','Status')}
Get-Service | Select @params

And even add some extra parameters, like this:

$params = @{
            Property=@('Name','Status');
            First=10;
            }
Get-Service | Select @params
查看更多
登录 后发表回答