Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

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
Avatar of sirbounty
sirbounty
Flag of United States of America image

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

Avatar of bibi92

ASKER

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

Thanks
Avatar of bibi92

ASKER

I have created a new script and your code works but the type is always string thanks
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

SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bibi92

ASKER

$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
}