Breaking long line of code in a script

This topic contains 8 replies, has 5 voices, and was last updated by Profile photo of Graham Beer Graham Beer 1 year, 7 months ago.

  • Author
    Posts
  • #26482
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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
    
  • #26483
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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")
    
  • #26484
    Profile photo of GJ
    GJ
    Participant

    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
    
    
  • #26485
    Profile photo of Martin Nielsen
    Martin Nielsen
    Participant

    @ is for splatting.

    Add-CMUserCollectionQueryMembershipRule @params -verbose
  • #26486
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Thanks again everyone.

  • #26500
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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'
    "@
    
  • #26501
    Profile photo of Graham Beer
    Graham Beer
    Participant

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

  • #26508
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    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.

  • #26515
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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 🙂 )

You must be logged in to reply to this topic.