a-k-p
June 11, 2014, 10:07am
1
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
system
June 11, 2014, 10:20am
2
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
a-k-p
June 11, 2014, 10:29am
3
Thanks Dave, just what I was looking for…
Are these hash tables?
system
June 11, 2014, 10:31am
4
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.
a-k-p
June 11, 2014, 10:46am
5
Thank you Dave.
I will do some reading up on arrays as well.
a-k-p
June 12, 2014, 8:13am
6
I know understand spaltting!