Running nested .ps1 sctipts from a ps1 script RRS feed

  • Question

  • Good day all.  I would like to log onto exchange online, run two scripts I have already created then close the session to exchange online.  One of these scripts give data output and the other doesn't show data as often.  This is what I created but I am not sure its working as planned.  I am thinking I need to put in a WAIT, DELAY, TIMEOUT or some other pause some how.  Tips appreciated.


    $UserCredential = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    Write-Output "Enabling audit tracking on all accounts"
    . \\s.com\NETLOGON\scripts\email-SET-audit-enabled_v1.0.ps1
    Write-Output "Setting calendars default to Limited Details"
    . \\s.com\NETLOGON\scripts\email-SET-calendar-permissions_v1.0.ps1
    Write-Output "Closing Exchange Online connection"
    Remove-PSSession $Session

    Below is each one of the nested PS1 scripts;


    Get-Mailbox | where-object {$_.AuditEnabled -eq $False } | set-mailbox -AuditEnabled $true |ft -a


    foreach($user in Get-Mailbox -RecipientTypeDetails usermailbox) {
    $calendar = $user.alias+":\calendar"
    Set-MailboxFolderPermission -Identity $calendar -user default -AccessRights LimitedDetails| 
    Where-Object {
    $_.accessrights -NE "LimitedDetails" -and 
    $_.identity -NE "CEO:\calendar" -and 
    $_.identity -NE "HR:\calendar" }}

    Friday, February 9, 2018 2:28 PM

All replies

  • You cannot use "Where-Object" on a "Set" command.

    $users = Get-Mailbox -RecipientTypeDetails usermailbox |
        Where-Object {
            $_.accessrights -NE "LimitedDetails" -and 
            $_.identity -NE "CEO:\calendar" -and 
            $_.identity -NE "HR:\calendar" 
    foreach ($user in $users) {
        Set-MailboxFolderPermission -Identity ($user.alias + ':\calendar') -user default -AccessRights LimitedDetails

    Start here to learn how to use PowerShell and how to write scripts. Learn PowerShell  

    Adding waits or delays is almost never a solution.  This use is just Internet fog caused by people who have not learned basic computing or PowerShell.


    • Edited by jrv Friday, February 9, 2018 2:49 PM
    • Proposed as answer by PRASOON KARUNAN V Friday, February 9, 2018 6:14 PM
    Friday, February 9, 2018 2:47 PM