Reputation: 13
Seem to be figuring it out quite quickly though!
So basically I'm modifying a script to help us speed the employee joining process, ill have a few questions, but my first is trying to get the user account mail enabled in exchange after its created. Ive put some options in there so our guys can choose which exchange database the account should be created on. What i cant work out is how to do it properly. (not sure on the correct syntax)
Originally i tried using a variable and passing the input to that, but that didn't seem to work as the variable didn't get picked up, so I'm now trying the below way
I know I've not written the below expressions correctly, as they error - so want to know how i should correctly write the below
switch ($result) {
1 { $database = "Enable-Mailbox -Identity domain\$un -Database Database1" }
2 { $database = "Enable-Mailbox -Identity domain\$un -Database Database2" }
3 { $database = "Enable-Mailbox -Identity domain\$un -Database Database3" }
}
Help very much appreciated :)
Upvotes: 1
Views: 83
Reputation: 2229
My recommendation in situations like this is to separate the data from the code, as this makes maintenance far more simple later on. It also enables you to re-use the data. In this instance, I'd create a hash table, e.g.:
Set-StrictMode -Version 2;
[Int32] $Local:intHashKey = 0;
[Int32] $Local:intSelection = 0;
[String] $Local:strDatabase = '';
[Hashtable] $Local:objExchangeDBLookup = @{ 1 = 'Database1';
2 = 'Database2';
3 = 'Database3'
};
# Display options and get selection.
do {
Foreach ( $intHashKey in ($objExchangeDBLookup.Keys | Sort-Object) ) {
Write-Host -Object ( '{0}. {1}' -f $intHashKey, $objExchangeDBLookup[$intHashKey] );
} #Foreach
$intSelection = Read-Host -Prompt "`nPlease select a database";
} until ( $objExchangeDBLookup.ContainsKey( $intSelection ) );
# Create Mailbox.
try {
$strDatabase = $objExchangeDBLookup[$intSelection];
Write-Host -Object ( 'Creating mailbox on DB "{0}"...' -f $strDatabase );
Enable-Mailbox -Identity domain\$un -Database $strDatabase;
} #try
catch [System.Exception] {
# Something went wrong.
} #catch
The code is quit verbose and deliberately uses static data types for hardening purposes.
Upvotes: 1
Reputation: 13176
I'd say simply remove the quotes around your commands, now they're only strings:
switch ($result) {
1 { $database = Enable-Mailbox -Identity domain\$un -Database Database1 }
2 { $database = Enable-Mailbox -Identity domain\$un -Database Database2 }
3 { $database = Enable-Mailbox -Identity domain\$un -Database Database3 }
}
Upvotes: 3