Skip to content

Instantly share code, notes, and snippets.

@nicolonsky
Last active January 8, 2019 06:13
Show Gist options
  • Save nicolonsky/483bf7b9fc3d0c238d7cdac4c2461aa3 to your computer and use it in GitHub Desktop.
Save nicolonsky/483bf7b9fc3d0c238d7cdac4c2461aa3 to your computer and use it in GitHub Desktop.
function New-Example{
[CmdletBinding()]
Param(
[Parameter(
Mandatory=$true,
Position=0)]
[ValidateScript({
$requiredProperties=@("Property1","Property2","Property3", "Property4")
$members=Get-Member -InputObject $_ -MemberType NoteProperty
$missingProperties=Compare-Object -ReferenceObject $requiredProperties -DifferenceObject $members.Name -PassThru -ErrorAction SilentlyContinue
if (-not($missingProperties)){
$true
}else{
$missingProperties | ForEach-Object {
Throw [System.Management.Automation.ValidationMetadataException] "Property: '$_' missing"
}
}
$_.PSObject.Properties | ForEach-Object {
if (([string]::IsNullOrEmpty($_.Value))){
Throw [System.Management.Automation.ValidationMetadataException] "Property '$($_.Name)' has either a NULL or empty value"
}
}
})]
[Object]
$InputObject
)
begin{}
process{
Write-Output "Main function"
}
end{}
}
$config= [PSCUSTOMOBJECT]@{
property1= "Value";
property2= "Value";
property3= "Value";
property4= $null;
}
New-Example -InputObject $config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment