Announcement

Collapse
No announcement yet.

Looking to open different powershell script from current and close current

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking to open different powershell script from current and close current

    Hi,
    Im currently building a menu based PowerShell script and thought its probably easier to call on other scripts rather than just having one continuous script which is a pain to see whats going on. After doing a little reading I thought I had it mastered but sadly not. Here is my code the running script is in the same directory as the script I am trying to call

    Code:
    $PSScriptRoot=Split-Path-Parent-Path$MyInvocation.MyCommand.Definition 
    Code:
    write-host"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    
    write-host"AD Management Menu"
    write-host
    write-host"Press 1: To Create A New User Account"
    write-host"Press 2: To Manage O365 Accounts"
    write-host"Press Q: To Quit"
    write-host
    write-host
    write-host
    write-host"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
    
    $MM=Read-Host"Please Make Your Selection"
    Switch ($MM) {
    '1' {
    $PSScriptRoot+"\UserCreation.ps1"
    }
    '2' { 
    Is it obvious what Im doing incorrectly? Thanks

  • #2
    Change your $PSScriptRoot variable to this:
    Code:
    $PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path
    See if that fixes it for you.
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment


    • #3
      Thanks but it didn't open the other script I was after it just sits in the current "do" loop as below

      Code:
      do
      
      {
      
      write-host"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      
      write-host"AD Management Menu"
      
      write-host
      
      write-host"Press 1: To Create A New User Account"
      
      write-host"Press 2: To Manage O365 Accounts"
      
      write-host"Press Q: To Quit"
      
      write-host
      
      write-host
      
      write-host
      
      write-host"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
      
      $MM=Read-Host"Please Make Your Selection"
      
      Switch ($MM) {
      
      '1' {
      
      $PSScriptRoot=Split-Path$MyInvocation.MyCommand.Path
      
      $PSScriptRoot+"\UserCreation.ps1"
      
      }
      
      '2' {
      
      Set-ExecutionPolicyRemoteSigned
      
      $credential=Get-Credential
      
      Import-ModuleMSOnline
      
      Connect-MsolService-Credential$credential
      
      }
      
      'Q' {
      
      return
      
      }
      
      }
      
      }
      
      until ($MM-eq'Q')
      
      cls 
      The script I am trying to call is in the same directory as the running script.

      Comment


      • #4
        Oh, I see, you're just echoing the path and not executing the script. Try this:
        Code:
        & "$PSScriptRoot\UserCreation.ps1"
        Regards,
        Jeremy

        Network Consultant/Engineer
        Baltimore - Washington area and beyond
        www.gma-cpa.com

        Comment


        • #5
          Bingo - thanks worked a treat

          Comment


          • #6
            Glad to help.
            Regards,
            Jeremy

            Network Consultant/Engineer
            Baltimore - Washington area and beyond
            www.gma-cpa.com

            Comment


            • #7
              Slightly different situation but... If I have a switch statement and then halfway through that switch statement I have another switch statement and then want to finish off the previous switch statement - is this possible? Or should I be going about it differently? As currently it seems to jump out of the switch Im in

              Code:
              write-host  "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
              write-host          "AD User Management Menu"                              
              write-host                                                           
              write-host          "Press 1: To Create Local User Account"
              write-host          "Press 2: To Link User to O365"              
              write-host          "Press 3: To Disable A User Account"
              write-host          "Press Q: To Quit"        
              write-host                                         
              write-host
              write-host
              write-host "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
              $UM1 = Read-Host "Please Make Your Selection"
              
              Switch ($UM1) {
              
              '1' {
              {
              
              $FirstName = Read-Host "Please specify the users First name"
              $Surname = Read-Host "Please specify users Surname"
              $Username = $Firstname+"."+ $Surname
              $SamAccountName = $Username
              $DisplayName = $Firstname+" "+$Surname
              
              
              
              
              write-host  "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
              write-host          'Site'                              
              write-host                                                           
              Write-Host          '1. Site A'
              Write-Host          '2. Site B'
              write-host         
              write-host
              write-host  "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
              $DV1 = Read-Host "Please select the site for this user"
              
              
              #+++++++++++++++++++++++++++++++++++++++++++++#
              #         Site Section                         #
              #                                             #  
              #+++++++++++++++++++++++++++++++++++++++++++++#
              
              Switch ($DV1) {
              '1'
                  {
              $UserEmail = $Username+"@Domain.com"
              $wwwHomePage = "www.Domain.com"

              Comment


              • #8
                Managed to sort it with an IF statement

                Comment


                • #9
                  You should be able to do a switch within a switch. The code you posted looks incomplete. But glad you got it sorted.
                  Regards,
                  Jeremy

                  Network Consultant/Engineer
                  Baltimore - Washington area and beyond
                  www.gma-cpa.com

                  Comment

                  Working...
                  X