problem with my script

Welcome Forums General PowerShell Q&A problem with my script

This topic contains 1 reply, has 2 voices, and was last updated by

4 years, 5 months ago.

  • Author
  • #21796

    Topics: 3
    Replies: 11
    Points: 1
    Rank: Member

    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

    Topics: 13
    Replies: 4872
    Points: 1,813
    Helping HandTeam Member
    Rank: Community Hero

    "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. Learn PowerShell Toolmaking in a Month of Lunches 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, The Big Book of PowerShell Gotchas or Learn Windows PowerShell in a Month of Lunches. 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!

The topic ‘problem with my script’ is closed to new replies.