Home > Mobile >  Middle name value still exists when script is run next time
Middle name value still exists when script is run next time

Time:01-07

I have a script to create a user account in my domain.

I have added a Middle name which is optional, but is required if the username is going to be a duplicate, so will take the middle name initial to differentiate.

The problem is that the following code keeps the last value. So if I specify first, middle and last name, then the next user is first and last name, then it still keeps the middle name.

Is there something that keeps the middle name empty when it runs the next time from scratch?

Do
{
#Getting variable for the First Name
Do
{
    $firstname = Read-Host "Enter in the First Name"
    Write-Host

   if ($firstname -ge 1) {
        Write-Host "First name is $firstname"
    } 

    else 
    {
        Write-Host "Please enter the first name" -ForegroundColor:Green
    }
}
Until ($firstname -ge 1)

$option = Read-Host "Does the person have a middle name? (y/n)" 
if ($option -eq "y")

{
#Getting variable for the Middle Name
Do
{
    $middlename = Read-Host "Please enter the middle name"
    Write-Host
    if ($middlename -ge 1)
    {
        Write-Host "Middle name is $middlename"
    } 
    else 
    {
        Write-Host "Please enter the middle name" -ForegroundColor:Green
    }
}
Until ($middlename -ge 1)
}
else
{
Write-Host "No middle name required"
}

#Getting variable for the Last Name
Do
{
    $lastname = Read-Host "Enter in the Last Name"
    Write-Host

    if ($lastname -ge 1) 
    {
        Write-Host "Last name is $lastname"
    } 
    else 
    {
        Write-Host "Please enter the last name" -ForegroundColor:Green
    }
}
Until ($lastname -ge 1)


    #Setting Full Name (Display Name) to the users first and last name
    if ($middlename)
    {
    $fullname = "$firstname $middlename $lastname"
    Write-Host $fullname
    sleep 5
    }
    else
    {
    $fullname = "$firstname $lastname"
    }
    #Write-Host
    #Setting username to first initial of first name along with the last name.
    $i = 1
    $logonname = $firstname.substring(0,$i)   $lastname

CodePudding user response:

This behavior comes from how you choose to launch a script. You have essentially two options in PowerShell, Invocation (Running) or dot-sourcing (Loading).

Take this short script.

#stack.ps1
"my variable should be empty $myVar"
$myvar = "cat"
"my variable should have a value $myVar"

When I run it / invoke it in PowerShell using the Invocation operator, look what happens:

PS> & .\stack.ps1
my variable should be empty
my variable should have a value cat
PS> & .\stack.ps1
my variable should be empty
my variable should have a value cat

PowerShell runs the script and so the script stores its variable values in the script scope. Meaning that when the script ends, the variables disappear.

Versus Dot-Sourcing

Now, compare that to dot-sourcing, which runs a script in your present context.

PS> . .\stack.ps1
my variable should be empty
my variable should have a value cat
PS> . .\stack.ps1
my variable should be empty cat  #!!!! the value persisted!
my variable should have a value cat

Because it runs in your present context, the values persist when the script ends.

If I had to guess, I'd say you might be dot-sourcing your script instead of invoking it. It is a very easy thing to do and would explain what you're seeing.

CodePudding user response:

Just clear the variable at the beginning of your script.

Either:

$middlename = ""

Or use Remove-Variable

Remove-Variable middlename
  •  Tags:  
  • Related