FunctionGivingErrorWhileTryingCall

sns 8,116 Reputation points
2022-01-24T08:50:38.667+00:00

I want to open some of the processes with the help of function, But I saw below warning which I am not quite sure, Please assist
and Also what I should do to use this function every time when I log in ISE ( not only for current session, I need for all sessions )

Please help

167785-wanttocreatefunctionforopeningprocessesalways.png

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,731 questions
No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 24,701 Reputation points
    2022-01-24T14:04:15.873+00:00

    You should start by downloading and reviewing this document.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    Powershell can use positional parameters or named parameters.

    function TestPosition ($ProcessName,$SomethingElse) {
        Write-Host ""
        Write-Host "You asked me to look for $ProcessName." 
        Write-Host "And do something with $SomethingElse." 
        Get-Process $ProcessName
    }
    function TestNamed {param (
            [string]$ProcessName = "",
            [string]$SomethingElse = ""
        )
        Write-Host ""
        Write-Host "You asked me to look for $ProcessName." 
        Write-Host "And do something with $SomethingElse." 
        Get-Process $ProcessName
    
    }
    TestPosition "notepad" "foo"
    TestNamed -SomethingElse "XXXXXX" -ProcessName "Powershell"
    
  2. Limitless Technology 37,506 Reputation points
    2022-01-28T08:51:13.6+00:00

    Hi there,

    There are many ways to handle errors in Windows PowerShell, including:

    $Error.Clear(); Do-Something; If ($Error) {..} Else {..}
    Trap
    $ErrorActionPreference

    Trap blocks generally catch any errors in the scope of the entire script or function. The beauty of Try, Catch, Finally is that it is like a localized Trap for a specific block of commands. This gives you great flexibility in your error handling.

    Here are some links to help you out

    Using Try, Catch, Finally Blocks for PowerShell Error Handling
    https://devblogs.microsoft.com/scripting/weekend-scripter-using-try-catch-finally-blocks-for-powershell-error-handling/

    Handling Errors the PowerShell Way
    https://devblogs.microsoft.com/scripting/handling-errors-the-powershell-way/


    --If the reply is helpful, please Upvote and Accept it as an answer--