problem with my script

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 2 years, 7 months ago.

  • Author
  • #21796

    goran ljubic

    i'm newbie i'm learning powershell.i created simple script but i cannot received freephysicalmemory from my script. why

    function Get-HostProperties{
    param ($computerName = (Read-Host "Enter Computer Name")
    Get-WmiObject -Class win32_logicaldisk -ComputerName $computerName -Credential dri\administrator | ft DeviceID, @{Name="Free Disk Space (GB)";e={$_.FreeSpace /1GB}}, @{Name="Total Disk Size (GB)";e={$_.Size /1GB}} -AutoSize
    Get-WmiObject -Class win32_computersystem -ComputerName $computerName -Credential dri\administrator|ft @{Name="Physical Processors";e={$_.NumberofProcessors}} ,@{Name="Logical Processors";e={$_.NumberOfLogicalProcessors}}, @{Name="TotalPhysicalMemory (GB)";e={[math]::truncate($_.TotalPhysicalMemory /1GB)}}, @{Name="FreePhysicalMemory (GB)"; e={[math]::Round($_.FreePhysicalMemory/1024)}}, Model -AutoSize
  • #21797

    Don Jones

    "FreePhysicalMemory" is not a property of Win32_ComputerSystem

    Also, as you're just getting started, using Read-Host to prompt for a parameter value is generally a poor practice.

    function Get-HostProperties {

    Will cause a prompt for the value if it isn't provided. This is the preferred way to accomplish that. [i]Learn PowerShell Toolmaking in a Month of Lunches[/i] covers that and other techniques. Additionally, formatting your output within the function is a common "gotcha" and leads the output being less useful. In your function, for example, you cannot pipe the output of Get-HostProperties to anything like Export- or ConvertTo- or anything. See our free ebook, [i]The Big Book of PowerShell Gotchas[/i] or [i]Learn Windows PowerShell in a Month of Lunches[/i]. Generally, functions should not format output. You can accomplish the same thing, in a more flexible fashion, by using Select-Object instead of Format-Table.

    Good luck!

You must be logged in to reply to this topic.