Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting a string to a set of arguments - Powershell

Posted on 2009-05-07
1
Medium Priority
?
784 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Steps to fix “Unable to mount database. (hr=0x80004005, ec=1108)”.
If you have come across a situation where you need to find some EDB mailbox recovery techniques, then here you will find the same. In this article, we will take you through three techniques using which you will be able to perform EDB recovery. You …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

971 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