Avatar of Jason Crawford
Jason CrawfordFlag for United States of America

asked on 

Use PowerShell parameter with .ps1 file name

Below is a simple script to force either a full or delta sync with Azure AD Connect.  What I want to be able to do is call the .ps1 file from a PowerShell command prompt and immediately follow the file name with the -Type parameter so it would look something like this:

> .\Force-ADSync.ps1 -Type Delta

or

> .\Force-ADSync.ps1 -Type Full

Here is the script I'm working with:

function Force-ADSync {
    [CmdletBinding()]
    Param (
        [Parameter(position=0,mandatory=$true)][String]$type
    )

    switch ($type) {
        Delta {
            Invoke-Command -ComputerName server.domain.local -ScriptBlock {
                Import-Module adsync
                Start-ADSyncSyncCycle -PolicyType Delta
            }
        }
        Full {
            Invoke-Command -ComputerName server.domain.local -ScriptBlock {
                Import-Module adsync
                Start-ADSyncSyncCycle -PolicyType Initial
            }
        }
    }
}

Open in new window

Thanks in advance!
PowershellAzure

Avatar of undefined
Last Comment
Albert Widjaja
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Jason Crawford

ASKER

Flawless as always.  Thank you!
so if I execute the command from my laptop PowerShell ISE, do I need to change:

Line 7:
Invoke-Command -ComputerName server.domain.local

or do I need to add this two more lines:

Enter-PSSession -ComputerName server.domain.local
Import-Module ADSync 

Open in new window

Avatar of oBdA
oBdA

You can just define it as an argument as well, with a default value of whatever system you use most:
[CmdletBinding()]
Param(
	[Parameter(position=0,mandatory=$true)]
	[ValidateSet('Delta', 'Full')]
	[String]$Type,
	[string]$ComputerName = 'server.domain.local'
)
Invoke-Command -ComputerName $ComputerName -ArgumentList $type -ScriptBlock {
	Param($Type)
	If ($Type -eq 'Full') {$Type = 'Initial'}
	Import-Module adsync
	Start-ADSyncSyncCycle -PolicyType $Type
}

Open in new window

Wow that is so cool.
Thanks Obda :-)
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo