Parameters

Hello

Im trying to get this to work, the goal for me is to clean old code using linebreaks. How can I code the properties declaration to make it look like bellow?

#Propertes declaration
$properties = {
objectguid
Name
distinguishedName
samaccountName
mail
telephonenumber
mobile
title
l
department
buildingName
ExtensionAttribute10
roomNumber
manager
}

#parameters declare
$Parameters = @{
Filter = “*”
Properties = “$properties” #instead of “‘name’,‘roomNumber’”
SearchBase = “OU=Users,DC=company,DC=com”

}

getusers

Get-ADUser @Parameters

Thx in advance!

You can have line breaks after each comma, semikolon, opening parenthesis, curly braces or - if it’s really necessary - backticks. Another technique to make code better readable is splatting.

Properties are an array:

{} = Scriptblock
@{} = Hash Table
@() = Array

So, you can do properties like so:

$properties = @(
    'objectguid',
    'Name',
    'distinguishedName',
    'samaccountName',
    'mail',
    'telephonenumber',
    'mobile',
    'title',
    'l',
    'department',
    'buildingName',
    'ExtensionAttribute10',
    'roomNumber',
    'manager'
)

$Parameters = @{
    Filter = "*"
    Properties = $properties
    SearchBase = "OU=Users,DC=company,DC=com"
}

Get-ADUser @Parameters

Also, just another tip, don’t put unnecessary comments in your code.

# properties declaration
$properties = ....

The variable is named “$properties” is sufficient with out the clutter of comments. Use comments to tell someone the purpose of something that isn’t obvious. For instance, Invoke-RestMethod uses TLS 1.0 by default and it’s being deprecated everywhere, so I put comments around [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 because you would have to look that up (most likely) to understand why that is there.

# Force TLS 1.2 protocol. Invoke-RestMethod uses 1.0 by default
Write-Verbose -Message ('{0} - Forcing TLS 1.2 protocol for invoking REST method.' -f $MyInvocation.MyCommand.Name)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
                
$results = Invoke-RestMethod @restParams

Thanks for reply and thanks for the tip I will follow your advice when asking questions here. (Im still a newbie, but have learnt a lot recent days. The code bellow is what I executed when i got the error. Any ideas why I get the error?

$properties = @(
‘objectguid’,
‘Name’,
‘distinguishedName’,
‘samaccountName’,
‘mail’,
‘telephonenumber’,
‘mobile’,
‘title’,
‘l’,
‘department’,
‘buildingName’,
‘ExtensionAttribute10’,
‘roomNumber’
‘manager’
)

$parameters = @{
Filter = “*”
Properties = “$properties”
Searchbase = “OU=Users,DC=company,DC=com”
Erroraction = “Stop”
Searchscope = “subtree”
}

Get-ADUser @parameters

Get-ADUser : One or more properties are invalid.
Parameter name: objectguid Name distinguishedName samaccountName mail telephonenumber mobile title l department buildingName ExtensionAttribute10 roomNumber manager´

Remove the qoutes around Properties, you are converting the array to a string:

$parameters = @{
Filter = "*"
Properties = $properties
Searchbase = "OU=Users,DC=company,DC=com"
Erroraction = "Stop"
Searchscope = "subtree"
}

[Edit] Code updated above. I missed that you had qoutes there in the original post.

Thank you very much, now it worked like a charm and I learnt a lot from yuor explainations.
Cheers