none
Scoping variables: using 1 variable from 1 function in another RRS feed

  • Question

  • Hi,

    I'm struggling with scoping, checked the e about scopes help. I have this script which has a variable $OS which has a validateset.
    Deploy-SoftwareUpdates -OS W7/W10 etc

    This is the script.

    Get-WhatIneed
    {
    
     [Validateset("W7","W10")]
                 [Parameter(Position=1)][String]$OS = (Read-Host "Please provide OS")
                 )
    
    if ($os -eq W7)
        {Get-SomeInfo -os W7
               if ($environment -eq LAB)
               {}
               if ($environment -eq PRD)
               {}
         }
    
    if ($os -eq W10)
            {Get-SomeInfo -os W10
               if ($environment -eq LAB)
               {}
               if ($environment -eq PRD)
               {}
            }
    }
    
    Then I have this other function in the same script which continues on the option chosen in the first script
    
    Get-SomeInfo
    {
     [Validateset("W7","W10")]
                 [Parameter(Position=1)][String]$OS = (Read-Host "Please provide OS")
                 )
    if ($os -eq W7)
        {
        if ($environment -eq LAB)
        {}
        if ($environment -eq PRD)
        {}
        }
    
    if ($os -eq W10)
        {}
    
    }
    

    When I execute Get-WhatIneed -OS W7 I get a prompt from Get-SomeInfo
    "Supply values for the following parameters:
    OS:

    I tried scoping $os but that didn't work. How should I correctly do that?
    Should it be in the Parameter section: $Script:OS =
    Should it be in the script itself $OS = $SCript:OS, $OS = $Global:OS ...

    Tried them all but it didn't work.
    Please advise.
    J.

     

    Jan Hoedt

    Friday, January 29, 2016 12:58 PM

Answers

  • That is how it works with validation,  You cannot override it by adding Read-Host.  Use one or the other but not both.

    \_(ツ)_/

    Friday, January 29, 2016 4:27 PM