convert column MEM to int in format-table

Hello,

I search to convert MEM to int in this script :
If ($ProcessName -eq "ORACLE"{
  $ProcessList = gwmi Win32_PerfRawData_PerfProc_Process | where {$_.Name -eq $ProcessName} | select IDProcess,Name,WorkingSetPrivate | sort WorkingSetPrivate -Descending | select -First $SelectFirst
}
else {exit 0}
$TProcess = @()
ForEach ($Process in $ProcessList) {
    $row = new-object PSObject -Property @{
    Id = $Process.IDProcess
    Name = $Process.Name
    MEM = $Process.WorkingSetPrivate /1024
  }
  $TProcess += $row
}
[int] $proc = $TopProcess | sort MEM -Descending | select MEM | ft -hide -autosize

Open in new window


Thanks
bibi92Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Try this adjustment:
$selectfirst = 3
$ProcessName = "Oracle"
 $ProcessList = gwmi Win32_PerfRawData_PerfProc_Process | where {$_.name -like $ProcessName} |select IDProcess,Name,WorkingSetPrivate | sort WorkingSetPrivate -Descending | select -First $SelectFirst

$TProcess = @()
ForEach ($Process in $ProcessList) {
    $row = new-object PSObject 
    $row | Add-Member -MemberType NoteProperty -Name Id -Value $Process.IDProcess
    $row | Add-Member -MemberType NoteProperty -Name Name -value $Process.Name
    $row | Add-Member -MemberType NoteProperty -Name MEM -value ($Process.WorkingSetPrivate /1024)
    $TProcess += $row
}
$proc = $TProcess | sort MEM -Descending | select MEM | ft -hide -autosize

Open in new window

bibi92Author Commented:
Missing '=' operator after key in hash literal.
+   $row | <<<<  Add-Member -MemberType NoteProperty -Name Id -Value $Process.I
DProcess
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEqualsInHashLiteral

Thanks
bibi92Author Commented:
I have created a new script and your code works but the type is always string thanks
Check Out How Miercom Evaluates Wi-Fi Security!

It's not just about Wi-Fi connectivity anymore. A wireless security breach can cost your business large amounts of time, trouble, and expense. Plus, hear first-hand from Miercom on how WatchGuard's Wi-Fi security stacks up against the competition plus a LIVE demo!

sirbountyCommented:
Not sure why you have the table formatting in there, but this works fine for me:
$selectfirst = 3
$ProcessName = "Oracle"
 $ProcessList = gwmi Win32_PerfRawData_PerfProc_Process | where {$_.name -like $ProcessName} |select IDProcess,Name,WorkingSetPrivate | sort WorkingSetPrivate -Descending | select -First $SelectFirst

$TProcess = @()
ForEach ($Process in $ProcessList) {
    $row = new-object PSObject 
    $row | Add-Member -MemberType NoteProperty -Name Id -Value $Process.IDProcess
    $row | Add-Member -MemberType NoteProperty -Name Name -value $Process.Name
    $row | Add-Member -MemberType NoteProperty -Name MEM -value ($Process.WorkingSetPrivate /1024)
    $TProcess += $row
}
[int]$proc = $TProcess | sort MEM -Descending | select -expand MEM 

Open in new window

footechCommented:
I tested in both PS 3.0 and 2.0 and your original code has MEM with a type of "UInt64" and "Double", respectively.
You can force it to be an [int] (or [int32]) by modifying line 10.
MEM = [int]$Process.WorkingSetPrivate /1024

Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The result of format-* cmdlets always is a string array consisting of the complete lines. That formatting should always be applied for display on screen, not for storing into a var.
footechCommented:
Now I'm wondering if I misunderstood the question.  Is it all about line 14?

@Qlemo - I'm curious about your statement, "The result of format-* cmdlets always is a string array consisting of the complete lines."  The output of Format-* cmdlets is format data objects.  I suppose it could be argued that format data objects are just strings, or have I misunderstood what you meant.?
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You are correct, my statement is too simplistic ;-). The result of the formatting cmdlets are format data objects with complex header, footer, ... object types.

I was more after the original objects loosing their original properties, and getting transformed into something only useful for displaying. In no way it makes sense to convert a format-table output to int.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bibi92Author Commented:
$selectfirst = 3
$ProcessName = "Oracle"
 $ProcessList = gwmi Win32_PerfRawData_PerfProc_Process | where {$_.name -like $ProcessName} |select IDProcess,Name,WorkingSetPrivate | sort WorkingSetPrivate -Descending | select -First $SelectFirst

$TProcess = @()
ForEach ($Process in $ProcessList) {
  $row = new-object PSObject -Property @{
    Id = $Process.IDProcess
    Name = $Process.Name
    MEM = [INT]$Process.WorkingSetPrivate /1024
  }
  $TopProcess += $row
  echo $row.MEM
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.