locked
ps1 file run in ise but not work in powershell.exe RRS feed

  • Question

  • Hi.

    i write a simple script in powershell ise:

    #advancedfunction.ps1
    
    
    function advancedfunction
    {
        param(
        [parameter (Mandatory=$true)]
        $a,
    
        [parameter ()]
        $b
        )
    
        Write-Output "a is $a"
        Write-Output "b is $b"
    }

    in powershell.exe I have this problem:

    advancedfunction : The term 'advancedfunction' is not recognized as the name of a cmdlet, function, script file, or
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
    again.
    At line:1 char:1
    + advancedfunction 1 2
    + ~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (advancedfunction:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    what is problem ? and how to fix its?

    Tuesday, May 15, 2018 11:58 AM

Answers

  • Before you can use the function, you need to load the function into your Powershell session.

    . .\advancedfunction.ps1

    Now you can use the created function

    advancedfunction

    When you want to create a function. Note that Powershell is using verb-noun: https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx


    Sincerely, Martien van Dijk. Please remember to mark the replies as answers if they help and unmark them if they provide no help. Check out My Blog!





    Tuesday, May 15, 2018 12:04 PM

All replies

  • You need to define a function before you can use it.
    If you have the function definition in a script file you have to dot source it in your current console session.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Tuesday, May 15, 2018 12:02 PM
    Tuesday, May 15, 2018 12:00 PM
  • Before you can use the function, you need to load the function into your Powershell session.

    . .\advancedfunction.ps1

    Now you can use the created function

    advancedfunction

    When you want to create a function. Note that Powershell is using verb-noun: https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx


    Sincerely, Martien van Dijk. Please remember to mark the replies as answers if they help and unmark them if they provide no help. Check out My Blog!





    Tuesday, May 15, 2018 12:04 PM
  • https://ibb.co/g13qkd
    https://ibb.co/hDoO5d
    Tuesday, May 15, 2018 12:06 PM