Question answer queries will not list results during script.

Welcome Forums General PowerShell Q&A Question answer queries will not list results during script.

This topic contains 11 replies, has 4 voices, and was last updated by

 
Participant
3 months, 4 weeks ago.

  • Author
    Posts
  • #104614

    Participant
    Points: 1
    Rank: Member

    I am trying to configure a PowerShell script that will allow a user to put in a name and then come back with a listing of names to choose from for the next question to be copied and pasted. The issue is that the none of the results come back with any listing to choose from. I have tried the out-string before with success but in this case, I am not able to get any results.

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;
    Start-Sleep 5
    # Shows a listing of all Exchange mailboxes
    $1 = Read-Host "What is the first name of the user that is in need to have email forwarded?"
    $2 = $1
    $list = Get-Mailbox -SortBy Alias | ? {$_.Name -like "$2*"} | Select Alias
     
    $list = Out-String
    
    $box = Read-Host "Copy and paste the mailbox name"
    $user = $box
    
    $3 = Read-Host "To whom do you want to forward the email to?"
     
    $flist = Get-Mailbox -SortBy Alias | ? {$_.Name -like "$3*"} | Select Alias
    $flist = Out-String
    
    $fbox = Read-Host "Copy and paste the forward to mailbox name"
    $fbox
    
    #Set-Mailbox -Identity $user #-DeliverToMailboxAndForward $false
    Get-Mailbox -Identity $user | select name,ForwardingAddress,DeliverToMailboxAndForward | FT -AutoSize
    
    
    
    $answer = Read-Host 'Should email continue to go to' $box+'s mailbox as well? Please type Yes or No'
    $y = $answer
        If ($y -like "y*")
        {Set-Mailbox -Identity $user -ForwardingAddress $fbox -DeliverToMailboxAndForward $true}
        ELSE {Set-Mailbox -Identity $user -ForwardingAddress $fbox -DeliverToMailboxAndForward $false; Return}
    Start-Sleep 5
    
    
    Get-Mailbox -Identity $user | select name,ForwardingAddress,DeliverToMailboxAndForward | FT -AutoSize
    
    
  • #104618

    Participant
    Points: 22
    Rank: Member

    There appears to be a problem between lines 26-31.

    Where are you calling the variable to prompt the user? I see you assign one.

    If you are using vscode, you should see some scriptanalyzer errors if there are any.

    Will look more when I'm back at my computer, hard to read on an iPhone.

    • #104620

      Participant
      Points: 1
      Rank: Member

      Line 8 and line 16 are not giving any output to what is asked. If I run the code line by line the output comes back fine but when running through the script I am not receiving any results from the following:

      $list = Get-Mailbox -SortBy Alias | ? {$_.Name -like "$2*"} | Select Alias

      $list = out-string ——No output to the user to choose from ———-

      And

      $flist = Get-Mailbox -SortBy Alias | ? {$_.Name -like "$3*"} | Select Alias
      $flist = Out-String ——No output to the user to choose from ———-

  • #104623

    Participant
    Points: 1
    Rank: Member

    I am doing all of this through Powershell ISE.

  • #104626

    Participant
    Points: 22
    Rank: Member

    Line 6 looks suspect, is that a valid parameter '-SortBy'? I would think you would instead pipe the results of get-mailbox to 'Sort-Object -Property'

    ***Edit
    I guess it is so disregard. I haven't had to do much with Exchange in years so pardon my ignorance. I copied your code as is into my install of VSCode and let ScriptAnalyzer do it's thing and there were a few errors:

    RuleName Severity ScriptName Line Message
    ——– ——– ———- —- ——-
    PSAvoidUsingCmdletAliases Warning Exchange-t 6 '?' is an alias of 'Where-Object'. Alias can
    est.ps1 introduce possible problems and make scripts
    hard to maintain. Please consider changing
    alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 6 'Select' is an alias of 'Select-Object'.
    est.ps1 Alias can introduce possible problems and
    make scripts hard to maintain. Please
    consider changing alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 15 '?' is an alias of 'Where-Object'. Alias can
    est.ps1 introduce possible problems and make scripts
    hard to maintain. Please consider changing
    alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 15 'Select' is an alias of 'Select-Object'.
    est.ps1 Alias can introduce possible problems and
    make scripts hard to maintain. Please
    consider changing alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 22 'select' is an alias of 'Select-Object'.
    est.ps1 Alias can introduce possible problems and
    make scripts hard to maintain. Please
    consider changing alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 22 'FT' is an alias of 'Format-Table'. Alias can
    est.ps1 introduce possible problems and make scripts
    hard to maintain. Please consider changing
    alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 34 'select' is an alias of 'Select-Object'.
    est.ps1 Alias can introduce possible problems and
    make scripts hard to maintain. Please
    consider changing alias to its full content.
    PSAvoidUsingCmdletAliases Warning Exchange-t 34 'FT' is an alias of 'Format-Table'. Alias can
    est.ps1 introduce possible problems and make scripts
    hard to maintain. Please consider changing
    alias to its full content.
    PSAvoidTrailingWhitespace Information Exchange-t 7 Line has trailing whitespace
    est.ps1
    PSAvoidTrailingWhitespace Information Exchange-t 14 Line has trailing whitespace
    est.ps1
    PSUseDeclaredVarsMoreThanAssignment Warning Exchange-t 6 The variable 'list' is assigned but never
    s est.ps1 used.
    PSUseDeclaredVarsMoreThanAssignment Warning Exchange-t 15 The variable 'flist' is assigned but never
    s est.ps1 used.

  • #104629

    Participant
    Points: 1
    Rank: Member

    I have hopefully cleaned the PowerShell script. So far it seems to work. It seems to be a little hit and miss with the search results. It looks as though that I will need to get my hands on VScode in the future.

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010;
    Start-Sleep 5
    
    # Shows a listing of all Exchange mailboxes
    $1 = Read-Host "What is the first name of the user that is in need to have email forwarded?"
    Get-Mailbox -SortBy Alias | Where-Object{$_.Name -like "$1*"} | Select-Object Alias | FT -AutoSize;
    
    $box = Read-Host "Copy and paste the mailbox name"
    $user = $box
    
    $3 = Read-Host "To whom do you want to forward the email to?"
    Get-Mailbox -SortBy Alias | Where-Object{$_.Name -like "$3*"} | Select-Object Alias | FT -AutoSize;
    
    $fbox = Read-Host "Copy and paste the forward to mailbox name"
    #Set-Mailbox -Identity $user #-DeliverToMailboxAndForward $false
    Get-Mailbox -Identity $user | Select-Object name,ForwardingAddress,DeliverToMailboxAndForward | FT -AutoSize
    $answer = Read-Host 'Should email continue to go to' $box+'s mailbox as well? Please type Yes or No'
    $y = $answer
        If ($y -like "y*")
        {Set-Mailbox -Identity $user -ForwardingAddress $fbox -DeliverToMailboxAndForward $true}
        ELSE {Set-Mailbox -Identity $user -ForwardingAddress $fbox -DeliverToMailboxAndForward $false}
    Start-Sleep 5
    
    Get-Mailbox -Identity $user | Select-Object name,ForwardingAddress,DeliverToMailboxAndForward | FT -AutoSize
    
    • #104633

      Participant
      Points: 22
      Rank: Member

      ScriptAnalyzer is just a PS module. You can get it from PowerShellGallery.com and install, just have to manually invoke it against your script. If you use code, download the PowerShell extension and it comes with it. No additional steps required.

    • #104683

      Participant
      Points: 1
      Rank: Member

      Thank you for all of your input and suggestions on how to make this much more simple. I will use all of the tools that you have given to me in the future. I only wish that I had the time to focus on one part of the IT world to become more proficient.
      Thanks again

  • #104635

    Participant
    Points: 160
    Helping Hand
    Rank: Participant

    You are asking for trouble if you try to automate with copying and pasting. For instance, if I put nothing in there, then it's just a * and you are making changes to a whole lotta stuff you don't want to. Even if someone didn't want to be malicious, it would be easy to type Smith and make more changes that expected. Then you start putting code wrappers to ensure only one object is returned, etc. and then you will have a ton of code and not catch your issues. You should really consider making a GUI interface which is designed to provide controls (dropdown) and validation vs trying to capture plain text. Take a look at Powershell + WPF:

    https://mcpmag.com/articles/2016/04/28/building-ui-using-powershell.aspx

    • #104698

      Participant
      Points: 1
      Rank: Member

      Thank you, I will look into the information that you have offered. I have always wanted to create a GUI frontend for some of my PowerShell scripts but everyone was pointing towards PowerShell Studio which I am not that proficient in PowerShell to justify spending that amount of money.
      Thanks again

  • #104645

    Participant
    Points: 213
    Helping Hand
    Rank: Participant

    You are unnecessarily repeating yourself in what you've posted.

    Just grab what you want a pipe that to the next cmdlet.

    # Get target user information from ADDS and get email alias
    $UserInfo = Read-Host "What is the first name of the user that is in need to have email forwarded?"
    $UserList = Get-ADUser -Filter * | Where-Object -Property GivenName -Like "$UserInfo*" | 
    Select-Object -Property GivenName,SamAccountName | 
    Out-GridView -OutputMode Single -Title 'Select a user from the list provided'
    Get-Mailbox -Identity $UserList.SamAccountName | Select-Object -Property Alias,name,ForwardingAddress,DeliverToMailboxAndForward
    

    This allows you to only as the first and last question or really only the first. If the goal is to forward the selected name then just grab that and go. All the sorting stuff is automatic on the properties provided.

    • #104762

      Participant
      Points: 1
      Rank: Member

      Thank you

The topic ‘Question answer queries will not list results during script.’ is closed to new replies.