Link to home
Start Free TrialLog in
Avatar of Jean-Jacques CELMA
Jean-Jacques CELMAFlag for France

asked on

Powershell : xx AD inactive users within an OU with Office365 licences true

Dear Experts,

I have created that powershell script

add-type -assemblyName "Microsoft.VisualBasic"
$item = [Microsoft.VisualBasic.Interaction]::InputBox("Enter number of days :", "List Inactive Users within xxx Days")
$OU = [Microsoft.VisualBasic.Interaction]::InputBox("Enter OU (DN) :", "Distinguish Name")
Search-ADAccount -UsersOnly -SearchBase "$OU"-AccountInactive -TimeSpan $item | ?{$_.DistinguishedName -notmatch ‘OU=Spain,OU=Europe,OU=Root,DC=contoso,DC=com’} | Get-ADUser -Properties Name, sAMAccountName, givenName, st, LastLogonDate, mail, userAccountControl, physicalDeliveryOfficeName  | Where-Object {($_.userAccountControl -band 2) -eq $False} | Select-Object Name, sAMAccountName, givenName, LastLogonDate, st, mail, physicalDeliveryOfficeName, @{n='OU'; e={($_.distinguishedName -split ',OU=')[1]}} | Where {$_.OU -cnotlike "*Accounts" -and $_.OU -cnotlike "*Account"}
Import-Module MSOnline
$credential = get-credential
Connect-MsolService -Credential $credential
Get-MsolUser -UserPrincipalName $_.mail | where {$_.isLicensed -eq "True"} | Select-Object UserPrincipalName, FirstName, LastName, @{n='AccountSKUid'; e={$_.Licenses.AccountSKUid}}, IsLicensed | Out-GridView

When I run it I am getting the following error

Get-MsolUser : Cannot bind argument to parameter « UserPrincipalName » because it is null.
Au caractère C:\powershell_scripts\Inactive user account.ps1:8 : 33
+ Get-MsolUser -UserPrincipalName $_.mail | where {$_.isLicensed -eq "True"} | Sel ...
+                                 ~~~~~~~
    + CategoryInfo          : InvalidData : (:) [Get-MsolUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Online.Administration.Automation.GetUs
Avatar of oBdA
oBdA

The Get-MsolUser (which is trying to access the mail property) has no connection to the results of Search-ADAccount.
Add-Type -assemblyName "Microsoft.VisualBasic"
$item = [Microsoft.VisualBasic.Interaction]::InputBox("Enter number of days :", "List Inactive Users within xxx Days")
$OU = [Microsoft.VisualBasic.Interaction]::InputBox("Enter OU (DN) :", "Distinguished Name")

$ADAccounts = Search-ADAccount -UsersOnly -SearchBase $OU -AccountInactive -TimeSpan $item |
	Where-Object {$_.DistinguishedName -notmatch 'OU=Spain,OU=Europe,OU=Root,DC=contoso,DC=com'} |
	Get-ADUser -Properties Name, sAMAccountName, givenName, st, LastLogonDate, mail, userAccountControl, physicalDeliveryOfficeName  |
	Where-Object {($_.userAccountControl -band 2) -eq $False} |
	Select-Object Name, sAMAccountName, givenName, LastLogonDate, st, mail, physicalDeliveryOfficeName, @{n='OU'; e={($_.distinguishedName -split ',OU=')[1]}} |
	Where-Object {$_.OU -cnotlike "*Accounts" -and $_.OU -cnotlike "*Account"}

Import-Module MSOnline
$Credential = Get-Credential
Connect-MsolService -Credential $Credential

$ADAccounts | ForEach-Object {
	Get-MsolUser -UserPrincipalName $_.mail |
		Where-Object {$_.isLicensed -eq "True"} |
		Select-Object UserPrincipalName, FirstName, LastName, @{n='AccountSKUid'; e={$_.Licenses.AccountSKUid}}, IsLicensed
} | Out-GridView

Open in new window

In the future, please use 'code' blocks when posting code (see the Tools bar directly above the input box).

Edit: Fixed "pretty" single quotes in line 6.
Avatar of Jean-Jacques CELMA

ASKER

Thks OBdA for the info and correction.

$ADAccounts | ForEach-Object {
	Get-MsolUser -UserPrincipalName $_.mail |
		Where-Object {$_.isLicensed -eq "True"} |
		Select-Object UserPrincipalName, FirstName, LastName, @{n='AccountSKUid'; e={$_.Licenses.AccountSKUid}}, IsLicensed , [b]@{n='Location'; e={$ADAccounts.PhysicalDeliveryOfficeName}}[/b]
} | Out-GridView

Open in new window


I have added @{n='Location'; e={$ADAccounts.PhysicalDeliveryOfficeName}}. But I got empty field in the list. It odes not display the physicalDeliveryOfficeName value of Get-ADuser.

Any clues?

Thks

JJC
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial