How to connect to Exchange Online via PowerShell using a Mac

Is the first time using a Mac and I was wondering if I’m able to connect to exchange online using a computer with another operating system different as windows.

The first attempt was with Powershell Core and I downloaded the version 6.2.1 from https://github.com/PowerShell/PowerShell/releases/tag/v6.2.1 to get the package to install on my Mac.

Once I got the package I started with the installation

Powershell has been installed successfully, then its moment to try the connection to exchange online

As you can see in the image, I got an error: “New-PSSession : This parameter set requires WSMan, and no support WSMan client library was found, or PowerShell quits with an unhandled exception

Researching I figured out there is another way to install PowerShell using HomeBrew (https://brew.sh).

To install, open Terminal and paste these lines:

/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

Press Enter

Input your admin password, then xcode will be installed.

HomeBrew installation will be completed, then now let’s install PowerShell.

paste the follow line: brew cask install powershell to start the installation

Input again the admin password

And PowerShell has been installed on my Mac! Now I will try to connect to Exchange online.

Use the follow command to save your o365 credentials in a variable: $UserCredential = Get-Credential

then save the session information in another variable called $Session:
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

As you can see, the error does not appear anymore!
The next step is start the session using: Import-PSSession $Session -DisableNameChecking

We can see that PowerShell is importing all exchange online cmdlets on this computer. To confirm, let’s get all mailboxes in my tenant.

And that’s all! I hope this process helps to all Mac users.

Tony Gonzalez

Advertisement

How to use open file dialog in PowerShell

When you are working with files sometimes is easier use the open file dialog or select the files.

For example when you use the Import-Csv cmdlet to retrieve data from a csv you can use these lines:

$File = New-Object System.Windows.Forms.OpenFileDialog -Property @{

    InitialDirectory = [Environment]::GetFolderPath(‘Desktop’)

}

$null = $File.ShowDialog()

$FilePath = $File.FileName

Import-Csv $FilePath

Then work with all data in the Csv file.

The most important part of this code is the class System.Windows.Forms.OpenFileDialog that allows create the object $File

I have another post working with .csv files to see how process the records but for now I want to show you this nice feature.

Thanks – Gracias  – Dhanyavaad

How to purge emails

this only apply for exchange on-prem, can be exchange 2010, 2013 or 2016

Search-Mailbox tony@tonyexchange.com -SearchQuery ‘from:”brannon@companyx.net” subject:”December Unpaid Invoice” sent:10/11/2017’ -TargetMailbox “Exch Admin” -TargetFolder “Purged01″ -LogLevel full

The switches –LogLevel full mean that the result of the search will be copied to the folder Purged01 into the mailbox Exch Admin.

If we don’t want to copy put the switches -LogLevel full –logonly and the end of the above command.

Example:

Search-Mailbox tony@tonyexchange.com -SearchQuery ‘from:”brannon@companyx.net” subject:”December Unpaid Invoice” sent:10/11/2017’ -TargetMailbox “Exch Admin” -TargetFolder “Purged01″ -LogLevel full –logonly

If we want to delete the content just put the switch –DeleteContent.

Example:

Search-Mailbox tony@tonyexchange.com -SearchQuery ‘from:”brannon@companyx.net” subject:”December Unpaid Invoice” sent:10/11/2017’ –DeleteContent.

How to get all users in Active directory and Exchange using PowerShell

When we are using Exchange PowerShell and we want to retrieve all mailboxes we can use
Get-mailbox –resultsize unlimited otherwise you will get only 10000 mailboxes.
PS C:\> Get-Mailbox -ResultSize Unlimited
Name    Alias  Database      ProhibitSendQuota   ExternalDirectoryObjectId                
—-    —-   ——-        —————–    ———————
If you want to filter the query you can use
PS C:\> Get-Mailbox | ?{$_.Office -eq ‘US’}
The question mark stands for “where-object” and it’s a way to short the query.
For Active Directory is a quite different, if you want to retrieve all users you have to use
  Get-AdUser –Filter * -Properties *
If you want to filter the query you can use
Get-AdUser –Filter * -Properties Department | where-object {$_.Department –eq “IT”}
In the same way as before, you can use ? also
  Get-AdUser –Filter * -Properties Department | ?{$_.Department –eq “IT”}

Using $? And !$? in PowerShell

 

   This is very helpful when you need to validate the last operation if it has information (values), in other words, if the last command was successfully.
For example, I want to validate if a folder exists in my computer:
PS C:\> $Folder = Get-ChildItem -Path c:\temp
if($?)
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
The folder Temp already exists
PS C:\> 
Let’s give it a try with a folder that doesn’t exist
PS C:\> $Folder = Get-ChildItem -Path c:\temp\NewOne
if($?)
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
Get-ChildItem : Cannot find path ‘C:\temp\NewOne’ because it does not exist.
At line:1 char:11
+ $Folder = Get-ChildItem -Path c:\temp\NewOne
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\NewOne:String) [Get-Chil
   dItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChi
   ldItemCommand
The folder Temp does not exists
PS C:\> 
Yo can use !$?also, just change the order in the if-else. I mean first the validation if the result will be false, it’s easier if we see this example:
PS C:\> $Folder = Get-ChildItem -Path c:\temp
if(!$?)
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
The folder Temp already exists
PS C:\> 
Now let’s try with a folder that does not exist
PS C:\> $Folder = Get-ChildItem -Path c:\temp\NewOne
if(!$?)
{
    Write-Host “The folder Temp does not exists” -ForegroundColor Yellow
}
else
{
    Write-Host “The folder Temp already exists” -ForegroundColor Yellow
}
Get-ChildItem : Cannot find path ‘C:\temp\NewOne’ because it does not exist.
At line:1 char:11
+ $Folder = Get-ChildItem -Path c:\temp\NewOne
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\NewOne:String) [Get-Chil
   dItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChi
   ldItemCommand
The folder Temp does not exists
PS C:\>
Thanks for reading

 

How to get the folders size in windows

   Sometimes when we have some space problems, we need to know what information ewe must delete. The first step is get the folders with more space in the disk.
   There is not a fast way to know that, worst-case scenario you have to go through all folders one by one and right click to see the size used on disk.
   Researching on internet I found some PowerShell scripts to get folders details like name and size, but taking information from different sources I have created a script and even when I don’t get any error it takes more than an hour to finish, this is not a good idea for desperate people like me…. ¯\_()_/¯
This is the script… Not sure if it works because after an hour running  without any error, it never finishes.
But as part of the research I found a very interesting site with a bunch of Microsoft tools, one of them is “Disk Usage”  a very fast and reliable tool
After download and unzip the tool, this is the result:
You can copy and paste the information in a spreadsheet and you will be able to see the information as follow:
Adding a column to convert the Size on Disc to MB

How to see Windows time settings

   The command W32tm /query /configuration works in all windows versions and its helpful when a server takes another timeserver and can be different from other servers or computers running an application or service and there is a time discrepancy.
   There are more tools and different parameters to use with that command but sometimes with /query /configuration are enough.  

for more details you can visit https://docs.microsoft.com/en-us/windows-server/networking/windows-time-service/windows-time-service-tools-and-settings

How to connect powershell to Skype for business online

To use all cmdlets for Skype, you must install the module, first download from Microsoft:

Click on the .exe file to install
Basically, is Next – Next – Finish
Once the module is installed, open your powershell as administrator.
To Create the session type these lines:
#only once
#Set-ExecutionPolicy RemoteSigned
$Cred = Get-Credential
$Session = New-CsOnlineSession -Credential$Cred
Import-PSSession $Session
NOTE: only the first time delete # in order to uncomment the second line to allow execute scripts. After that, you can comment this line again, is not needed the next time you execute this script to create the session.
Input the credentials to open the session
Once the session is established, the prompt will appear
And, you can type get-CsOnlineUser UserName and you will see all the user properties.

And that’s it. You can start using all cmdlets for Skype online.

Process to purge emails in office 365

   When an email with malicious content like virus, phising, spam, etc is coming to the company we need to delete those emails from the recipients Inboxes, sometimes in the companies, an employee send an email with incorrect or offensive content, this process applies as well.
Please follow this process:  

1. create the content. (based on https://docs.microsoft.com/en-us/office365/securitycompliance/content-search)             



Click on Content search
Create a new Search
Here we can put the conditions like sender, date range, subject, attachment, etc.
Click on save and run, its important input a name for this search.
In the open option, we can see all the searches saved
Once the search has finished, go to powershell and use the cmdlet New-ComplianceSearchAction
NOTE: to use the compliance module execute this script
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationNameMicrosoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/-Credential $UserCredential-Authentication Basic-AllowRedirection
Import-PSSession $Session -DisableNameChecking
You can validate the report and this task has been completed.

Problems to install Skype for business online powershell module in windows 10

When I was trying to install the Skype for business online I was getting different errors, the first was
“must have C++ Redistributable (x64) – 14.10.25008 installed as a Minimum Runtime.”
while I was trying to install the module.

If I tried to import the module appears the follow error:
“import-module : the specified module ‘skypeonlineconnector’ was not loaded because no valid module file was found in any module directory.” 
The solution is download the latest supported C++, you can find it here:
After install, reboot the computer and you will be able to install the skype for business module without any problems.

To connect to Skype online, type the follow lines

Import-Module SkypeOnlineConnector
$userCredential = Get-Credential
$sfbSession = New-CsOnlineSession -Credential $userCredential
Import-PSSession $sfbSession

and Listo!

to make sure that you have the module ready, you can type Get-CsOnlineUser Username, for example:

and you will see the properties for that user.

I hope it helps.