Windows Vista Forums

Read-Host issue, won't store to variable when using multiple read-host lines

  1. #1


    bryan.rutkowski@gmail.com Guest

    Read-Host issue, won't store to variable when using multiple read-host lines

    I am prompting the user to enter new user information in using Read-
    Host. The only problem is, when I run my function and enter the data
    in, it doesn't exists, all the variables are null. If I just do each
    one, one at a time outside of the function I can get read-host to work
    fine. But whenever I try to do more then one at a time it doesn't
    work at all. Below is my code. So once I run the function create-
    user and try to get the data in any of the variables (ex.
    $strLogonName) they are all ways null. Does anyone have any ideas?

    function create-user{

    # Asks the user to input the Account name they wish to create.

    $strLogonName = read-host "Enter the users Logon Name, use ** as a
    wildcard where you would want the State Code to go"

    # Asks the user to input the Domain Suffix of the user.

    $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    @cars.com"

    # Asks the user to input the First Name of the user.

    $strFirstName = read-host "Enter the users First Name, use ** as a
    wildcard where you would want the State Code to go"

    # Asks the user to input the Last Name of the user.

    $strLastName = read-host "Enter the users Last Name, use ** as a
    wildcard where you would want the State Code to go"

    # Asks the user to input the Description for the user.

    $strDescription = read-host "Enter the users Description, use ** as a
    wildcard where you would want the State Code to go"

    # Asks the user to input the Password of the user.

    $strPassword = read-host "Enter the users Password, use ** as a
    wildcard where you would want the State Code to go"

    # Asks the user to input the OU Path they wish to create the user in,
    do not include the Domain DN.

    $strOUPath = read-host "Enter the OU Path. Leave out all domain
    specific information, use ## as a wildcard where you would want the
    State Code to go"
    }


      My System SpecsSystem Spec

  2. #2


    RichS Guest

    RE: Read-Host issue, won't store to variable when using multiple read-

    How are you returning this information from your function?

    I've just tried the function & the variables seem to pick up the input OK
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "bryan.rutkowski@gmail.com" wrote:

    > I am prompting the user to enter new user information in using Read-
    > Host. The only problem is, when I run my function and enter the data
    > in, it doesn't exists, all the variables are null. If I just do each
    > one, one at a time outside of the function I can get read-host to work
    > fine. But whenever I try to do more then one at a time it doesn't
    > work at all. Below is my code. So once I run the function create-
    > user and try to get the data in any of the variables (ex.
    > $strLogonName) they are all ways null. Does anyone have any ideas?
    >
    > function create-user{
    >
    > # Asks the user to input the Account name they wish to create.
    >
    > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Domain Suffix of the user.
    >
    > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > @cars.com"
    >
    > # Asks the user to input the First Name of the user.
    >
    > $strFirstName = read-host "Enter the users First Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Last Name of the user.
    >
    > $strLastName = read-host "Enter the users Last Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Description for the user.
    >
    > $strDescription = read-host "Enter the users Description, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Password of the user.
    >
    > $strPassword = read-host "Enter the users Password, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the OU Path they wish to create the user in,
    > do not include the Domain DN.
    >
    > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > specific information, use ## as a wildcard where you would want the
    > State Code to go"
    > }
    >
    >


      My System SpecsSystem Spec

  3. #3


    bryan.rutkowski@gmail.com Guest

    Re: Read-Host issue, won't store to variable when using multiple read-

    Here is the exact output when I run it.

    PS C:\Documents and Settings\bryan.rutkowski> function create-user{
    >>
    >> # Asks the user to input the Account name they wish to create.
    >>
    >> $strLogonName = read-host "Enter the users Logon Name, use ** as a wildcard where you would want the State Code to go

    "
    >>
    >> # Asks the user to input the Domain Suffix of the user.
    >>
    >> $strDomainSuffix = read-host "Enter the users Domain Suffix, ex. @cars.com"
    >>
    >> # Asks the user to input the First Name of the user.
    >>
    >> $strFirstName = read-host "Enter the users First Name, use ** as a wildcard where you would want the State Code to go

    "
    >>
    >> # Asks the user to input the Last Name of the user.
    >>
    >> $strLastName = read-host "Enter the users Last Name, use ** as a wildcard where you would want the State Code to go"
    >>
    >> # Asks the user to input the Description for the user.
    >>
    >> $strDescription = read-host "Enter the users Description, use ** as a wildcard where you would want the State Code to

    go"
    >>
    >> # Asks the user to input the Password of the user.
    >>
    >> $strPassword = read-host "Enter the users Password, use ** as a wildcard where you would want the State Code to go"
    >>
    >> # Asks the user to input the OU Path they wish to create the user in, do not include the Domain DN.
    >>
    >> $strOUPath = read-host "Enter the OU Path. Leave out all domain specific information, use ## as a wildcard where you

    would want the State Code to go"
    >> }
    >>

    PS C:\Documents and Settings\bryan.rutkowski> create-user
    Enter the users Logon Name, use ** as a wildcard where you would want
    the State Code to go: ** Logon Name
    Enter the users Domain Suffix, ex. @cars.com: @cars.com
    Enter the users First Name, use ** as a wildcard where you would want
    the State Code to go: John
    Enter the users Last Name, use ** as a wildcard where you would want
    the State Code to go: Doe
    Enter the users Description, use ** as a wildcard where you would want
    the State Code to go: John Doe
    Enter the users Password, use ** as a wildcard where you would want
    the State Code to go: 12345
    Enter the OU Path. Leave out all domain specific information, use ##
    as a wildcard where you would want the State Code t
    o go: ou=cars
    PS C:\Documents and Settings\bryan.rutkowski> $strlogonname
    PS C:\Documents and Settings\bryan.rutkowski> $strDomainSuffix
    PS C:\Documents and Settings\bryan.rutkowski>

    As you can see nothing is set in the variables. Am I doing something
    wrong, or calling them the wrong way?



    On Feb 9, 2:19 pm, RichS <R...@discussions.microsoft.com> wrote:
    > How are you returning this information from your function?
    >
    > I've just tried the function & the variables seem to pick up the input OK
    > --
    > Richard Siddaway
    > Please note that all scripts are supplied "as is" and with no warranty
    > Blog:http://richardsiddaway.spaces.live.com/
    > PowerShell User Group:http://www.get-psuguk.org.uk
    >
    > "bryan.rutkow...@gmail.com" wrote:
    > > I am prompting the user to enter new user information in using Read-
    > > Host. The only problem is, when I run my function and enter the data
    > > in, it doesn't exists, all the variables are null. If I just do each
    > > one, one at a time outside of the function I can get read-host to work
    > > fine. But whenever I try to do more then one at a time it doesn't
    > > work at all. Below is my code. So once I run the function create-
    > > user and try to get the data in any of the variables (ex.
    > > $strLogonName) they are all ways null. Does anyone have any ideas?

    >
    > > function create-user{

    >
    > > # Asks the user to input the Account name they wish to create.

    >
    > > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > > wildcard where you would want the State Code to go"

    >
    > > # Asks the user to input the Domain Suffix of the user.

    >
    > > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > > @cars.com"

    >
    > > # Asks the user to input the First Name of the user.

    >
    > > $strFirstName = read-host "Enter the users First Name, use ** as a
    > > wildcard where you would want the State Code to go"

    >
    > > # Asks the user to input the Last Name of the user.

    >
    > > $strLastName = read-host "Enter the users Last Name, use ** as a
    > > wildcard where you would want the State Code to go"

    >
    > > # Asks the user to input the Description for the user.

    >
    > > $strDescription = read-host "Enter the users Description, use ** as a
    > > wildcard where you would want the State Code to go"

    >
    > > # Asks the user to input the Password of the user.

    >
    > > $strPassword = read-host "Enter the users Password, use ** as a
    > > wildcard where you would want the State Code to go"

    >
    > > # Asks the user to input the OU Path they wish to create the user in,
    > > do not include the Domain DN.

    >
    > > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > > specific information, use ## as a wildcard where you would want the
    > > State Code to go"
    > > }




      My System SpecsSystem Spec

  4. #4


    RichS Guest

    Re: Read-Host issue, won't store to variable when using multiple r

    OK I see your problem now. You are creating the variables inside the
    function and they work OK and get the values you input. When you get to the
    end of the function it in effect closes and those variables and the values
    they hold are lost. You need to return the values out of the function.

    I'll put an example together & post it in a while
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "bryan.rutkowski@gmail.com" wrote:

    > Here is the exact output when I run it.
    >
    > PS C:\Documents and Settings\bryan.rutkowski> function create-user{
    > >>
    > >> # Asks the user to input the Account name they wish to create.
    > >>
    > >> $strLogonName = read-host "Enter the users Logon Name, use ** as a wildcard where you would want the State Code to go

    > "
    > >>
    > >> # Asks the user to input the Domain Suffix of the user.
    > >>
    > >> $strDomainSuffix = read-host "Enter the users Domain Suffix, ex. @cars.com"
    > >>
    > >> # Asks the user to input the First Name of the user.
    > >>
    > >> $strFirstName = read-host "Enter the users First Name, use ** as a wildcard where you would want the State Code to go

    > "
    > >>
    > >> # Asks the user to input the Last Name of the user.
    > >>
    > >> $strLastName = read-host "Enter the users Last Name, use ** as a wildcard where you would want the State Code to go"
    > >>
    > >> # Asks the user to input the Description for the user.
    > >>
    > >> $strDescription = read-host "Enter the users Description, use ** as a wildcard where you would want the State Code to

    > go"
    > >>
    > >> # Asks the user to input the Password of the user.
    > >>
    > >> $strPassword = read-host "Enter the users Password, use ** as a wildcard where you would want the State Code to go"
    > >>
    > >> # Asks the user to input the OU Path they wish to create the user in, do not include the Domain DN.
    > >>
    > >> $strOUPath = read-host "Enter the OU Path. Leave out all domain specific information, use ## as a wildcard where you

    > would want the State Code to go"
    > >> }
    > >>

    > PS C:\Documents and Settings\bryan.rutkowski> create-user
    > Enter the users Logon Name, use ** as a wildcard where you would want
    > the State Code to go: ** Logon Name
    > Enter the users Domain Suffix, ex. @cars.com: @cars.com
    > Enter the users First Name, use ** as a wildcard where you would want
    > the State Code to go: John
    > Enter the users Last Name, use ** as a wildcard where you would want
    > the State Code to go: Doe
    > Enter the users Description, use ** as a wildcard where you would want
    > the State Code to go: John Doe
    > Enter the users Password, use ** as a wildcard where you would want
    > the State Code to go: 12345
    > Enter the OU Path. Leave out all domain specific information, use ##
    > as a wildcard where you would want the State Code t
    > o go: ou=cars
    > PS C:\Documents and Settings\bryan.rutkowski> $strlogonname
    > PS C:\Documents and Settings\bryan.rutkowski> $strDomainSuffix
    > PS C:\Documents and Settings\bryan.rutkowski>
    >
    > As you can see nothing is set in the variables. Am I doing something
    > wrong, or calling them the wrong way?
    >
    >
    >
    > On Feb 9, 2:19 pm, RichS <R...@discussions.microsoft.com> wrote:
    > > How are you returning this information from your function?
    > >
    > > I've just tried the function & the variables seem to pick up the input OK
    > > --
    > > Richard Siddaway
    > > Please note that all scripts are supplied "as is" and with no warranty
    > > Blog:http://richardsiddaway.spaces.live.com/
    > > PowerShell User Group:http://www.get-psuguk.org.uk
    > >
    > > "bryan.rutkow...@gmail.com" wrote:
    > > > I am prompting the user to enter new user information in using Read-
    > > > Host. The only problem is, when I run my function and enter the data
    > > > in, it doesn't exists, all the variables are null. If I just do each
    > > > one, one at a time outside of the function I can get read-host to work
    > > > fine. But whenever I try to do more then one at a time it doesn't
    > > > work at all. Below is my code. So once I run the function create-
    > > > user and try to get the data in any of the variables (ex.
    > > > $strLogonName) they are all ways null. Does anyone have any ideas?

    > >
    > > > function create-user{

    > >
    > > > # Asks the user to input the Account name they wish to create.

    > >
    > > > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Domain Suffix of the user.

    > >
    > > > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > > > @cars.com"

    > >
    > > > # Asks the user to input the First Name of the user.

    > >
    > > > $strFirstName = read-host "Enter the users First Name, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Last Name of the user.

    > >
    > > > $strLastName = read-host "Enter the users Last Name, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Description for the user.

    > >
    > > > $strDescription = read-host "Enter the users Description, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Password of the user.

    > >
    > > > $strPassword = read-host "Enter the users Password, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the OU Path they wish to create the user in,
    > > > do not include the Domain DN.

    > >
    > > > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > > > specific information, use ## as a wildcard where you would want the
    > > > State Code to go"
    > > > }

    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    Stephen Merkel Guest

    RE: Read-Host issue, won't store to variable when using multiple read-

    You could give your variables global scope (i.e. outside of the script that
    creates the function) by addidng 'global:' between the '$' and the name of
    the variable.

    Like this $global:strLogonName
    --
    Merkel-DBA


    "bryan.rutkowski@gmail.com" wrote:

    > I am prompting the user to enter new user information in using Read-
    > Host. The only problem is, when I run my function and enter the data
    > in, it doesn't exists, all the variables are null. If I just do each
    > one, one at a time outside of the function I can get read-host to work
    > fine. But whenever I try to do more then one at a time it doesn't
    > work at all. Below is my code. So once I run the function create-
    > user and try to get the data in any of the variables (ex.
    > $strLogonName) they are all ways null. Does anyone have any ideas?
    >
    > function create-user{
    >
    > # Asks the user to input the Account name they wish to create.
    >
    > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Domain Suffix of the user.
    >
    > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > @cars.com"
    >
    > # Asks the user to input the First Name of the user.
    >
    > $strFirstName = read-host "Enter the users First Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Last Name of the user.
    >
    > $strLastName = read-host "Enter the users Last Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Description for the user.
    >
    > $strDescription = read-host "Enter the users Description, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Password of the user.
    >
    > $strPassword = read-host "Enter the users Password, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the OU Path they wish to create the user in,
    > do not include the Domain DN.
    >
    > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > specific information, use ## as a wildcard where you would want the
    > State Code to go"
    > }
    >
    >


      My System SpecsSystem Spec

  6. #6


    RichS Guest

    Re: Read-Host issue, won't store to variable when using multiple r

    Based on your function this is how you could get it to work in the way you
    seem to want to. Notice the return statement at the end of the function

    function create-user{

    # Asks the user to input the Account name they wish to create.

    $strLogonName = read-host "Enter the users Logon Name, use ** as a
    wildcard where you would want the State Code to go"

    # Asks the user to input the Domain Suffix of the user.

    $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    @cars.com"


    # Asks the user to input the First Name of the user.

    $strFirstName = read-host "Enter the users First Name, use ** as a
    wildcard where you would want the State Code to go"


    # Asks the user to input the Last Name of the user.

    $strLastName = read-host "Enter the users Last Name, use ** as a
    wildcard where you would want the State Code to go"


    # Asks the user to input the Description for the user.

    $strDescription = read-host "Enter the users Description, use ** as a
    wildcard where you would want the State Code to go"


    # Asks the user to input the Password of the user.

    $strPassword = read-host "Enter the users Password, use ** as a
    wildcard where you would want the State Code to go"


    # Asks the user to input the OU Path they wish to create the user in,do not
    include the Domain DN.

    $strOUPath = read-host "Enter the OU Path. Leave out all domain
    specific information, use ## as a wildcard where you would want the
    State Code to go"


    return $strLogonName, $strDomainSuffix, $strFirstName, $strLastName,
    $strDescription, $strPassword, $strOUPath
    }

    $strLogonName = ""
    $strDomainSuffix = ""
    $strFirstName = ""
    $strLastName = ""
    $strDescription = ""
    $strPassword = ""
    $strOUPath = ""

    create-user

    $strLogonName = ""
    $strDomainSuffix
    $strFirstName
    $strLastName
    $strDescription
    $strPassword
    $strOUPath
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "bryan.rutkowski@gmail.com" wrote:

    > Here is the exact output when I run it.
    >
    > PS C:\Documents and Settings\bryan.rutkowski> function create-user{
    > >>
    > >> # Asks the user to input the Account name they wish to create.
    > >>
    > >> $strLogonName = read-host "Enter the users Logon Name, use ** as a wildcard where you would want the State Code to go

    > "
    > >>
    > >> # Asks the user to input the Domain Suffix of the user.
    > >>
    > >> $strDomainSuffix = read-host "Enter the users Domain Suffix, ex. @cars.com"
    > >>
    > >> # Asks the user to input the First Name of the user.
    > >>
    > >> $strFirstName = read-host "Enter the users First Name, use ** as a wildcard where you would want the State Code to go

    > "
    > >>
    > >> # Asks the user to input the Last Name of the user.
    > >>
    > >> $strLastName = read-host "Enter the users Last Name, use ** as a wildcard where you would want the State Code to go"
    > >>
    > >> # Asks the user to input the Description for the user.
    > >>
    > >> $strDescription = read-host "Enter the users Description, use ** as a wildcard where you would want the State Code to

    > go"
    > >>
    > >> # Asks the user to input the Password of the user.
    > >>
    > >> $strPassword = read-host "Enter the users Password, use ** as a wildcard where you would want the State Code to go"
    > >>
    > >> # Asks the user to input the OU Path they wish to create the user in, do not include the Domain DN.
    > >>
    > >> $strOUPath = read-host "Enter the OU Path. Leave out all domain specific information, use ## as a wildcard where you

    > would want the State Code to go"
    > >> }
    > >>

    > PS C:\Documents and Settings\bryan.rutkowski> create-user
    > Enter the users Logon Name, use ** as a wildcard where you would want
    > the State Code to go: ** Logon Name
    > Enter the users Domain Suffix, ex. @cars.com: @cars.com
    > Enter the users First Name, use ** as a wildcard where you would want
    > the State Code to go: John
    > Enter the users Last Name, use ** as a wildcard where you would want
    > the State Code to go: Doe
    > Enter the users Description, use ** as a wildcard where you would want
    > the State Code to go: John Doe
    > Enter the users Password, use ** as a wildcard where you would want
    > the State Code to go: 12345
    > Enter the OU Path. Leave out all domain specific information, use ##
    > as a wildcard where you would want the State Code t
    > o go: ou=cars
    > PS C:\Documents and Settings\bryan.rutkowski> $strlogonname
    > PS C:\Documents and Settings\bryan.rutkowski> $strDomainSuffix
    > PS C:\Documents and Settings\bryan.rutkowski>
    >
    > As you can see nothing is set in the variables. Am I doing something
    > wrong, or calling them the wrong way?
    >
    >
    >
    > On Feb 9, 2:19 pm, RichS <R...@discussions.microsoft.com> wrote:
    > > How are you returning this information from your function?
    > >
    > > I've just tried the function & the variables seem to pick up the input OK
    > > --
    > > Richard Siddaway
    > > Please note that all scripts are supplied "as is" and with no warranty
    > > Blog:http://richardsiddaway.spaces.live.com/
    > > PowerShell User Group:http://www.get-psuguk.org.uk
    > >
    > > "bryan.rutkow...@gmail.com" wrote:
    > > > I am prompting the user to enter new user information in using Read-
    > > > Host. The only problem is, when I run my function and enter the data
    > > > in, it doesn't exists, all the variables are null. If I just do each
    > > > one, one at a time outside of the function I can get read-host to work
    > > > fine. But whenever I try to do more then one at a time it doesn't
    > > > work at all. Below is my code. So once I run the function create-
    > > > user and try to get the data in any of the variables (ex.
    > > > $strLogonName) they are all ways null. Does anyone have any ideas?

    > >
    > > > function create-user{

    > >
    > > > # Asks the user to input the Account name they wish to create.

    > >
    > > > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Domain Suffix of the user.

    > >
    > > > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > > > @cars.com"

    > >
    > > > # Asks the user to input the First Name of the user.

    > >
    > > > $strFirstName = read-host "Enter the users First Name, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Last Name of the user.

    > >
    > > > $strLastName = read-host "Enter the users Last Name, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Description for the user.

    > >
    > > > $strDescription = read-host "Enter the users Description, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the Password of the user.

    > >
    > > > $strPassword = read-host "Enter the users Password, use ** as a
    > > > wildcard where you would want the State Code to go"

    > >
    > > > # Asks the user to input the OU Path they wish to create the user in,
    > > > do not include the Domain DN.

    > >
    > > > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > > > specific information, use ## as a wildcard where you would want the
    > > > State Code to go"
    > > > }

    >
    >
    >


      My System SpecsSystem Spec

  7. #7


    bryan.rutkowski@gmail.com Guest

    Re: Read-Host issue, won't store to variable when using multiple r

    Richard when I tried your method I was able to get the data output to
    the screen right after the function completes, but the variables still
    were not kept. They were blank like before.

    I tried using Stephen's method of making them global. That worked.

    What I am trying to do is get all this user information then use it to
    create a user in every domain in our forest. So as soon as the user
    inputs the data the rest of my script gets all the domain information
    the proceeds to create a user in each domain.

    On Feb 9, 3:09 pm, RichS <R...@discussions.microsoft.com> wrote:
    > Based on your function this is how you could get it to work in the way you
    > seem to want to. Notice the return statement at the end of the function
    >
    > function create-user{
    >
    > # Asks the user to input the Account name they wish to create.
    >
    > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Domain Suffix of the user.
    >
    > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > @cars.com"
    >
    > # Asks the user to input the First Name of the user.
    >
    > $strFirstName = read-host "Enter the users First Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Last Name of the user.
    >
    > $strLastName = read-host "Enter the users Last Name, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Description for the user.
    >
    > $strDescription = read-host "Enter the users Description, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the Password of the user.
    >
    > $strPassword = read-host "Enter the users Password, use ** as a
    > wildcard where you would want the State Code to go"
    >
    > # Asks the user to input the OU Path they wish to create the user in,do not
    > include the Domain DN.
    >
    > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > specific information, use ## as a wildcard where you would want the
    > State Code to go"
    >
    > return $strLogonName, $strDomainSuffix, $strFirstName, $strLastName,
    > $strDescription, $strPassword, $strOUPath
    >
    > }
    >
    > $strLogonName = ""
    > $strDomainSuffix = ""
    > $strFirstName = ""
    > $strLastName = ""
    > $strDescription = ""
    > $strPassword = ""
    > $strOUPath = ""
    >
    > create-user
    >
    > $strLogonName = ""
    > $strDomainSuffix
    > $strFirstName
    > $strLastName
    > $strDescription
    > $strPassword
    > $strOUPath
    > --
    > Richard Siddaway
    > Please note that all scripts are supplied "as is" and with no warranty
    > Blog:http://richardsiddaway.spaces.live.com/
    > PowerShell User Group:http://www.get-psuguk.org.uk
    >
    > "bryan.rutkow...@gmail.com" wrote:
    > > Here is the exact output when I run it.

    >
    > > PS C:\Documents and Settings\bryan.rutkowski> function create-user{

    >
    > > >> # Asks the user to input the Account name they wish to create.

    >
    > > >> $strLogonName = read-host "Enter the users Logon Name, use ** as a wildcard where you would want the State Code to go

    > > "

    >
    > > >> # Asks the user to input the Domain Suffix of the user.

    >
    > > >> $strDomainSuffix = read-host "Enter the users Domain Suffix, ex. @cars.com"

    >
    > > >> # Asks the user to input the First Name of the user.

    >
    > > >> $strFirstName = read-host "Enter the users First Name, use ** as a wildcard where you would want the State Code to go

    > > "

    >
    > > >> # Asks the user to input the Last Name of the user.

    >
    > > >> $strLastName = read-host "Enter the users Last Name, use ** as a wildcard where you would want the State Code to go"

    >
    > > >> # Asks the user to input the Description for the user.

    >
    > > >> $strDescription = read-host "Enter the users Description, use ** as a wildcard where you would want the State Code to

    > > go"

    >
    > > >> # Asks the user to input the Password of the user.

    >
    > > >> $strPassword = read-host "Enter the users Password, use ** as a wildcard where you would want the State Code to go"

    >
    > > >> # Asks the user to input the OU Path they wish to create the user in, do not include the Domain DN.

    >
    > > >> $strOUPath = read-host "Enter the OU Path. Leave out all domain specific information, use ## as a wildcard where you

    > > would want the State Code to go"
    > > >> }

    >
    > > PS C:\Documents and Settings\bryan.rutkowski> create-user
    > > Enter the users Logon Name, use ** as a wildcard where you would want
    > > the State Code to go: ** Logon Name
    > > Enter the users Domain Suffix, ex. @cars.com: @cars.com
    > > Enter the users First Name, use ** as a wildcard where you would want
    > > the State Code to go: John
    > > Enter the users Last Name, use ** as a wildcard where you would want
    > > the State Code to go: Doe
    > > Enter the users Description, use ** as a wildcard where you would want
    > > the State Code to go: John Doe
    > > Enter the users Password, use ** as a wildcard where you would want
    > > the State Code to go: 12345
    > > Enter the OU Path. Leave out all domain specific information, use ##
    > > as a wildcard where you would want the State Code t
    > > o go: ou=cars
    > > PS C:\Documents and Settings\bryan.rutkowski> $strlogonname
    > > PS C:\Documents and Settings\bryan.rutkowski> $strDomainSuffix
    > > PS C:\Documents and Settings\bryan.rutkowski>

    >
    > > As you can see nothing is set in the variables. Am I doing something
    > > wrong, or calling them the wrong way?

    >
    > > On Feb 9, 2:19 pm, RichS <R...@discussions.microsoft.com> wrote:
    > > > How are you returning this information from your function?

    >
    > > > I've just tried the function & the variables seem to pick up the input OK
    > > > --
    > > > Richard Siddaway
    > > > Please note that all scripts are supplied "as is" and with no warranty
    > > > Blog:http://richardsiddaway.spaces.live.com/
    > > > PowerShell User Group:http://www.get-psuguk.org.uk

    >
    > > > "bryan.rutkow...@gmail.com" wrote:
    > > > > I am prompting the user to enter new user information in using Read-
    > > > > Host. The only problem is, when I run my function and enter the data
    > > > > in, it doesn't exists, all the variables are null. If I just do each
    > > > > one, one at a time outside of the function I can get read-host to work
    > > > > fine. But whenever I try to do more then one at a time it doesn't
    > > > > work at all. Below is my code. So once I run the function create-
    > > > > user and try to get the data in any of the variables (ex.
    > > > > $strLogonName) they are all ways null. Does anyone have any ideas?

    >
    > > > > function create-user{

    >
    > > > > # Asks the user to input the Account name they wish to create.

    >
    > > > > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > > > > wildcard where you would want the State Code to go"

    >
    > > > > # Asks the user to input the Domain Suffix of the user.

    >
    > > > > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > > > > @cars.com"

    >
    > > > > # Asks the user to input the First Name of the user.

    >
    > > > > $strFirstName = read-host "Enter the users First Name, use ** as a
    > > > > wildcard where you would want the State Code to go"

    >
    > > > > # Asks the user to input the Last Name of the user.

    >
    > > > > $strLastName = read-host "Enter the users Last Name, use ** as a
    > > > > wildcard where you would want the State Code to go"

    >
    > > > > # Asks the user to input the Description for the user.

    >
    > > > > $strDescription = read-host "Enter the users Description, use ** as a
    > > > > wildcard where you would want the State Code to go"

    >
    > > > > # Asks the user to input the Password of the user.

    >
    > > > > $strPassword = read-host "Enter the users Password, use ** as a
    > > > > wildcard where you would want the State Code to go"

    >
    > > > > # Asks the user to input the OU Path they wish to create the user in,
    > > > > do not include the Domain DN.

    >
    > > > > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > > > > specific information, use ## as a wildcard where you would want the
    > > > > State Code to go"
    > > > > }




      My System SpecsSystem Spec

  8. #8


    RichS Guest

    Re: Read-Host issue, won't store to variable when using multiple r

    If you put the rest of your code after the function it should work but if
    you've got it work thats good
    --
    Richard Siddaway
    Please note that all scripts are supplied "as is" and with no warranty
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "bryan.rutkowski@gmail.com" wrote:

    > Richard when I tried your method I was able to get the data output to
    > the screen right after the function completes, but the variables still
    > were not kept. They were blank like before.
    >
    > I tried using Stephen's method of making them global. That worked.
    >
    > What I am trying to do is get all this user information then use it to
    > create a user in every domain in our forest. So as soon as the user
    > inputs the data the rest of my script gets all the domain information
    > the proceeds to create a user in each domain.
    >
    > On Feb 9, 3:09 pm, RichS <R...@discussions.microsoft.com> wrote:
    > > Based on your function this is how you could get it to work in the way you
    > > seem to want to. Notice the return statement at the end of the function
    > >
    > > function create-user{
    > >
    > > # Asks the user to input the Account name they wish to create.
    > >
    > > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > > wildcard where you would want the State Code to go"
    > >
    > > # Asks the user to input the Domain Suffix of the user.
    > >
    > > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > > @cars.com"
    > >
    > > # Asks the user to input the First Name of the user.
    > >
    > > $strFirstName = read-host "Enter the users First Name, use ** as a
    > > wildcard where you would want the State Code to go"
    > >
    > > # Asks the user to input the Last Name of the user.
    > >
    > > $strLastName = read-host "Enter the users Last Name, use ** as a
    > > wildcard where you would want the State Code to go"
    > >
    > > # Asks the user to input the Description for the user.
    > >
    > > $strDescription = read-host "Enter the users Description, use ** as a
    > > wildcard where you would want the State Code to go"
    > >
    > > # Asks the user to input the Password of the user.
    > >
    > > $strPassword = read-host "Enter the users Password, use ** as a
    > > wildcard where you would want the State Code to go"
    > >
    > > # Asks the user to input the OU Path they wish to create the user in,do not
    > > include the Domain DN.
    > >
    > > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > > specific information, use ## as a wildcard where you would want the
    > > State Code to go"
    > >
    > > return $strLogonName, $strDomainSuffix, $strFirstName, $strLastName,
    > > $strDescription, $strPassword, $strOUPath
    > >
    > > }
    > >
    > > $strLogonName = ""
    > > $strDomainSuffix = ""
    > > $strFirstName = ""
    > > $strLastName = ""
    > > $strDescription = ""
    > > $strPassword = ""
    > > $strOUPath = ""
    > >
    > > create-user
    > >
    > > $strLogonName = ""
    > > $strDomainSuffix
    > > $strFirstName
    > > $strLastName
    > > $strDescription
    > > $strPassword
    > > $strOUPath
    > > --
    > > Richard Siddaway
    > > Please note that all scripts are supplied "as is" and with no warranty
    > > Blog:http://richardsiddaway.spaces.live.com/
    > > PowerShell User Group:http://www.get-psuguk.org.uk
    > >
    > > "bryan.rutkow...@gmail.com" wrote:
    > > > Here is the exact output when I run it.

    > >
    > > > PS C:\Documents and Settings\bryan.rutkowski> function create-user{

    > >
    > > > >> # Asks the user to input the Account name they wish to create.

    > >
    > > > >> $strLogonName = read-host "Enter the users Logon Name, use ** as a wildcard where you would want the State Code to go
    > > > "

    > >
    > > > >> # Asks the user to input the Domain Suffix of the user.

    > >
    > > > >> $strDomainSuffix = read-host "Enter the users Domain Suffix, ex. @cars.com"

    > >
    > > > >> # Asks the user to input the First Name of the user.

    > >
    > > > >> $strFirstName = read-host "Enter the users First Name, use ** as a wildcard where you would want the State Code to go
    > > > "

    > >
    > > > >> # Asks the user to input the Last Name of the user.

    > >
    > > > >> $strLastName = read-host "Enter the users Last Name, use ** as a wildcard where you would want the State Code to go"

    > >
    > > > >> # Asks the user to input the Description for the user.

    > >
    > > > >> $strDescription = read-host "Enter the users Description, use ** as a wildcard where you would want the State Code to
    > > > go"

    > >
    > > > >> # Asks the user to input the Password of the user.

    > >
    > > > >> $strPassword = read-host "Enter the users Password, use ** as a wildcard where you would want the State Code to go"

    > >
    > > > >> # Asks the user to input the OU Path they wish to create the user in, do not include the Domain DN.

    > >
    > > > >> $strOUPath = read-host "Enter the OU Path. Leave out all domain specific information, use ## as a wildcard where you
    > > > would want the State Code to go"
    > > > >> }

    > >
    > > > PS C:\Documents and Settings\bryan.rutkowski> create-user
    > > > Enter the users Logon Name, use ** as a wildcard where you would want
    > > > the State Code to go: ** Logon Name
    > > > Enter the users Domain Suffix, ex. @cars.com: @cars.com
    > > > Enter the users First Name, use ** as a wildcard where you would want
    > > > the State Code to go: John
    > > > Enter the users Last Name, use ** as a wildcard where you would want
    > > > the State Code to go: Doe
    > > > Enter the users Description, use ** as a wildcard where you would want
    > > > the State Code to go: John Doe
    > > > Enter the users Password, use ** as a wildcard where you would want
    > > > the State Code to go: 12345
    > > > Enter the OU Path. Leave out all domain specific information, use ##
    > > > as a wildcard where you would want the State Code t
    > > > o go: ou=cars
    > > > PS C:\Documents and Settings\bryan.rutkowski> $strlogonname
    > > > PS C:\Documents and Settings\bryan.rutkowski> $strDomainSuffix
    > > > PS C:\Documents and Settings\bryan.rutkowski>

    > >
    > > > As you can see nothing is set in the variables. Am I doing something
    > > > wrong, or calling them the wrong way?

    > >
    > > > On Feb 9, 2:19 pm, RichS <R...@discussions.microsoft.com> wrote:
    > > > > How are you returning this information from your function?

    > >
    > > > > I've just tried the function & the variables seem to pick up the input OK
    > > > > --
    > > > > Richard Siddaway
    > > > > Please note that all scripts are supplied "as is" and with no warranty
    > > > > Blog:http://richardsiddaway.spaces.live.com/
    > > > > PowerShell User Group:http://www.get-psuguk.org.uk

    > >
    > > > > "bryan.rutkow...@gmail.com" wrote:
    > > > > > I am prompting the user to enter new user information in using Read-
    > > > > > Host. The only problem is, when I run my function and enter the data
    > > > > > in, it doesn't exists, all the variables are null. If I just do each
    > > > > > one, one at a time outside of the function I can get read-host to work
    > > > > > fine. But whenever I try to do more then one at a time it doesn't
    > > > > > work at all. Below is my code. So once I run the function create-
    > > > > > user and try to get the data in any of the variables (ex.
    > > > > > $strLogonName) they are all ways null. Does anyone have any ideas?

    > >
    > > > > > function create-user{

    > >
    > > > > > # Asks the user to input the Account name they wish to create.

    > >
    > > > > > $strLogonName = read-host "Enter the users Logon Name, use ** as a
    > > > > > wildcard where you would want the State Code to go"

    > >
    > > > > > # Asks the user to input the Domain Suffix of the user.

    > >
    > > > > > $strDomainSuffix = read-host "Enter the users Domain Suffix, ex.
    > > > > > @cars.com"

    > >
    > > > > > # Asks the user to input the First Name of the user.

    > >
    > > > > > $strFirstName = read-host "Enter the users First Name, use ** as a
    > > > > > wildcard where you would want the State Code to go"

    > >
    > > > > > # Asks the user to input the Last Name of the user.

    > >
    > > > > > $strLastName = read-host "Enter the users Last Name, use ** as a
    > > > > > wildcard where you would want the State Code to go"

    > >
    > > > > > # Asks the user to input the Description for the user.

    > >
    > > > > > $strDescription = read-host "Enter the users Description, use ** as a
    > > > > > wildcard where you would want the State Code to go"

    > >
    > > > > > # Asks the user to input the Password of the user.

    > >
    > > > > > $strPassword = read-host "Enter the users Password, use ** as a
    > > > > > wildcard where you would want the State Code to go"

    > >
    > > > > > # Asks the user to input the OU Path they wish to create the user in,
    > > > > > do not include the Domain DN.

    > >
    > > > > > $strOUPath = read-host "Enter the OU Path. Leave out all domain
    > > > > > specific information, use ## as a wildcard where you would want the
    > > > > > State Code to go"
    > > > > > }

    >
    >
    >


      My System SpecsSystem Spec


Read-Host issue, won't store to variable when using multiple read-host lines
Similar Threads
Thread Forum
How to read a user input value from command line and store it in a variable? VB Script
Can't read physical CD without rebooting host Virtual Server
Read-Host values into an Array PowerShell
Default value for Read-Host PowerShell
BUG? Read-Host 'xxx' can't read '!xxx' PowerShell