Jul 25 2022 05:12 AM
Hello guys
Well, first of all, I apologize in advance as I still use an online translator to be able to communicate.
That said, here's my question:
I have a Script - nothing too complex, I'm still a beginner - that is getting a little big, with many lines.
I thought about splitting them into parts, which would make it easier for me to manage.
I had seen a way to write, for example, a Script that could collect information whenever another was invoked.
Something similar to this example:
Script 01: file1.ps1
$NPC = $Env:COMPUTERNAME
$DATE = date
Script 02: file2.ps1
.\file1.ps1
Write-Host "the pc name is" $NPC ...
Script 03: file3.ps1
.\file1.ps1
Write-Host "Today is" $DATA...
In theory this should work. Via ISE works. But not in practice. I really don't know where my fault would be and if this is really possible.
Can you help me? Clarify and better understand this issue?
Thanks a lot for the help everyone! And good week!
Jul 25 2022 05:36 AM
SolutionHi
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.
Jul 25 2022 10:23 AM
Jul 25 2022 09:40 PM
Jul 25 2022 05:36 AM
SolutionHi
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.