Author Posts

June 16, 2015 at 12:37 am

How can i break up this line in a script to make it look neater ? I don't want to use backtick's if can...

Add-CMUserCollectionQueryMembershipRule -CollectionName "Name" -QueryExpression "select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.SecurityGroupName = 'groupname' " -RuleName "rulename" -Verbose

June 16, 2015 at 12:46 am

Would breaking up the QueryExpression like this work ok ?

$QE = ("select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User 
where SMS_R_User.SecurityGroupName")

June 16, 2015 at 12:56 am

If you don't want to use backtick, you could consider using splatting technique. Splatting is a method of passing a collection of parameter values to a command as unit. Windows PowerShell associates each value in the collection with a command.

So you can modify your code as –


$params = @{'Collection name' = 'Name'
            'QueryExpression' = 'select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.SecurityGroupName = "groupname"'
            'RuleName' = 'rulename'
           }

Add-CMUserCollectionQueryMembershipRule $params -verbose

June 16, 2015 at 1:07 am

@ is for splatting.

Add-CMUserCollectionQueryMembershipRule @params -verbose

June 16, 2015 at 2:15 am

Thanks again everyone.

June 16, 2015 at 5:57 am

Also look at here strings:

$groupname = "foo"

$hereString =@" 
    select SMS_R_USER.ResourceID,
           SMS_R_USER.ResourceType,
           SMS_R_USER.Name,
           SMS_R_USER.UniqueUserName,
           SMS_R_USER.WindowsNTDomain 
    From SMS_R_User 
    where SMS_R_User.SecurityGroupName = '$groupname'
"@

June 16, 2015 at 6:28 am

Thats nice Rob, like that. What is it called when you use @= ? Not an array is it ?

June 16, 2015 at 7:35 am

Graham,

What about this:

$groupName = 'foo'

$query = @"
'select SMS_R_USER.ResourceID,
            SMS_R_USER.ResourceType,
            SMS_R_USER.Name,SMS_R_USER.UniqueUserName,
            SMS_R_USER.WindowsNTDomain 
            from SMS_R_User where SMS_R_User.SecurityGroupName = "$groupName"'
"@

$params = @{
    'Collection name'	= 'Name'
    'QueryExpression'	= $query
    'RuleName'			= 'rulename'
}

Add-CMUserCollectionQueryMembershipRule @params -Verbose

I'm using splatting and a here-string construct, like GJ and Rob suggested.

June 16, 2015 at 9:27 am

Richard thats brilliant. Looks really nice and clean. Like that. Thanks again. (p.s ordered my "Learn Powershell toolmaking in a month of lunches Sunday, Richard. Looking to reading it 🙂 )