Senior Systems Engineer
Senior Systems Engineer

Reputation: 1141

How to execute PowerShell code remotely from my Workstation ISE without install & loading module?

I have one dedicated server called PRODAZAD01-VM which is running Azure Active Directory Synch to Office 365.

[CmdletBinding()]
Param(
    [Parameter(position = 0, mandatory = $true)]
    [ValidateSet('Delta', 'Full')]
    [String]$Type,
    [string]$ComputerName = 'PRODAZAD01-VM'
)
Invoke-Command -ComputerName $ComputerName -ArgumentList $type -ScriptBlock {
    Param($Type)
    If ($Type -eq 'Full') {$Type = 'Initial'}
    Import-Module adsync
    Start-ADSyncSyncCycle -PolicyType $Type
}

How can I execute the PowerShell to force it to Synch from my PowerShell ISE on my laptop without installing the module & loading it every time?

Upvotes: 1

Views: 362

Answers (1)

postanote
postanote

Reputation: 16096

What Drew is pointing you to, is to use PowerShell Implicit Remoting to a machine with those cmdlet, thus proxying those cmdlets to your workstation for your session in the same way as you would with other cmdlets, say ADDS. For example -

See: How to use AD cmdlets *Update for OP *

[CmdletBinding()]

Param
(
    [Parameter(position = 0, mandatory = $true)]
    [ValidateSet('Delta', 'Initial')]
    [String]$Type,
    [string]$ComputerName = 'PRODAZAD01-VM',
    [string]$Creds = 'YourAdminCreds' #(Get-Credential -Credential 'YourAdminCreds')
)

$ADSession = New-PSSession -Authentication Kerberos -ComputerName $ComputerName 

Invoke-Command -Session $ADSession -ScriptBlock { 
    'ADSync','AzureADPreview','MSOnline' | 
    %{ Import-Module -Name $_ -Force } 
}

Import-PSSession -Session $ADSession
Get-Module -Name 'ADSync','AzureADPreview','MSOnline'

Connect-MsolService -Credential $Creds
Connect-AzureAD -Credential $Creds

If ($Type -eq 'Delta') { Start-ADSyncSyncCycle -PolicyType $Type }
Else { Start-ADSyncSyncCycle -PolicyType $Type }

Upvotes: 2

Related Questions