Select-object -property parameter not displaying multiple column - powershell

Currently when I try
get-process | Select-Object -property NAME,Id,CPU,PM
Only the Name column is displayed in vscode in ubuntu
Name
----
(sd-pam)
accounts-daemon
However if i switch the position of the value 'name' it would work but it seems to only display till where the 'name property is?
PS /home/limyk14> get-process | Select-Object -property Id,CPU,name,PM
Id CPU Name
-- --- ----
1506 0 (sd-pam)
PS /home/limyk14> get-process | Select-Object -property Id,CPU,PM,name
Id CPU PM Name
-- --- -- ----
1506 0 880640 (sd-pam)
797 2.77 49152 accounts-daemon

Thanks to #iRon and #mclayton
It is actually due to the length of a few processes that had really long names
vs code itself was one of them,
Format-table var1,2,3,4 -autosize didn't help neither did wrapping.
However the suggestion of using a hash table and declaring the width works.
format-table #{name = 'name';expression = 'name'; width = 7}, cpu, id, pm

Related

How to transform multiple objects with same name into one object with the sum?

Drawing a weird blank here as i'm sure i've done this before.
I have these objects:
Name01 Capacity0
------ ---------
P1234 4096
P1234 4096
P1234 4096
How do i turn it into only one object containing the sum of all 'capacity0' like this? :
Name01 Capacity0
------ ---------
P1234 12288
I know how to get the sum for example:
... Select Name01,Capacity0 | Measure-Object -Property Capacity0 -sum
but then i lose the 'name01' property if i pipe it like that.
What's the best way? Something with group-object maybe? Struggling with the logic i should use.
Much appreciated.
Yes, group by Name01 first, then select the grouped Sum:
... | Select Name01, Capacity0 `
| Group-Object -Property Name01 `
| Select `
#{Label = "Name01"; Expression={$_.Name}},
#{Label = "Sum"; Expression = {($_.Group | Measure-Object -Property Capacity0 -Sum).Sum}}
Result:
Name01 Sum
------ ---
P1234 12288

powershell select-object property AND expandproperty

Trying to execute a Select-Object, but i can't seem to return property and a value from a key-value list property. There are 3 properties i want returned RunStart, Message and 'tableName' from this Parameters property.
I get too much with this:
|Select-Object -Property Parameters,RunStart,Message
Parameters RunStart Message
---------- -------- -------
{[tableName, AHROR012], [schemaName, dbo]...} 11/14/2019 5:39:06 PM Operation on target failed
But I dont get the RunStart or Message when i do this:
|Select-Object -ExpandProperty Parameters -Property tableName,RunStart,Message
Key Value
--- -----
tableName AHROR012
How do i do it to get:
Parameters.tableName RunStart Message
---------- -------- -------
AHROR012 11/14/2019 5:39:06 PM Operation on target failed
THANKS!!!
... | Select-Object -Property #{
Name='ParametersTableName';
Expression={ $_.Parameters.tableName }
}, RunStart, Message
A more elegant solution would be to use a 2nd select statement:
... | select RunStart,Message -ExpandProperty Parameters | Select RunStart,Message,tableName

How to strip out everything but numbers in a variable?

I'm trying to automate setting an IP address through PowerShell and I need to find out what my interfaceindex number is.
What I have worked out is this:
$x = ( Get-NetAdapter |
Select-Object -Property InterfceName,InterfaceIndex |
Select-Object -First 1 |
Select-Object -Property Interfaceindex ) | Out-String
This will output:
InterfaceIndex
--------------
3
Now the problem, when I try to grab only the number using:
$x.Trim.( '[^0-9]' )
it still leave the "InterfaceIndex" and the underscores. This causes the next part of my script to error because I just need the number.
Any suggestions?
This will get your job done:
( Get-NetAdapter | Select-Object -Property InterfceName,InterfaceIndex | Select-Object -First 1 | Select-Object -Property Interfaceindex).Interfaceindex
Actually you do not need two times to select the property: do like this:
( Get-NetAdapter |Select-Object -First 1| Select-Object -Property InterfceName,InterfaceIndex).Interfaceindex
Answering your immediate question: you can remove everything that isn't a number from a variable by, well, removing everything that isn't a number (or rather digit):
$x = $x -replace '\D'
However, a better aproach would be to simply not add what you want removed in the first place:
$x = Get-NetAdapter | Select-Object -First 1 -Expand InterfaceIndex
PowerShell cmdlets usually produce objects as output, so instead of mangling these objects into string form and cutting away excess material you normally just expand the value of the particular property you're interested in.
(Get-NetAdapter | select -f 1).Interfaceindex
No point in selecting properties as they are there by default. If you want to keep object do:
(Get-NetAdapter | select -f 1 -ov 'variablename').Interfaceindex
where f = first, ov = outvariable
$variablename.Interfaceindex
You don't need Out-String as cast to string is implicit when you output to screen. and if you try to work with this data further down powershell is clever enough to cast it from int to string and vice versa when needed.

Returning the member in a group with the highest of a specific property

I'm trying to find the process with the highest CPU in a given group, in my case the group is simply by processname. Assume I have these processes running:
Id ProcessName CPU Memory Threads
-- ----------- --- ------ -------
7532 MicrosoftEdgeCP 40,3125 355,51953125 27
1680 powershell_ise 47,875 214,1015625 23
7568 lync 7,9375 213,859375 52
9664 chrome 19,71875 167,9609375 12
4216 MicrosoftEdgeCP 92,578125 152,2890625 26
5392 explorer 31,09375 116,390625 66
2676 chrome 23,390625 110,96875 41
9812 chrome 14,625 100,859375 8
2872 MsMpEng 100,5234375 36
5752 SearchUI 2,609375 90,0625 23
I'm trying to find Chrome (9664). Currently I have this code:
$list = #()
$grouped = Get-Process | Where ProcessName -Like "c*" |
Sort CPU -Descending |
Group ProcessName |
Select $_ -First 10
$grouped
foreach($item in $grouped) {
$list += #($item.Group |
Sort CPU -Descending |
Select Id, ProcessName, CPU, Memory, Threads -First 1)
}
$list | ft -Wrap
Is it possible to do the same without storing the first in each group in the $list array?
So you wanted to get the process where the CPU is the highest among the threads of the same name correct?
Get-Process -Name "c*" |
Sort-Object CPU -Descending |
Group ProcessName |
ForEach-Object{$_.Group | Select Id, ProcessName, CPU, Memory, Threads -First 1}
-Name supports wildcards so we can save returning all the processes just to drop most of them. Do the first sort and group on processname. Then just take the first entry from each individual group. The last for each is required to prevent the object array from being unrolled and only returning the highest overall entry.

Powershell free disk space for drives without drive letter

I am working on a power shell script based on http://www.powershellneedfulthings.com/?p=36 to check the disk space for volumes that do not have a driver letter assigned.
The script works pretty well, but I'd like to filter that only drives are shown that have less than 10% free disk space. I'm running into troubles using the where-object filter with hash tables.
# calculations for displaying disk size information
$TotalGB = #{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1GB),2)}}
$FreeGB = #{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1GB),2)}}
$FreePerc = #{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1GB)/($_.Capacity / 1073741824)) * 100),0)}}
# array declarations
$volumes = #()
# import server names to check
$servers = (Get-Content .\servers.txt)
# check disk space for volumes without drive letter
foreach ($server in $servers){
$volumes += Get-WmiObject -computer $server win32_volume | Where-Object {$_.DriveLetter -eq $null -and $_.Label -ne "System Reserved"}
}
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize
What I tried is:
Where-Object {$FreePerc -le 10}
The current output is:
SystemName Label Capacity(GB) FreeSpace(GB) Free(%)
---------- ----- ------------ ------------- ----
SERVER01 X:\data\ 9.97 0.89 9
SERVER01 X:\log\ 9.97 1.20 12
SERVER01 X:\info\ 9.97 3.49 35
I'd like to only show the volumes that have less than 10% free disk space. So in this case, only the first entry should be shown.
Thanks!
I think the where clause variable $FreePerc is the issue. Arco had the right idea.
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.'Free(%)' -le 10} | Format-Table -AutoSize
I put the property in single quotes because i think PowerShell would try to evaluate (%) otherwise. Also to make Arco's solution work it might just be easier to call the Name propery of $FreePerc. That way you only have to update one location
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.($FreePerc.Name) -le 10} | Format-Table -AutoSize