none
Use variables from original script in new powershell window RRS feed

  • Question

  • Hi guys,

    i have this script

    $firstname = "aaaaaa"
    $lastname =  "aaaaaaa"
    $email = "xxxxxx.com"
    $username = "$firstname.$lastname"
    
    
    Start powershell -Credential domain\account { 
    
    Import-Module C:\some\path\modul.psm1 -Force
    (or C:\some\path\script.ps1 - script or module doesnt matter as they are both working alone)
    } 
    

    and i need to use variables $email and $username to newly opened powershell window.

    I tried some things i found but none worked. Can someone help me with this?

    • Moved by Bill_Stewart Thursday, January 25, 2018 9:54 PM Unanswerable drive-by question
    Monday, October 23, 2017 12:38 PM

All replies

  • help start -online

    Read carefully and look closely at the examples.


    \_(ツ)_/

    Monday, October 23, 2017 1:06 PM
  • It would be nice if you could be more specific. Seems like -RedirectStandartinput is what i was searching for, but its not working as expected. Ive got some symbols for $email in opened script and username: $firstname = "aaaaaa".$lastname =  "aaaaaaa" . Seems like its reading whole string and not the value of variable.

    • Edited by 3Domco9 Tuesday, October 24, 2017 8:15 AM
    Tuesday, October 24, 2017 8:00 AM
  • Example 7 is the exact answer.


    \_(ツ)_/

    Tuesday, October 24, 2017 8:04 AM
  • I suspect that you haven't read the help for Start-Process.  If you had you would see that it cannot accept a script block.  It can only take a "program path" and an arguments list.

    What you are trying to do cannot be done.

    To start PowerShell with a file:

    Start-Process -FilePath powershell -ArgumentList '-file script.psq',$arg1,$arg2,$arg3

    Now read the help to learn the other options and how they work.  As with the old CMD shell every command has help.  Techs learn how to read the help to find how to use the command.   The help also tells us what the command is intended to do.  Learning to read and understand help is the first thing you must learn to use any technical system. 


    \_(ツ)_/


    • Edited by jrv Tuesday, October 24, 2017 9:03 AM
    Tuesday, October 24, 2017 8:17 AM
  • I read the help but i thought that something is missing from there as my original script is working with script block

    start powershell -Credential domain\username  {
    
    Import-Module C:\some\path\modul.psm1 -Force
    
    $email = read-host 'Input email address'
    $username = read-host 'Input user login'
    
    Modul $username $email
    
    }

    Thats why i asked for help here.

    Can you suggest how can i do this: I have one big script but one part needs to be run under another user credentials. Does not matter if it will be written in the big script as code or if ill just run the script from path.

    Tuesday, October 24, 2017 9:01 AM
  • "start" is the alias for Start-Process. Start-Process does not take a script block. The command line turns the script block into a string that is passed as the first argument. You need to add the remaining arguments to the command. Unfortunately the args cannot be referenced in the command line. A module cannot be called with arguments. You will have to construct the string before passing it to Start-Process.

    \_(ツ)_/

    Tuesday, October 24, 2017 9:13 AM
  • After few hours of trying i found out that if i rewrite the modul.psm1 to big script as script not module, there is no need for another user credentials.

    If someone will need run part of the script as another user, there is a Impersonate User script - https://gallery.technet.microsoft.com/scriptcenter/Impersonate-a-User-9bfeff82 , i have tried this and its working good

    Tuesday, October 24, 2017 12:52 PM