Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Variable not staying stored

$
0
0

Trying to figure out why a variable I'm declaring is not being used.  I'm wondering if there is some behavior that I'm not aware of and hoping some can enlighten me.  In the beginning of my script, I'm basically creating a function asking user for input of an AD user id and store it as a variable.  I then use that variable to check if it's in AD.  It loops until a valid user id is entered.  It then takes userid variable and checks to see if a folder with the name of the userid variable exists in a specified directory.

The code seems to execute as expected with the exception that userid variable is not being carried through to the bottom of the script.  To validate it, I wrote out what the script saw as the variables.  Below is the script and the output...Any ideas?

Code:

#Function to obtain userid and check if id exists in Active Directory
Function IDCHECK
{
$error.clear()
$useid = Read-Host 'Enter the User ID to set the permission'
Try {get-aduser $useid > $null}
Catch {"UserID does not exist.  Check the UserID and try again."}
IF (!$error)
 {
    Write-Host $useid "exists in AD...proceeding" -foregroundcolor Green
 }
}
Do {IDCHECK}
While ($error.count -gt 0)
#Test if folder with name of userid exists
IF (test-path C:\Temp\$useid)
 {
 $homefolder="C:\Temp\$useid"
 Write-Host "Folder exists...proceeding to apply permission" -foregroundcolor Green
    Write-Host "Processing User:" $useid
 Write-Host "Current Directory to act upon:" $homefolder
    }

- - - - - - - - - - -

Here is the output:

~~~~~~~~ Folder Permission Application Script ~~~~~~~

Enter the User ID to set the permission: tdr

tdr exists in AD...proceeding

Folder exists...proceeding to apply permission

Processing User:

Current Directory to act upon: C:\Temp\

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles