How to check mailbox size without exchange module? - powershell

I need to find a way to check mailbox size and items count under PowerShell, but without using exchange snapin/module.
Normally this command would look like this:
Get-MailboxStatistics "XXX" | ft TotalItemSize, ItemCount
My goal is to checked those things without exchange specific commands. I don't have much knowledge about ADSI, but maybe this is the key to solve this.
Does anyone have any idea?

Related

Get-Recipient to Get-EXORecipient? Powershell and Exchange

Had a quick and hopefully easy question for you guys. I've tried googling and searching but I haven't been able to find much info.
We've been asked to try and streamline and improve some scripts we have. I was trying to convert some Get-Recipient commands to the newer and quicker Get-EXORecipient module.
I'm having some issues with the below though and would appreciate any help or advice:
As part of one of my scripts, I use the below command to find a specific DDL. This works fine
$var1 = Get-DynamicDistributionGroup -Identity "DDLName"
The issue I have is trying to get details from this DDL using the new module
Extract of old code which works but is quite time consuming depending on the size of the DDL
$var2 = Get-Recipient -RecipientPreviewFilter $var1.RecipientFilter
The new code I'm trying which spits out an error
$var2 = Get-ExoRecipient -RecipientPreviewFilter $var1.RecipientFilter
The error I get from the above is "Get-ExoRecipient : RecipientPreviewFilter is not a supported parameter"
I haven't been able to find out how to apply the DDL filter to this command, I tried putting the entire filter in as a string but that didn't work either. That wasn't an ideal solution as the filter may change on the exchange side.
Would appreciate any help on this one!
Thanks
According to the Get-EXORecipient documentation, the -RecipientPreviewFilter parameter has been reserved for 'internal Microsoft use'.
You should be able to use the -Filter parameter instead though like this:
Get-ExoRecipient -Filter "Title -eq 'Teaching Staff'"
However, the format of the filter returned by the dynamic distribution group may differ slightly and require some changes before it can be used with -Filter

Learning Powershell scripting

I am a young I.T Apprentice who is responsible (in part)for administering Active Directory tasks.
I have looked to learn powershell to help with this.
Anyway, I'm looking to incorporate reading data from files into my daily tasks to simplify a process, I'm looking to pull data and configure Changes for multiple AD accounts. My idea is to have a file with the samaccountname listed in a single column (with no header). Import the csv file to get the ad username and disable the users then place a new description in the accounts description field.
Right now I have a csv with usernames listed and a script that imports the csv and this is where I'm stuck.
I can execute the disable-aduser and set-aduser -description "sample text", functions I need for the script separately , successfully in testing in a one liner situation by calling on the get-aduser and piping the result to each command, but I'm looking to place this in a script and grab the ad usernames from a csv for multiple accounts.
I'm having trouble setting the object variable from the csv (i hope that's the right terminology), I have been unable to 'get this' for lack of a better term. I'm hoping to place this into a for loop to include the functions I have described.
Can anyone help me or describe how I can set the variables to encapsulate each ad user account in my file to help me continue on with my script and configure the changes above?
I know this may seem like a strange or overly simple question to ask I.T pros but I can assure you I have done further reading but I havent been able to find a resolution to this specific problem.
My apologies if the terminology in my question is not spot on.
Thank you in advance, Glenn.
here's one way to loop thru a collection of items imported from a CSV file ...
# fake reading in a headerless, one-column text file as a CSV
# in real life, use Import-CSV
$UserNameList = #'
OneTest
TwoTest
ThreeTest
FourTest
'# | ConvertFrom-Csv -Header UserName
foreach ($UNL_Item in $UserNameList)
{
# do the things you need done to each user id here
'Acting on user [ {0} ] ...' -f $UNL_Item.UserName
}
hope that helps,
lee
The below will load the entire CSV and show it in an Out-GridView. You can then CTRL click multiple users and process them that way.
I enjoy doing it this way in the event that another name has snuck into my CSV when it should not have,
Import-CSV 'C:\Location\of\CSV.csv' | Out-GridView -PassThru | ForEach-Object{Disable-ADAccount $_.SamAccountName; Set-ADUser -Identity $_.SamAccountName -Description "Test Description"}
NOTE: Thanks Robert for advice of skipping putting the Import-csv into a variable to handle and going straight to the source.

how to get email body for exchange server on prem using powershell cmdlet

I am looking for cmdlet which would allow me to search and fetch email body/to/from information. Currently I am using Search-Mailbox but I am getting only the resultItemCount. Any idea how to get actual body via cmdlet?
If you use exchange online powershell.
You can identify emails for the past 48 hours ( up to ten days if you specify start and end date).
$messages = Get-MessageTrace -SenderAddress anEmail#YourExchange.com -StartDate 03/21/2021 -Enddate 03/31/2021
Get-message -messageId $messages[1].messageid
Will return the message from the first one on the list that is returned.
For more info, check out this documentation from microsoftdocs.
https://learn.microsoft.com/en-us/powershell/module/exchange/get-message?view=exchange-ps
Are you an exchange admin?? If you are, or have access to exchange admin credentials, I would check out this tool (which is used by hackers to search email) called MailSniper https://github.com/dafthack/MailSniper It is technically for pentesting... but if you wanted to search your emails for body content this would work.
It's a powershell module, pretty easy to use, just download it, go to that directory in powershell, import the module, and start running the commands. For what you are looking for, some commands to look at are:
Invoke-SelfSearch (if you're looking for your own mailbox)
Invoke-GlobalMailSearch (if you're looking through others' mailboxes)

How to output specific information from a powershell command?

So I've been playing around with Powershell recently, trying some things with a basic command net user $username. As an example net user administrator produces an output that you can see at the bottom of this page.
My question is: How do I output specific elements of this?
I'm aware of pipes and have been trying to use them but I think I'm missing something as it never comes out right. Lets say, for example, I just want user name, full name, password expires and last logon to be shown as an output. What command do I use after the pipe to get this?
Many thanks!
net.exe is not a PowerShell cmdlet. Therefore getting information out it is processing the output of the executable as a string.
For example, retrieving the user name:
$netoutput = net user administrator
#User name is on the first line, separated by spaces, the actual username is last word
$netoutput[1].Split(" ")[-1]
If you are using Win10 1607 or newer, you could retrieve this information with the Get-LocalUser cmdlet
Get-LocalUser administrator | Select-Object Name,FullName,PasswordExpires,LastLogon

How can I filter mailboxes that have never been logged on in Exchange Management Shell?

I need to run a Get-Mailbox | Get-MailboxStatistics command across a large number of mailboxes but the majority have never been used as it is a new install. As a result, I have to sit through hundreds of lines of
WARNING: There is no data to return for the specified mailbox '<mailbox DN>' because it has not been logged on to.
It would seem that I need to use a server-side filter of some kind but I haven't been able to find anything appropriate.
What can I do here?
There is no server side filtering in Get-MailboxStatistics and I can't repro it. Can you try this:
Get-Mailbox | Get-MailboxStatistics -warningAction silentlyContinue
This is the standard PS behavior for warnings. You can find Shay's parameter in the help for common_parameters get-help about_common_parameters. Alternately, you can set $WarningPreference = silentlycontinue. There are no statistics to return as the mailboxes have not yet been initialized, hence the warning.