We help IT Professionals succeed at work.

Use PowerShell parameter with .ps1 file name

Jason Crawford
on
84 Views
1 Endorsement
Last Modified: 2018-10-05
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

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Author

Commented:
Flawless as always.  Thank you!
Senior IT System EngineerSenior Systems Engineer
CERTIFIED EXPERT

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

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable 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

Senior IT System EngineerSenior Systems Engineer
CERTIFIED EXPERT

Commented:
Wow that is so cool.
Thanks Obda :-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.