Forum Discussion

tcboeira's avatar
tcboeira
Brass Contributor
Jul 25, 2022

Using a Script to Data to Others

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...
  • farismalaeb's avatar
    Jul 25, 2022

    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.

     

     

     

Resources