Solved

Converting a string to a set of arguments - Powershell

Posted on 2009-05-07
1
767 Views
Last Modified: 2012-05-06
I have written a script (see code) to create Dynamic Distribution Groups. I have a function that creates the RecipientFilter and passes it to the New-DynamicDistributionGroup command. Problem is, it sees it as a single argument and not a valid recipient filter. If I copy the newly copy the newly created command to a .ps1 file and then call that file, it works fine, but it simply will not let me run the command from the original script. I know why this is happening, but I dont know how to fix it.

In a nutshell, the Create-Filter function returns $filter, which will look something like (Alias -like *dude*).

It then generates the code:

New-DynamicDistributionGroup -Name $DDG -domaincontroller $dc -OrganizationalUnit $ou -RecipientContainer $rc -RecipientFilter {($RecFilter)}

So, in the above example, it would have RecipientFilter {((Alias -like *dude*))} . The problem is, it does not see (Alias like *dude*) as a valid set of arguments, it sees them as a single property with no arguments attached. Since the (Alias like *dude*) property does not exist, it fails to set it.

If I have it write the command to a new script file instead, (which is the way I have it written right now) and then call the new script, it reads the whole thing in properly and executes it. I guess what I am asking is, is there a way to make it read it as property operator value and not just a property within the script that generated the RecipientFilter?

Function Create-Filter

{

	$Filter = $null

	$DoMore = "Y"

	

	While ($DoMore -eq "Y")	

	{

		Clear-Host

		Write-Host -fore White "The following is a list of common filterable properties:"

		Write-Host -fore Yellow "Alias                          AssistantName     C                     City                  Co"

		Write-Host -fore Yellow "Company                        CountryCode       CustomAttribute(1-15) Database              Department"

		Write-Host -fore Yellow "DisplayName                    EmailAddress      ExternalEmailAddress  ExternalOofOptions    FirstName"

		Write-Host -fore Yellow "HiddenFromAddressListsEnabled  IsLinked          IsMailboxEnabled      IsResource            IsShared"

		Write-Host -fore Yellow "LastName                       Manager           MobileFeaturesEnabled Name                  Office"

		Write-Host -fore Yellow "PostalCode                     RecipientType     RecipientTypeDetails  SamAccountName        ServerName"

		Write-Host -fore Yellow "StateOrProvince                StreetAddress     Title                 UMEnabled             UseerPrincipalName"

		Write-Host -fore White "---------------------------------------------------------------------------------------------------------------"

		Write-Host -fore White "The following is a list of some of the less-common filterable properties:"

		Write-Host -fore Yellow "CommonName                     DistinguishedName ExchangeVersion       ExternalOofOptions    Fax"

		Write-Host -fore Yellow "HiddenGroupMembershipEnabled   HomeMTA           HomePhone             Id                    LegacyExchangeDN"

		Write-Host -fore Yellow "ManagedBy                      MemberOfGroup     Members               MobilePhone           Name"

		Write-Host -fore Yellow "Notes                          Pager             Phone                 PostOfficeBox         PrimarySMTPAddress"
 

		$Property = Read-Host "Enter the Property you wish to filter on. (Ensure you use the correct spelling as above)"

		Write-Host

		Write-Host -fore Yellow "What kind of comparison would you like to do?"

		Write-Host -fore Yellow "Enter 1 for Equals (-eq)"

		Write-Host -fore Yellow "Enter 2 for Does not equal (-ne)"

		Write-Host -fore Yellow "Enter 3 for Like (-like)"

		Write-Host -fore Yellow "Enter 4 for Not Like (-notlike)"

		Write-Host -fore Yellow "Enter 5 for Less Than (-lt)"

		Write-Host -fore Yellow "Enter 6 for Less Than or Equal To (-le)"

		Write-Host -fore Yellow "Enter 7 for Greater Than or Equal To (-ge)"

		Write-Host -fore Yellow "Enter 8 for Greater Than (-gt)"

		$operator = Read-Host "Please select your comparison operator (1, 2, 3, 4, 5, 6, 7, or 8)"

		Write-Host

		Write-Host -fore Yellow "What value is the property being compared to?"

		$Value = Read-Host "Enter the value the property is being compared against"

		Switch ($operator)

		{

			1 {$Filter = "$Filter($Property -eq '$Value')"}

			2 {$Filter = "$Filter($Property -ne '$Value')"}

			3 {$Filter = "$Filter($Property -like '*$Value*')"}

			4 {$Filter = "$Filter($Property -notelike '*$Value*')"}

			5 {$Filter = "$Filter($Property -lt '$Value')"}

			6 {$Filter = "$Filter($Property -le '$Value')"}

			7 {$Filter = "$Filter($Property -ge '$Value')"}

			8 {$Filter = "$Filter($Property -gt '$Value')"}

		}
 

		$More = Read-Host "Would you like to add more to the filter? (Y/N)"

		If ($More -eq "Y")

			{	

				Write-Host -fore Yellow "What operator do you wish to use for the next Propery?"

				Write-Host -fore Yellow "Enter 1 for -and"

				Write-Host -fore Yellow "Enter 2 for -or"

				Write-Host -fore Yellow "Enter 3 for -and -not"

				Write-Host -fore Yellow "Enter 4 for -or -not"

				$op = Read-Host "Make your selection (1, 2, 3, or 4)"

				Switch ($op)

					{

						1 {$Filter = "$Filter -and "}

						2 {$Filter = "$Filter -or "}

						3 {$Filter = "$Filter -and -not "}

						4 {$Filter = "$Filter -or -not "}

					}

				$DoMore = "Y"

			}

		Else 

			{

				return $Filter

				$DoMore = "N"

			}

		

	}	

	

}
 

#This script will create/Update a Dynamic Distribution Group

#$ErrorActionPreference = "SilentlyContinue"

$ou = "Domain.com/Exchange/Query-Based Groups"

$DoAgain = "Y"

$Output = "CreateDDG1.ps1"

$Output2 = "UpdateDDG1.ps1"
 

While ($DoAgain -eq "Y")

{

	Clear-Host

	#Choose which Domain Controller to use for your Task

	Write-Host -fore Yellow "Please select a domain controller to process this request:"

	Write-Host -fore Yellow "Enter 4 for DC4.domain.com"

	Write-Host -fore Yellow "Enter 5 for DC5.domain.com"

	Write-Host -fore Yellow "Enter 6 for DC6.domain.com"

	Write-Host -fore Yellow "Enter 7 for DC7.domain.com"

	Write-Host -fore Yellow "Enter 8 for DC8.domain.com"

	$DCChoose = Read-Host "Make selection now: (4, 5, 6, 7, or 8)"

	Write-Host
 

	Switch ($DCChoose)

		{

			4 {$DC = "DC4.domain.com"}

			5 {$DC = "DC5.domain.com"}

			6 {$DC = "DC6.domain.com"}

			7 {$DC = "DC7.domain.com"}

			8 {$DC = "DC8.domain.com"}
 

		}

	#Choose whether to create or update a Dynamic Distribution List

	Clear-Host

	Write-Host -fore Yellow "Do you wish to Create or Update a Dynamic Distribution Group?"

	Write-Host -fore Yellow "Enter 1 to Create a new Dynamic Distribution Group"

	Write-Host -fore Yellow "Enter 2 to Update an Existing Dynamic Distribution"

	$TaskType = Read-Host "Make Selection now: (1 or 2)"

	Write-Host

	Write-Host -fore Yellow "What is the name of the Dynamic Distribution Group (DDG)?"

	$DDG = Read-Host "Enter the DDG Name"

	Write-Host
 

	If ($TaskType -eq 1) 

		{

			$err = $null

			Write-Host -fore Yellow "Do you wish to use domain.com/Users as your RecipientContainer?"

			$rcyn = Read-Host "Make selection now: (Y/N)"

			Write-Host 

			If ($rcyn -eq "Y"){$rc = "domain.com/Users"}

			Else {$rc = Read-Host "Enter the RecipientContainer: (I.E. domain.com/Users)"}

			Write-Host -fore Yellow "What is the LAN-ID of the Owner of this DDG?"

			$Owner = Read-Host "Enter the LAN-ID of the DDG Owner"

			$mb = get-mailbox $Owner

			$Notes = "Owner = $mb"

			$RecFilter = Create-Filter

			

			

			#New-DynamicDistributionGroup -Name $DDG -domaincontroller $dc -OrganizationalUnit $ou -RecipientContainer $rc -RecipientFilter {($RecFilter)}
 

			$sInput1 = "New-DynamicDistributionGroup -Name '$DDG' -domaincontroller '$dc' -OrganizationalUnit '$ou' -RecipientContainer '$rc' -RecipientFilter {($RecFilter)}"

			$sInput2 = "Write-Host -fore Yellow 'Please stand by while we create $DDG'"

			$sInput3 = "Start-Sleep 10"

			$sInput4 = "Set-DynamicDistributionGroup '$DDG' -DomainController '$dc' -ManagedBy '$Owner' -Notes '$Notes' -RequireSenderAuthenticationEnabled:`$False"
 

			#Set-DynamicDistributionGroup $DDG -DomainController $dc -ManagedBy $Owner -Notes $Notes -RequireSenderAuthenticationEnabled:$False

			Out-File $output -InputObject "$sInput1"

			Out-File $output -InputObject "$sInput2" -Append

			Out-File $output -InputObject "$sInput3" -Append

			Out-File $output -InputObject "$sInput4" -Append

			.\CreateDDG1
 
 

			

		}

	ElseIf ($TaskType -eq 2)

		{ 

			Write-Host -fore Yellow "What would you like to modify?"

			Write-Host -fore Yellow "Enter 1 for Owner"

			Write-Host -fore Yellow "Enter 2 for RecipientFilter"

			Write-Host -fore Yellow "Enter 3 for RecipientContainer"

			$Change = Read-Host "Enter your selection (1, 2, or 3)"

			Switch ($Change)

				{

					1 {

						$err = $null

						Write-Host -fore Yellow "What is the New Owner's LAN-ID?"

						$Owner = Read-Host "Enter the LAN-ID of the DDG's new Owner"

						$mb = get-mailbox $Owner

						$Notes = "Owner =$mb"

						Write-Host -fore Green "Set-DynamicDistributionGroup '$DDG' -DomainController '$dc' -ManagedBy '$Owner' -Notes '$Notes' -RequireSenderAuthenticationEnabled:`$False"

						Set-DynamicDistributionGroup $DDG -DomainController $dc -ManagedBy $Owner -Notes $Notes -RequireSenderAuthenticationEnabled:$False

					}

					2 {

						$err = $null

						$RecFilter = Create-Filter

						$sInput = "Set-DynamicDistributionGroup '$DDG' -DomainController '$dc' -RecipientFilter {($RecFilter)}"

						Out-File $output2 -InputObject "$sInput"

						.\UpdateDDG1

						Write-Host -fore Green "Recipient filter for $DDG is updated."
 

						#Set-DynamicDistributionGroup $DDG -DomainController $dc  -RecipientFilter {($RecFilter)}

					}

					3 {

						$err = $null

						$rc = Read-Host "Enter the RecipientContainer: (I.E. domain.com/Users)"

						Write-Host -fore Green "Set-DynamicDistributionGroup $DDG -DomainController $dc -RecipientContainer $rc"

						Set-DynamicDistributionGroup $DDG -DomainController $dc -RecipientContainer $rc

					}

				}

		}

	Else { Write-Host -fore Red "Invalid Entry" }

	

#	Write-Host -fore White "Process returned error code $err"
 

	$DoAgain = Read-Host "Would you like to work with another Dynamic Distribution Group? (Y/N)"

	Write-Host

}

Open in new window

0
Comment
Question by:GusGallows
1 Comment
 
LVL 12

Accepted Solution

by:
GusGallows earned 0 total points
ID: 24330363
Nevermind, I figured it out. I have to save the whole command to a variable and then run invoke-expression against it. (iex) See corrected code below.
$sInput1 = "New-DynamicDistributionGroup -Name '$DDG' -domaincontroller '$dc' -OrganizationalUnit '$ou' -RecipientContainer '$rc' -RecipientFilter {($RecFilter)}"
 

iex $sInput1

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
The viewer will learn how to dynamically set the form action using jQuery.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now