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!
LVL 17
Jason CrawfordTransport NinjaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Just don't wrap it into a function; save the following as 'Force-ADSync.ps1'
Then you should use a ValidateSet for the Type argument, and you can pass the type straight down (by order!) to the remote scriptblock.
[CmdletBinding()]
Param(
	[Parameter(position=0,mandatory=$true)]
	[ValidateSet('Delta', 'Full')]
	[String]$Type
)
Invoke-Command -ComputerName server.domain.local -ArgumentList $type -ScriptBlock {
	Param($Type)
	If ($Type -eq 'Full') {$Type = 'Initial'}
	Import-Module adsync
	Start-ADSyncSyncCycle -PolicyType $Type
}

Open in new window


Edit: updated the Type in the scriptblock to Initial if Full is passed.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason CrawfordTransport NinjaAuthor Commented:
Flawless as always.  Thank you!
0
Senior IT System EngineerIT ProfessionalCommented:
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

0
oBdACommented:
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

1
Senior IT System EngineerIT ProfessionalCommented:
Wow that is so cool.
Thanks Obda :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.