Just trying to get a list of users assigned a specific MS licence using PowerShell - powershell

I am a bit of a novice where PowerShell is concerned. I do lots of things in ti, but generally have to look them up.
I am trying to get a list of users who have been assigned a specific licence in Office 365. I am using a command which I have used before successfully, but this time it will not work and keeps giving me the following error;
Get-MsolUser : The request channel timed out while waiting for a
reply after 00:00:59.9152154. Increase the timeout value passed to the
call to Request or increase the SendTimeout value on the Binding. The
time allotted to this operation may have been a portion of a longer
timeout. At line:1 char:1
+ Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "excha ...
+ ~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Get-MsolUser], TimeoutException
+ FullyQualifiedErrorId : System.TimeoutException,Microsoft.Online.Administration.Automation.GetUser
The command I am using is;
Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match "exchangestandard"}
Is there a way if increasing the timeout? Or is there another way of getting this information, other than doing it manually. The company only has about 500 users and this particular licence is only being used by 42.
Thank you.

Related

Powershell: Script that was working now errors giving Get-ADUser : The server was unable to process the request due to an internal error

I had a script that was working fine. Went away for a few hours and came back and now it errors.
Error
Get-ADUser : The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults
(either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or
turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.
At C:\Users\orion\Desktop\GetUsersProcessFile.ps1:1 char:1
+ Get-ADUser -Filter * -Properties CN,Department,Description,DisplayNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.GetADUser
The script is as follows
Get-ADUser -Filter * -Properties CN,Department,Description,DisplayName,Division,EmailAddress,extensionAttribute10,GivenName,mail,mailNickname,Name,Office,SamAccountName,sn,Title,UserPrincipalName | export-csv -path e:\ad\user-export.csv
I am looking to export every user I can with the details I want to a CSV file.
As I said, I got it working and it was working but I now get this error. This is sitting on a Windows 2019 Server.
Any ideas why it would suddenly stop working?
I did some digging and found a couple posts that may provide some insight:
First up: https://social.technet.microsoft.com/Forums/lync/en-US/a7ca2b45-bcf7-474b-a3d4-9ccc7b6f792b/internal-error-for-non-domain-admins?forum=winserverpowershell
In this case, someone removed Domain Users from the builtin Users group. Since ADWS restricts access to the Users group, queries were failing.
Second: https://social.technet.microsoft.com/Forums/en-US/f8eb3d11-6a79-4c0b-a59a-8c90b65557cf/active-directory-powershell-quotinternal-errorquot?forum=winserverpowershell
In this case, the problem was fixed by restarting the ADWS service on the domain controllers.
Note that the cmdlets like Get-ADUser and Set-ADUser depends on ADWS running on the DCs, whereas native ADSI methods do not. However, using native ADSI requires a bit more care, e.g. using paging for querying the memberOf property. There's a lot of good documentation out there on using native ADSI objects from PowerShell.

InvalidCastException when trying to obtain UserPrincipal.Current

I have a PowerShell script which checks the currently signed in user as part of its start-up process. I'm using .Net to do this by adding the assembly:
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$cUser = [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
For almost everyone this works fine and I get a UserPrincipal object that I can use elsewhere, however there are a couple of users who get the following error when running it:
Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. At line:2 char:1
+ [System.DirectoryServices.AccountManagement.UserPrincipal]::Current
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidCastException
+ FullyQualifiedErrorId : System.InvalidCastException
This is on a standard Windows 10 20H2 install and doesn't matter if PowerShell is running elevated or not. I've never seen this call return anything other than a UserPrincipal before, so I would be grateful for any advice: is this something I can deal with in code, or is there some underlying issue with the machines that are returning this exception?
I don't know if this is what you're looking for:
$cUser = Get-ADUser $Env:Username -Properties *
This has the most important attributes that you can use for whatever you want later on. It provides as much attribute as the code you posted that didn't work for some of the user.

powershell ver 2.0 search AD failure [duplicate]

We have mixed desktop operating systems consisting of windows 7 and windows 10. I have a login script that gathers various information from a powershell script that runs each time a user logs in. The windows 7 powershell is only version 2 which means I cannot use get-aduser, I am therefore challenged to query this information out of AD using a different method that would be compatible with both win7 and win10. I have this line of code that does not use get-aduser and successfully produces a list of all AD users on powershell 5(win10), however when I attempt to use it on powershell 2 it produces the error below.
My questions are these:
What do I need to change to get the script working on powershell version2 ?
How can I get it to output the current user as opposed to all the users in AD
thank you for any help in advance
<position> : The following exception was thrown when trying to enumerate the collection: "Configuration system failed t
o initialize".
At line:1 char:1
+ <<<< (New-Object DirectoryServices.DirectorySearcher "ObjectClass=user").FindAll() | Select-object -property path
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator
(New-Object DirectoryServices.DirectorySearcher “ObjectClass=user”).FindAll() | Select-object -property path

query AD using powershell version 2

We have mixed desktop operating systems consisting of windows 7 and windows 10. I have a login script that gathers various information from a powershell script that runs each time a user logs in. The windows 7 powershell is only version 2 which means I cannot use get-aduser, I am therefore challenged to query this information out of AD using a different method that would be compatible with both win7 and win10. I have this line of code that does not use get-aduser and successfully produces a list of all AD users on powershell 5(win10), however when I attempt to use it on powershell 2 it produces the error below.
My questions are these:
What do I need to change to get the script working on powershell version2 ?
How can I get it to output the current user as opposed to all the users in AD
thank you for any help in advance
<position> : The following exception was thrown when trying to enumerate the collection: "Configuration system failed t
o initialize".
At line:1 char:1
+ <<<< (New-Object DirectoryServices.DirectorySearcher "ObjectClass=user").FindAll() | Select-object -property path
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : ExceptionInGetEnumerator
(New-Object DirectoryServices.DirectorySearcher “ObjectClass=user”).FindAll() | Select-object -property path

Win32Shutdown Generic Failure

I am putting together a script which will log off VDI sessions which have been in a disconnected state for over 10 hours. I have managed to get everything together except for the last hurdle - actually forcing a logoff.
ForEach ($Desktop in $VDIlist)
{
$win32OS = Get-wmiobject win32_operatingsystem -ComputerName $desktop.'DNS Name' -EnableAllPrivileges
write-host "Shutting down host $Desktop."DNS Name""
$win32OS.Win32Shutdown(4)
}
This results in the error below.
Exception calling "Win32Shutdown" : "Generic failure "
At line:1 char:1
+ $win32OS.win32shutdown(4)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WMIMethodException
This does not appear to happen when no argument is used
($win32os.win32shutdown()), but this also does not force the log off like I require.
As far as I have read the -EnableAllPrivileges parameter should allow for the remote log off and it does work if I have a live PCoIP session to the VDI I am attempting to shutdown but not when in a disconnected state.
Could anyone point me in the right direction?
Still not entirely sure why the first script is giving an error but I have instead switched to using VMWare View's built in PowerCLI snapin to produce the same result - just faster and more efficiently.
get-remotesession -state "Disconnected" | Where-Object {($_.duration -match 'Day' -or $_.duration -match '\d\d hours')} | Send-SessionLogoff
This will query the Horizon view server for any sessions with the "Disconnected state", it will then filter out any objects that have had a lifetime of less than 10 hours and log off anything that is left.
This requires VMware View PowerCLI PSSnippets to be loaded and connected to your view connection broken.