SOLVED

Using a Script to Data to Others

Brass Contributor

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!

3 Replies
best response confirmed by tcboeira (Brass Contributor)
Solution

@tcboeira 

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, 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.
You 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
1 best response

Accepted Solutions
best response confirmed by tcboeira (Brass Contributor)
Solution

@tcboeira 

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.

 

 

 

View solution in original post