Welcome › Forums › General PowerShell Q&A › How to access a variable in one script in another script
- This topic has 15 replies, 4 voices, and was last updated 9 months ago by
Participant.
-
AuthorPosts
-
-
April 29, 2020 at 4:59 am #223566
How can I access a variable in Script1 in a different script: Script2?
-
April 29, 2020 at 5:10 am #223578
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
-
April 29, 2020 at 5:51 am #223596
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
-
April 29, 2020 at 6:45 am #223611
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.
-
April 29, 2020 at 11:06 am #223683
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.
-
April 29, 2020 at 11:20 am #223692
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 ….
-
April 29, 2020 at 11:29 am #223695
I want to access the output file stored a in variable in one script as input of of another script. Say a text file.
-
April 29, 2020 at 11:47 am #223698
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. 😉
-
April 29, 2020 at 11:47 am #223701
to save the content of a variable to a file check out this link
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7
to read content from a file check out this link
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-content?view=powershell-7Paul
-
April 29, 2020 at 12:05 pm #223704
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. 😉
-
April 29, 2020 at 12:06 pm #223707
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.
PowerShell123$out=E:\myfiles\out_29-4-20.txt$finaloutput | Out-File $outI 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.
-
April 29, 2020 at 12:11 pm #223710
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?
-
April 29, 2020 at 12:18 pm #223713
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.
-
April 29, 2020 at 12:25 pm #223719
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.
-
April 29, 2020 at 12:45 pm #223734
Thanks, I am aware of this approach. Thought may be there is a better way. 🙂
-
April 29, 2020 at 12:52 pm #223737
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
PowerShell1234567891011121314#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$outPaul
-
This reply was modified 9 months ago by
acer460527.
-
This reply was modified 9 months ago by
acer460527.
-
This reply was modified 9 months ago by
-
-
AuthorPosts
- The topic ‘How to access a variable in one script in another script’ is closed to new replies.