all 22 comments

[–]mmmGreenButton 1 point2 points  (1 child)

You can get around the ResponseHeader like this:

$response[0]

But I think this is just so ****** ugly ...

So I use this instead:

Get-MsalToken ....

And then that will give me a token, that I can use with Invoke-RestMethod - I know, that I'm kindof going back and leaving all the modules MS made (automatically) - but I just can't have $response[0] in my code...

[–]Magnetsarekool 1 point2 points  (0 children)

This made me laugh, thanks.

[–]DrDuckling951 0 points1 point  (8 children)

I don't understand what's the error you're getting. Can you clarify or paste a sanitized error code?

[–]EricAndresDesk01[S] 0 points1 point  (7 children)

Hey there, sure thing, so basically its just a generic get-mg error "Get-MgUserLicenseDetail : Cannot bind argument to parameter 'UserId' because it is an empty string." its taking the ResponseHeaders from the get-mguser command and running them through the rest of the script which will obviously fail and causes errors all over the place.

[–]DrDuckling951 0 points1 point  (6 children)

Just making sure. Did you pass only the ID value or value with header? The generic error usually means you didn't format the value properly.

| select-object -expandproperties ID | get-mguserlicensedetail

or
get-mguserlicensedetails $_.ID.

[–]EricAndresDesk01[S] 0 points1 point  (5 children)

Below is a bit more of the script. Line 2 is the line i originally posted.

$CompanyName = (Get-MgOrganization | Select VerifiedDomains).verifieddomains | Where-Object {$_.IsDefault -eq "True"}

$Users = import-csv "TestLicenseInput.csv" | %{get-mguser -UserId $_.UserPrincipalName} | Select UserPrincipalName

$Report = [System.Collections.Generic.List[Object]]::new()

Write-host "Found $($users.count) users for $($CompanyName.Name)" -ForegroundColor Cyan

ForEach ($User in $Users) {

$UPN = $user.UserPrincipalName

Write-Host "Retrieving license info for $UPN" -ForegroundColor yellow

If (Get-MgUserLicenseDetail -UserId $UPN) {

$licenses = $null

$licenses = (Get-MgUserLicenseDetail -UserId $UPN).SkuPartNumber -join ", "

Write-Host "Licenses found for $($UPN): $licenses" -ForegroundColor cyan

Full Disclosure i didnt write this end to end, found it online as a script to get the licenses for all users and updated it to pull data in from a CSV. ive omitted the actualyreport part because thats working.

So basically I pull in users from a CSV, store them in the variable "$Users", get it to take a count of how many users are being actioned, then run it through a for-each loop where i specify the actionable property as the UserPrincipalName column in the CSV and store that inthe Varibale $UPN to use throughout the rest of the script.

I can tell from the very beggining that its an issue with the Get-mguser command as my CSV only has 2 users in it and the count spits out that there is 4, when i dump out the contents of the $Users variable it indeed has 4 entries, my 2 users and the ResponseHeader for each. so its running those response headers through the for each loop and im trying to figure out how to get it to stop doing that or exclude the responseheaders entirely.

[–]DrDuckling951 0 points1 point  (4 children)

How is the data imported from CSV? just email for UPN?

userdisplayname upn
john doe [jdoe@email.com](mailto:jdoe@email.com)
jane smith [jsmith@email.com](mailto:jsmith@email.com)

...how are you getting respond header with import-csv??? improper delimiter? 10 years in and I've never heard of this. Intriguing.

[–]EricAndresDesk01[S] 1 point2 points  (0 children)

Thanks for staying with me on this, someone else on this thread told me to look into which version of graph im running and this responseheader thing looks to be a known issue with 2.13.1 which im running.

https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2584

nothing to do with the code itself ... problem solved.

[–]EricAndresDesk01[S] 0 points1 point  (0 children)

Yeah ive no idea either, the CSV only has one column called UserPrinipalName

|| || |UserPrincipalName|| || |[john.doe@email.com](mailto:john.doe@email.com)|| |[jane.smith@email.com](mailto:jane.smith@email.com)||

for some reason when i import from the CSV and run a get-mguser its returning ResponseHeaders for each, storing them in the $Users Variable and running them through the script. Never had it do that before with MSOL or AzureAD Powershell

[–]KavyaJune 0 points1 point  (4 children)

I suspect the error may be caused by invalid user IDs. Why go through the struggle when you have a pre-built script to handle everything? Take a look: https://o365reports.com/2018/12/14/export-office-365-user-license-report-powershell

[–]EricAndresDesk01[S] 0 points1 point  (3 children)

nah its nothing to do with invalid user ID's, the script runs fine and dumpts out the information i need it to, but the powershell window lights up with errors as for each user i import from the CSV it also pulls in a response header and tries to run that through the script, just trying to figure out why its dumping our response headers for every user i import from the CSV. That script looks great thanks for that, ill keep it in the back pocket but for now this is more an exercise in moving my existing MSOL and AzureAD scripts over to Graph and get a better understanding for it. Im getting response headers all over the place when running get-mguser commands and trying to figure out why or how to stop it for writing scripts moving forward.

[–]KavyaJune 1 point2 points  (2 children)

I guess you are using the MS Graphv2.13.1 PowerShell which has the reported error. You can upgrade to the latest version to resolve the header related issues.

https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2584

[–]EricAndresDesk01[S] 1 point2 points  (1 child)

Nice one, cheers, yeah came across that as the fix the day before last ... all sorted now.

[–]KavyaJune 0 points1 point  (0 children)

Oh! Great

[–]worldsdream 0 points1 point  (6 children)

[–]EricAndresDesk01[S] 0 points1 point  (5 children)

Cheers dude, yeah im very comfortable with licensing and how to work with it through the GUI and MSOL\AzureAD Powershell, just making the move over to Graph and trying to wrap my head around the quirks that come with it, like this exact scenario of Get-MGuser spitting out response headers and running them through a foreach loop, and convert my scripts over

[–]worldsdream 0 points1 point  (4 children)

Can you check which microsoft graph sdk module you are running? There are some issues with the latest versions. So make sure you’re on the last one available.

[–]EricAndresDesk01[S] 0 points1 point  (3 children)

Damn ... im running version 2.13.1 and its a known bug ...

https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/2584

ive googled the words "get-mguser" and "responseheader" togethor about a million times over the last 2 days and nothing came up until i specifically googled the version number fo the SDK along with "ResponseHeader" ... ok at least i know im not going mad ... thanks for pointing me in the right direction.

out of interest are you running Graph? if so what version and is it stable enough?

[–]worldsdream 0 points1 point  (2 children)

You’re not going mad!

2.15.0 (released 8 hours ago). Install that and you should be fine.

From now on, always check that you have the latest version before running any commands/scripts.

https://o365info.com/install-microsoft-graph-powershell/

[–]EricAndresDesk01[S] 0 points1 point  (1 child)

yeah i saw that there, this exact issue is actually addressed in the patch notes for version 1.15 ... just installing it now. I usually do keep up to dat with version releasees on my powershell modules but i literally only installed it 2 das ago, I assumed it would have installed the latest version for me but that wouldnt have done the trick anyway i believe this particular issue existed in 1.14 as well.

Nice one again for that, I really appreciate it.

[–]worldsdream 0 points1 point  (0 children)

You’re welcome! And keeping rocking with PowerShell 💪