Reputation: 13
Writing a script for AD sync. I want it to ask which type of sync, and based on the user input perform that sync. I see that its doing both.. Probably something simple, but Im a noob when it comes to this. 21 year old sys admin.
All help is appreciated. thanks
#Import AD Sync Module
Import-Module ADSync
#Variables
$DS = Start-ADSyncSyncCycle -PolicyType Delta
$FS = Start-ADSyncSyncCycle -PolicyType Initial
$DS = "Delta"
$FS = "Full"
$InputDS = $DS
$InputFS = $FS
#Prompt User for Sync Typec
Read-Host -Prompt "For Full Sync, Input $FS. For Delta Sync, Input $DS"
if($InputDS -eq $DS)
{
Write-Host -ForegroundColor Cyan "Starting a Delta Sync"
Start-ADSyncSyncCycle -PolicyType Delta
Write-Host -ForegroundColor Green "Delta Sync has finished"
}
if($InputFS -eq $FS)
{
Write-Host -ForegroundColor Cyan "Starting a Full Sync"
Start-ADSyncSyncCycle -PolicyType Initial
Write-Host -ForegroundColor Green "Full Sync has finished"
}
Upvotes: 0
Views: 81
Reputation: 5341
Try this out instead:
# Set user input to $Input
$UserInput = Read-Host -Prompt "Which sync type? (Delta|Full)"
# Use a switch statement instead of multiple If statements:
Switch ($UserInput ) {
'Delta' {
Write-Host -ForegroundColor Cyan "Starting a Delta Sync"
Start-ADSyncSyncCycle -PolicyType Delta
Write-Host -ForegroundColor Green "Delta Sync has finished"
}
'Full' {
Write-Host -ForegroundColor Cyan "Starting a Full Sync"
Start-ADSyncSyncCycle -PolicyType Initial
Write-Host -ForegroundColor Green "Full Sync has finished"
}
# Make an error if the user didn't enter a correct value
default {
throw 'Input not recognized, please specify either Full or Delta. Exiting...'
}
}
Code in variable definitions (like $a = Do-TheThing
) runs! The variable only gets the output. Be very careful with things like this:
$DS = Start-ADSyncSyncCycle -PolicyType Delta
$FS = Start-ADSyncSyncCycle -PolicyType Initial
That code was running both sync types like you saw instead of setting the command to be run later.
Upvotes: 1
Reputation: 60668
Cpt.Whale's answer already pointed out that both cmdlets will run:
$DS = Start-ADSyncSyncCycle -PolicyType Delta
$FS = Start-ADSyncSyncCycle -PolicyType Initial
If you want to save a command in a variable for later execution you do so like this:
$DS = { Start-ADSyncSyncCycle -PolicyType Delta } # Store the ScriptBlock
& $DS # Execute the ScriptBlock
As for how you can approach your script, you can create a Menu
like below that only accepts specific user input:
$menu = {
Clear-Host
@'
================ AD Sync ================
[1] - For Full Sync
[2] - For Delta Sync
[Q] - Quit
'@
}
$goTo = {
while(-not $goToMenu -or $goToMenu -notmatch '^y$|^n$')
{
''
$goToMenu = Read-Host 'Go back to Menu? [Y/N]'
switch($goToMenu)
{
'y' { rv selection -Scope script }
'n' { break }
}
}
}
$userInput = {Read-Host 'Selection'}
while(-not $selection -or $selection -notmatch '^1$|^2$|^q$')
{
& $menu
$selection = & $userInput
switch($selection)
{
1 {
Write-Host -ForegroundColor Cyan "Starting a Full Sync"
Start-ADSyncSyncCycle -PolicyType Initial
Write-Host -ForegroundColor Green "Full Sync has finished"
& $goTo
}
2 {
Write-Host -ForegroundColor Cyan "Starting a Delta Sync"
Start-ADSyncSyncCycle -PolicyType Delta
Write-Host -ForegroundColor Green "Delta Sync has finished"
& $goTo
}
Q { break }
}
}
Upvotes: 0