Using PSSession in a script to run a cmdlet not installed on local machine

Welcome Forums General PowerShell Q&A Using PSSession in a script to run a cmdlet not installed on local machine

This topic contains 6 replies, has 3 voices, and was last updated by

Jon
 
Participant
1 year, 9 months ago.

  • Author
    Posts
  • #76058

    Participant
    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    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"
    
  • #76061

    Participant
    Topics: 18
    Replies: 244
    Points: 109
    Rank: Participant

    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

  • #76066

    Participant
    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    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.

  • #76075

    Participant
    Topics: 18
    Replies: 244
    Points: 109
    Rank: Participant

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

  • #76078
    Jon

    Participant
    Topics: 13
    Replies: 219
    Points: 90
    Rank: Member

    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

    }

  • #76102

    Participant
    Topics: 1
    Replies: 2
    Points: 0
    Rank: Member

    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

  • #76127
    Jon

    Participant
    Topics: 13
    Replies: 219
    Points: 90
    Rank: Member

    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.

The topic ‘Using PSSession in a script to run a cmdlet not installed on local machine’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort