Forum Discussion
Using a Script to Data to Others
- Jul 25, 2022
Hi
I will try to make the explanation short and simple so you can easily translate it into your language.
It's normal and OK for the script to become bigger and bigger over time as you add more features and code to it; the thing is not in splitting it but rather tunning it.
What you propose won't work as expected.
If you need to split your script into two, here is how you can do it.
File1.ps1:
$PCName = $Env:COMPUTERNAME Return $PCName
File2.ps1
$x=.\file1.ps1 Write-Host "the pc name is" $X
The result when you call File2.ps1 is as the following the pc name is My-PC
File1 gets the value, and using Return will return the value from File1.ps1 and pass it to the caller, in this case, File2.ps1.
Even though it's highly recommended to use PowerShell Function and Parameter to do such thing rather than using this approach.
I wrote an article as a starting point to learn PowerShell parameters and function.
Take a look and if you have a question, let me know so we can go in the right direction.
Enjoy coding.
---------------
If this answer helps, please click on Best Response.
Hi
I will try to make the explanation short and simple so you can easily translate it into your language.
It's normal and OK for the script to become bigger and bigger over time as you add more features and code to it; the thing is not in splitting it but rather tunning it.
What you propose won't work as expected.
If you need to split your script into two, here is how you can do it.
File1.ps1:
$PCName = $Env:COMPUTERNAME
Return $PCName
File2.ps1
$x=.\file1.ps1
Write-Host "the pc name is" $X
The result when you call File2.ps1 is as the following the pc name is My-PC
File1 gets the value, and using Return will return the value from File1.ps1 and pass it to the caller, in this case, File2.ps1.
Even though it's highly recommended to use PowerShell Function and Parameter to do such thing rather than using this approach.
I wrote an article as a starting point to learn PowerShell parameters and function.
Take a look and if you have a question, let me know so we can go in the right direction.
Enjoy coding.
---------------
If this answer helps, please click on Best Response.
- tcboeiraJul 25, 2022Brass ContributorHi, Thanks for the feedback and the instructions. Actually thanks for the lesson!
I'm still reading and re-reading to create a better understanding.
When I talked about splitting the script, initially the idea was to take advantage of being able to use these "parts" for other Scripts. But that's ok, I understood the part that it's better to create and maintain one for each situation and not use the created to chain a certain goal.
Well, anyway, I've seen - in bash - an option like this. As I am now learning Powershell and with this demand for Scripts, I thought the same could be done.
I'm at the moment, using the examples that you commented, and studying if with the use of a function or something, I can use a single file/script as a source of several collections to be distributed to others who may need it.
But anyway, thank you very much. I'll read some more and if I have any doubts I'll come back. Soon I'll classify it and put it as solved.- farismalaebJul 26, 2022Steel ContributorYou are on the right track. script learning comes through practice and reading.
Use the approach you like, and that fits your requirements.
Keep tracking of the impact and execution speed, and security risk that can come from using any approach.
Last thing, would you please mark the answer as Best Response. Thanks
Fee; free and ask whatever you want