Use PowerShell parameter with .ps1 file name

Jason Crawford
Jason Crawford used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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.

Author

Commented:
Flawless as always.  Thank you!

Commented:
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

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial