Avatar of Jason Crawford
Jason Crawford
Flag 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

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jason Crawford

ASKER
Flawless as always.  Thank you!
Albert Widjaja

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

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
Albert Widjaja

Wow that is so cool.
Thanks Obda :-)