How to access a variable in one script in another script

Welcome Forums General PowerShell Q&A How to access a variable in one script in another script

Viewing 15 reply threads
  • Author
    Posts
    • #223566
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      How can I access a variable in Script1 in a different script: Script2?

    • #223578
      Participant
      Topics: 3
      Replies: 342
      Points: 1,130
      Helping Hand
      Rank: Community Hero

      I think you accidentally typed this into powershell.org instead of bing/google. However, in case i’m mistaken, check out this link.

      https://stackoverflow.com/questions/1864128/load-variables-from-another-powershell-script

    • #223596
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      I just want to access the variable in script 1 in Script2, I do not want to run the whole script i.e. Script1 from Script2

    • #223611
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      I just want to access the variable in script 1 in Script2, I do not want to run the whole script i.e. Script1 from Script2

      That’s impossible. A variable starts to exist when its assignment has run. So either you include the other script or you parse it by yourself and pick out what you need or you outsource the variables and setting you need in both scripts to a third script you could dot source in both scripts.

    • #223683
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      Thanks. What about making it is a module and exporting the variable. I am getting an error saying the The Export-ModuleMember can only be called from inside a module whereas I am inside a module only.

    • #223692
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      You may describe what you’re ACTUALLY trying to accomplish – not the attempted solution. There might be a better way. 😉

      You may also want to (re-)read the help for about_Scopes ….

    • #223695
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      I want to access the output file stored a in variable in one script as input of of another script. Say a text file.

    • #223698
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      I want to access the output file stored a in variable in one script as input of of another script. Say a text file.

      Hmmm … that’s still quite vague. Did you place the variable there or is it dynamic or where does it come from? You still can save it in a settings file you access from both scripts. Or you should elaborate much more to make us understand. 😉

    • #223701
      Participant
      Topics: 30
      Replies: 74
      Points: 402
      Rank: Contributor
    • #223704
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      to save the content of a variable to a file check out this link …

      I’d think Sam speaks about a file path assigned to a variable in another script file – not the content of the file itself. 😉

    • #223707
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      I created a variable with a path of a text file in Script1. In Script1, I am using Out-File to store the output of this script to this file.

      $out=E:\myfiles\out_29-4-20.txt
      
      $finaloutput | Out-File $out
      

      I want to use this $out variable as input of another script. Since the $out will change each time Script1 is run, I want to make sure , I use the correct file as input in the other script.

    • #223710
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      I want to use this $out variable as input of another script. Since the $out will change each time Script1 is run, I want to make sure , I use the correct file as input in the other script.

      You could use an external place to store this file path. Either a ragistry key or a settings file in plain text or CSV or XML or JSON. You could output the file path as the result of the script execution of Script1 and use this as the parameter to call the script 2 from script 1 or – as I already mentioned above – you parse script 1 from script 2.

      Who is changing script 1 actually?

    • #223713
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      Since the script runs on a regular basis say twice in a week. Each time the Script1 runs, it will create a new file with different name and this same file needs to be used as input for the other script.

    • #223719
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      Since the script runs on a regular basis say twice in a week. Each time the Script1 runs, it will create a new file with different name and this same file needs to be used as input for the other script.

      OK, so it’s dynamic. I actually already asked this. :-/ When you know the algorythm script1 uses to create the file name you can use the same in script 2. If the file is always created in the same folder you could simply always use the most recent file in that folder. OR … and that will not change … you write the file name script1 creates in a place accessible for script2. There’s no other way.

    • #223734
      Participant
      Topics: 15
      Replies: 68
      Points: 305
      Rank: Contributor

      Thanks, I am aware of this approach. Thought may be there is a better way. 🙂

    • #223737
      Participant
      Topics: 30
      Replies: 74
      Points: 402
      Rank: Contributor

      the name of the file changes each time you run it. if I understand you correctly if you run the file today the filename would be out_29-04-20.txt

      #check if the file exists
      
      $date = get-date -format “dd-MM-yy”
      $filename = “out_”+ $date +”.txt”
      $fileToCheck = “C:\temp\$filename”
      if (Test-Path $fileToCheck -PathType leaf) {
      write-host “file exists”
      } ELse {
      write-host “file not found” -ErrorAction Stop
      }
      
      #second script
      $out = get-content $fileToCheck
      $out
      

       

       

      Paul

      • This reply was modified 1 month ago by acer460527.
      • This reply was modified 1 month ago by acer460527.
Viewing 15 reply threads
  • You must be logged in to reply to this topic.