Get-MessageTrace : The term 'Get-MessageTrace' is not recognized as the name of a cmdlet

Prakash Besra 6 Reputation points
2021-03-12T05:06:55.617+00:00
    $ROOT_PATH = "c:\folder"
    $InputUserName = "xyz"
    $OutputFile = "reportFile"


    $cred = Get-Credential



    # Connecting to Office 365 using admin credentials

    Connect-MsolService -Credential $cred
    # Creating a remote session to Exchange Online with admin credentials and importing cmdlets
    $ExchangeOnlineSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred -Authentication "Basic" -AllowRedirection
    Write-host $ExchangeOnlineSession
    Import-PSSession $ExchangeOnlineSession  -DisableNameChecking  -AllowClobber

    Import-Module MSOnline


    # Local date and time Zone below commented line instead of UTC





    # UTC date and time Zone



    $arr = @()
    foreach($i in @(1,2,3))
    {

    $arr +=Get-MessageTrace -SenderAddress ******@contoso.com -page $i -pagesize 5000 -StartDate 03/11/2021 -EndDate 03/12/2021 | select 'RecipientAddress','Subject','Received','StartDate','EndDate','Index'

    }

    Remove-PSSession  -Session $ExchangeOnlineSession
    When I run this script on powershell_ise, I get result but when I run this script on powershell console then I am not getting any result and it gives error like

    "Get-MessageTrace : The term 'Get-MessageTrace' is not recognized as the name of a cmdlet"
    When I try to install to

    "Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force"
    then it is giving error.

    "Get-PSGalleryApiAvailability : PowerShell Gallery is currently unavailable."

I tried this also : Set-PSRepository -Name "myInternalSource" -InstallationPolicy Trusted
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

but still i am gettin this error ""Get-PSGalleryApiAvailability : PowerShell Gallery is currently unavailable."

please help me on this , i got stack in this situation.
Exchange | Exchange Server | Management
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} vote

2 answers

Sort by: Most helpful
  1. Vaibhav Chaudhari 38,921 Reputation points Volunteer Moderator
    2021-03-12T05:12:11.43+00:00

    Can you try to run below code first and later run your PS code

    Install-Module ExchangeOnlineManagement
    
    Import-Module ExchangeOnlineManagement
    

    Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav


  2. Vasil Michev 119.9K Reputation points MVP Volunteer Moderator
    2021-03-12T08:30:27.15+00:00

    You need permissions in order to be able to run the Get-MessageTrace cmdlet. To find out which permissions you need for a given cmdlet, follow the steps in this article: https://learn.microsoft.com/en-us/powershell/exchange/find-exchange-cmdlet-permissions?view=exchange-ps

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.