Command line too long..

This topic contains 5 replies, has 2 voices, and was last updated by  A K P 3 years, 3 months ago.

  • Author
    Posts
  • #16151

    A K P
    Participant

    Hi all,

    Just wondering what technique I could use to shorten my command line other than using variables for each parameter..

    e.g.
    Set-QADUser -Identity $_."Name" -UserPrincipalName $_."UserPrincipalName" -alias $_."alias" -Displayname $_."Displayname" -FirstName $_."FirstName" -LastName $_."LastName" -OrganizationalUnit $_."OrganizationalUnit" -PrimarySmtpAddress $_."PrimarySmtpAddress" -SamAccountName $_."SamAccountName" -Password (ConvertTo-SecureString $_.password -AsPlainText -Force)

    Any ideas appreciated!

    Cheers,
    AKP

  • #16152

    Dave Wyatt
    Moderator

    I prefer to use splatting to keep that sort of long command readable:

    $params = @{
        Identity           = $_.Name
        UserPrincipalName  = $_.UserPrincipalName
        Alias              = $_.Alias
        DisplayName        = $_.DisplayName
        FirstName          = $_.FirstName
        LastName           = $_.LastName
        OrganizationalUnit = $_.OrganizationalUnit
        PrimarySmtpAddress = $_.PrimarySmtpAddress
        SamAccountName     = $_.SamAccountName
        Password           = ConvertTo-SecureString $_.Password -AsPlainText -Force
    }
    
    Set-QADUser @params
    
  • #16153

    A K P
    Participant

    Thanks Dave, just what I was looking for...

    Are these hash tables?

  • #16154

    Dave Wyatt
    Moderator

    Yep, $params is a hash table. When you splat a hashtable, its keys become the names of command Parameters, and its values are the arguments passed to those parameters.

    You can also do splatting with an array, in which case each element of the array is passed as a positional parameter, in the same order as the array.

  • #16155

    A K P
    Participant

    Thank you Dave.
    I will do some reading up on arrays as well.

  • #16178

    A K P
    Participant

    I know understand spaltting!

You must be logged in to reply to this topic.