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

Powershell Scope - global variable in if...else

$
0
0

Good Afternoon All,

I have the following bit of code, but want I want to do is make sure '$users' is global and can be used throughout the script despite being in an if..else statement?

Am I missing something really simple? I basically want the '$Users' variable to be different based on the input given by the user.

Do
{$response=Read-Host"Would you like to archive all leavers? [y|n]"
}Until ($response-eq"y"-or$response-eq"n")if ($response-eq"y")
{    $Users=Get-ADUser-SearchBase '<OU>'-Filter * -Properties cn, givenNameelse
{$SamAccountName=Read-Host"Please type the SAMAccountName of the user"$Users=Get-ADUser-SearchBase '<OU>'-Filter * -Identity $SamAccountName-Properties cn, givenName
} 

Viewing all articles
Browse latest Browse all 6937

Trending Articles