problem with my script

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 10 months ago.

  • Author
    Posts
  • #21796
    Profile photo of goran ljubic
    goran ljubic
    Participant

    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
    }
    Get-HostProperties
    
  • #21797
    Profile photo of Don Jones
    Don Jones
    Keymaster

    "FreePhysicalMemory" is not a property of Win32_ComputerSystem http://msdn.microsoft.com/en-us/library/aa394102(v=vs.85).aspx.

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

    function Get-HostProperties {
      [CmdletBinding()]
      Param(
        [Parameter(Mandatory=$True)][string]$ComputerName
      }
    ...
    }
    

    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.