Author Posts

July 28, 2017 at 10:23 am

HI, first post here, so please be gentle.
I've started to learn Powershell properly after fudging stuff from the internet all the time. So, this is my first attempt at a script from scratch!

Desired outcome – to set the Proxyaddresses attribute of an Active Directory User by using powershell and text prompts (to help service desk change Office 365 alias's)

Problems – When using PSSession, the error returned:

"Start-ADSyncSyncCycle : The term 'Start-ADSyncSyncCycle' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

If I try to connect as a one-liner it connects, then successfully runs

Enter-PSSession -ComputerName server1
Start-ADSyncSyncCycle -PolicyType Delta
Exit-PSSession

Can anyone point me in the direction of how I connect and run the Sync command remotely as the Azure AD Connect tool is not installed where I would like to run the script.
Many thanks in advance.
🙂

Here is the PS:

cls
$User = Read-Host -Prompt "Please enter user logon name"
$NewEmailAd = Read-Host -Prompt "Enter New Email Address"
$Primary = Read-Host -Prompt "Type 1 for Primary email or 2 for Alias"
$ShowDetails = Get-ADUser -Identity $User -Properties * | fl Name,SamAccountname,proxyaddresses
Write-Host -foregroundcolor Green "Current User had the following addresses"
$ShowDetails

If ($Primary = 1)
{Set-ADUSer -Identity $User -Add @{Proxyaddresses="SMTP:"+$NewEmailAd}}
If ($Primary = 2)
{Set-ADUSer -Identity $User -Add @{Proxyaddresses="smtp:"+$NewEmailAd}}

Enter-PSSession -ComputerName server1

Start-ADSyncSyncCycle -PolicyType Delta
Exit-PSSession

Write-Host -ForegroundColor Green -BackgroundColor black "Complete"

July 28, 2017 at 10:46 am

Have a look at this http://www.expta.com/2017/01/how-to-trigger-aad-connect-sync-from.html once you have connected to your Azure AD in a PSSession you need to import the ADSync module before you can use the command Start-ADSyncSyncCycle. Same as if you were using PSSession to your exchange server or Lync server

July 28, 2017 at 11:30 am

Hi and thanks for the pointer!
I've seen this part from the comments of the article which looks good:

Invoke-Command -ScriptBlock {Import-Module "C:\Program Files\Microsoft Azure AD Sync\Bin\ADSync\ADSync.psd1"}
Invoke-Command -ScriptBlock {Start-ADSyncSyncCycle -PolicyType Delta}

Which makes sense, but doesn't work when run within the script. If I run it line by line (not in the whole script) it works fine.
I also navigated to the path for the ADsync.psd1 files through the PSSession to ensure it was there.

What am I doing wrong ? Why wont it run within the script? 🙁

Thanks again for your help.

July 28, 2017 at 12:11 pm

Try using New-PSSesion when running in a script as it gives a persistent connection as opposed to Enter-PSSession gives an interactive session

July 28, 2017 at 12:13 pm

Put this function in your code and call the function when you need to do the sync

function Start-DeltaSync

{

$session = New-PSSession -ComputerName

Invoke-Command -Session $session -ScriptBlock {Start-ADSyncSyncCycle Delta}

Remove-PSSession $session

}

July 28, 2017 at 1:12 pm

Super, thanks for the help.

Firstly I tried the New-PSSesion, but it resulted in the original error and presented the information about the remote session (Id Name,ComputerName,State,ConfigurationName,Availability)

I then tried the function suggested which worked perfectly. O365 showed the sync and the user had been updated.
Thank you very much, but any ideas why that works but the others did not work successfully?

🙂
Cheers
Fhiyll

July 28, 2017 at 4:59 pm

I believe it has something to do with the way interactive sessions work. I have had issues like you had in the past and my fix for it was the new-pssession and script block code.