SOLVED

Function within a pipline - Returns wrong value

Copper Contributor

Hi Everyone ,

 

I'm trying to create a list of user, and witch Office 365 license they have based on membership in an AD group. I have created a simple function for that myself, and it appears to be working when I test in on a specific user.

I have put the function within a hash table, and the function appears to be running, but the result is wrong (as it is "No License" for all users).

 

I'm not even sure it's possible to do this this way? But I feel that I'm quite close to something.

Any tips to how I can solve this?

function Get-LicenseType {

[CmdletBinding()]
param(
[parameter(mandatory=$true)]
[string]$name
)

$E3Members = Get-ADGroupMember -Identity License-Microsoft-Office365-E3 -Recursive | Select -ExpandProperty Name
$F1Members = Get-ADGroupMember -Identity License-Microsoft-Office365-F1 -Recursive | Select -ExpandProperty Name

if ($E3Members -contains $name) {
Write-Output "E3"
} #End foreach E3

elseif ($F1Members -contains $name) {
Write-Output "F1"
} #End foreach F1

else {
Write-Output "No licence"
}
} #End function Get-LicenceType


$users = Get-ADUser -Filter {SamAccountName -like "*user*"} -Properties "*"
$users | Select-Object name, mail, SamAccountName, @{n="License";e={Get-LicenseType name}} | Export-Csv C:\PS-Output\$((Get-Date).ToString('yyyyddMM'))-Users.csv -Append -Encoding UTF8 –NoTypeInformation

 

Thanks!

 

// Julian

2 Replies
best response confirmed by VI_Migration (Silver Contributor)
Solution

Well for starters, your list of users has a strict filter that will only return the ones with "user" in their samaccountname, are you sure you have enough matches and are those users actually members of the two groups? Next, your function doesn't return any value, instead it writes output. Change it to use the return statement. Well technically both should work, but I prefer "return" 🙂

 

Lastly, when using calculated properties, you need to use the $_.Name format instead of just "name". So this:

 

$users | Select-Object name, mail, SamAccountName, @{n="License";e={Get-LicenseType $_.name}} | Export-Csv C:\PS-Output\$((Get-Date).ToString('yyyyddMM'))-Users.csv -Append -Encoding UTF8 –NoTypeInformation

 

Thank you Vasil!

 

It actually worked when I changed to $_.name 🙂
But I like to make my small function better, so I will add "return" to my script  aswell.

 

Sorry to put you of by my filter, I just added "user" as an example.

 

I now have the list I need! Thanks again! 🙂

 

// Julian

1 best response

Accepted Solutions
best response confirmed by VI_Migration (Silver Contributor)
Solution

Well for starters, your list of users has a strict filter that will only return the ones with "user" in their samaccountname, are you sure you have enough matches and are those users actually members of the two groups? Next, your function doesn't return any value, instead it writes output. Change it to use the return statement. Well technically both should work, but I prefer "return" 🙂

 

Lastly, when using calculated properties, you need to use the $_.Name format instead of just "name". So this:

 

$users | Select-Object name, mail, SamAccountName, @{n="License";e={Get-LicenseType $_.name}} | Export-Csv C:\PS-Output\$((Get-Date).ToString('yyyyddMM'))-Users.csv -Append -Encoding UTF8 –NoTypeInformation

 

View solution in original post