none
script with param switch RRS feed

  • Question

  • Hi

    I try to run the next script

    https://github.com/PrateekKumarSingh/Graphical/tree/master/example

    but something is not done correctly.

    I tried :

    .\Invoke-PerfMon.ps1 CPU=true

    .\Invoke-PerfMon.ps1 true

    i am doing something wrong I assume..

    thanks for any help

    Sunday, September 15, 2019 10:19 AM

All replies

  • Hi Want 2 Learn,

    You would typically "dot" the script into scope (global, another script, within a script block). Dotting a script will both load and execute the script within that scope without creating a new, nested scope. With functions, this has the benefit that they stick around after the script has executed.

    powershell -command "& { . <path>\Invoke-PerfMon.ps1; Invoke-PerfMon }"

    If you just want to execute the function from your current PowerShell session then do this:

    . .\Invoke-PerfMon.ps1
    Invoke-PerfMon

    Just be aware that any script not in a function will be executed and any script variables will become global variables.

    Hopefully this helps,

    Dillon Sykes



    • Edited by DillonSykes Sunday, September 15, 2019 10:44 AM
    • Proposed as answer by DillonSykes Sunday, September 15, 2019 10:44 AM
    • Unproposed as answer by DillonSykes Sunday, September 15, 2019 10:48 PM
    Sunday, September 15, 2019 10:43 AM
  • I know how to run the script

    my question was how to pass a param that is part of the switch

    this is from the script

    Function Invoke-PerfMon {
        param(
            [switch] $CPU,
            [switch] $PhysicalMemory,
            [switch] $VirtualMemory,
            [switch] $EthernetSend,
            [switch] $EthernetReceive,
            [switch] $DiskRead,
            [switch] $DiskWrite,
            $MaxStepsOnYAxis = 4
        )

    Sunday, September 15, 2019 1:22 PM
  • Wouldn't a switch parameter be:


    Invoke-PerfMon -CPU

    • Edited by JS2010 Sunday, September 15, 2019 1:57 PM
    Sunday, September 15, 2019 1:57 PM
  • I tried

    I added 

    echo ('cpu:'+$CPU)

    but nothing is printed

    this is the script

    Added cmdlet Invoke-PerfMon

    • Edited by want 2 Learn Sunday, September 15, 2019 2:56 PM added link
    Sunday, September 15, 2019 2:31 PM
  • You'll have to take that up with the script's author. He's defined the variable $CPU as a parameter of type '[switch]' and later as a type '[int[]]'.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, September 15, 2019 3:00 PM
  • i changed in th switch to CPU1

    and tried

    echo ('cpu:'+$CPU1)

    still not prints

    Sunday, September 15, 2019 3:19 PM
  • You cannot pass a switch Param on a command line. You cannot pass a switch Param as a string. A switch is a defaulted Boolean and can only be managed as a Boolean. It can only be passed as a "named" parameter with no value.

    To pass a switch on a command line from PowerShell:

    Invoke-Perfmon -CPU

    You cannot add any values to the switch.  There is no other method to use this parameter. 


    \_(ツ)_/

    Sunday, September 15, 2019 3:55 PM
    Moderator
  • so how can it be used?

    can you give an example?

    can I see that it got a value of true or false?

    Sunday, September 15, 2019 4:41 PM
  • Just paste this at a PowerShell prompt to see what happens.

    function Test-Switch{
        Param(
            [switch]$Test
        )
        if($Test){
            'Switch used'
        }else{
            'switch NOT used'
        }
    }
    Test-Switch -Test
    Test-Switch


    \_(ツ)_/

    Sunday, September 15, 2019 4:56 PM
    Moderator
  • You can also do this to check parameters.

    function Test-Switch{
        Param(
            [switch]$Test
        )
        $PSBoundParameters
    }
    Test-Switch -Test
    Test-Switch


    \_(ツ)_/

    Sunday, September 15, 2019 4:57 PM
    Moderator
  • You can (still) set a switch to a Boolean value if you're not sure there's a default that's what you want.

    function Test-Switch{
        Param(
            [switch]$Test
        )
        if($Test){
            'Switch used'
        }else{
            'switch NOT used'
        }
    }
    Test-Switch -Test
    Test-Switch -Test:$true
    Test-Switch -Test:$false
    Test-Switch


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Sunday, September 15, 2019 5:14 PM
  • This script depends on a command called "Show-Graph" in a module called "Graphical".  You'd have to import the module, then source the script, then you can run the Invoke-Perfmon function, and all the switch parameters will work.  The -recurse parameter of get-childitem is an example of a switch parameter.


    import-module .\Graphical.psm1
    . .\example\Invoke-PerfMon.ps1
    Invoke-Perfmon -CPU

    Monday, September 16, 2019 12:34 AM
  • JS2010 Thanks!

    can you explain this line please

    . .\example\Invoke-PerfMon.ps1

    Monday, September 16, 2019 6:40 AM
  • I'm just sourcing the file to load the function.  It comes in an example folder, if you download the whole module from github.
    Monday, September 16, 2019 12:22 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Friday, October 4, 2019 8:10 AM
    Moderator