why can't use this variable in my script

Welcome Forums General PowerShell Q&A why can't use this variable in my script

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 week, 5 days ago.

  • Author
    Posts
  • #113441

    Participant
    Points: 0
    Rank: Member

    why can't use this variable in my script?

    i run script wiht parameter "Untitled1.ps1 -filePath C:\scripts\test01 -logPath C:\scripts" and result is

    step1 C:\scripts\test01 C:\scripts

    step2 C:\scripts\test01 C:\scripts

    step3 "$arg2"

     

    Why step3 not show result " step3  C:script

     

     

    script – Untitled1.ps1

    
    Param
    
    (
    
    [CmdletBinding()]
    
    [string]$filePath,
    
    [string]$logPath
    
    )
    
     
    
     
    
    Write-Host step1 $filePath $logpath
    
     
    
    function Test-Script
    
    {
    
     
    
    Param
    
    (
    
    [Parameter(Position=0)]
    
    [string]$arg1,
    
     
    
    [Parameter(Position=1)]
    
    [string]$arg2
    
    )
    
     
    
    Write-Host step2 $arg1 $arg2
    
     
    
    ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    
    $action = {$arg2}
    
    Write-Host step3 $action
    
    }
    
     
    
    Test-Script $filePath $logPath
    
    
  • #113450

    Participant
    Points: 0
    Rank: Member

    So if I'm reading this correctly... essentially what you're trying to do is have a function that takes a script path and simply executes it. Or something along those lines.

    If you need to read the contents of a script, you want to read it as a text file:

    Get-Content -Path $FilePath

    If you want to define an input string as an executable script block, you can do so like this:

    $Code = [ScriptBlock]::Create()
    $Code.Invoke()

    If you want to log what a script is doing, though, you're better off using Start-Transcript and Stop-Transcript

  • #113458

    Participant
    Points: 0
    Rank: Member

    The problem is scope. Your variable is only available in the scope of the Function. First take a look at: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-6

    This is basically what you are attempting to do:

    function Test-It {
        #Scope is function only
        $myVar = "Powershell"
    }
    
    function Test-AnotherThing {
        param (
            $MyVar
        )
        "Your variable is $myVar"
    }
    
    Test-It
    Test-AnotherThing
    

    The $MyVar is only in the scope of the Test-It function, so the result is:

    Test-It
    Test-AnotherThing
    Your variable is 
    

    You need to understand how to pass information around in your functions. This really where you should be reading a book to understand how to build functions because there are a lot of concepts to understand. A simple fix is something like this:

    function Test-It {
        #Scope is function only
        $myVar = "Powershell"
        #Return the value
        $myVar
    }
    
    function Test-AnotherThing {
        param (
            $MyVar
        )
        "Your variable is $myVar"
    }
    
    #Get the results of Test-It
    $results = Test-It
    #Pass the results of Test-It to Test-AnotherThing
    Test-AnotherThing -MyVar $results
    

    Output:

    $results = Test-It
    Test-AnotherThing -MyVar $results
    Your variable is Powershell
    

    There are other things you can do like change the variable to be global, but that is almost ALWAYS the wrong way to do things.

You must be logged in to reply to this topic.