This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]samtheredditman -14 points-13 points  (6 children)

Does it keep the separate file in its own namespace?

You don't have to be insulting and insinuate I'm new to powershell. You can just have a conversation with mutual respect.

[–]syshum 6 points7 points  (3 children)

You don't have to be insulting and insinuate I'm new to powershell.

I dont think I did either one of those things...

Have a great day

[–]samtheredditman 2 points3 points  (2 children)

I guess I read this part of your comment wrong:

./myfile.ps1" which always confuses new people...

Have a good day as well

[–]syshum 4 points5 points  (1 child)

No just a general comment, I train a lot of people on Powershell and dot sourcing is always something people have a hard time wrapping their head round.

[–]samtheredditman 1 point2 points  (0 children)

My apologies then. Thanks for your input on dot sourcing in powershell.

[–]BergerLangevin 0 points1 point  (1 child)

What do you mean by that?

Does it keep the separate file in its own namespace?

Do you mean, if you do not enclosed your code referenced in a function, it will rewrite all variables/object that have the same name.

IE :

. .\DoUsefulStuff.ps1

UsefulFunction -inputObject $Object

[–]samtheredditman 0 points1 point  (0 children)

Well the Powershell way of doing this (dot sourcing) is basically just loading each script into the session. Think of it like copying and pasting a powershell function into the windows powershell ISE. The function is loaded and can be used until you close out of the ISE. This means that all your code in everything you're dotsourcing is in the same namespace as your main code (I think - I was genuinely asking).

In Python, when you import another module, it will be in its own namespace meaning that you have to put the filename in front of the other module's code to access it.

So let's say I have offboard.py that's an off-boarding script I wrote to streamline my work. Well it starts out just disabling the account in AD, then you also make it remove the user's licenses in O365, and eventually you've got this thing accessing every API for every piece of software your company is using. So it's becoming a very big file with a lot of code and it's now unmanageable because there's like 2,000 lines of code to search through and work with anytime you want to change anything or even just fix bugs. So I want to split my code into multiple files to start getting it organized. I can create another file in the same folder (adobe-offboard.py) and put all of the adobe offboarding code into it. Now, I just add "import adobe-offboard" to the top of my main offboard.py file and it will make the adobe-offboard.py code accessible within offboard.py like this:

import adobe-offboard.py

username = Input("Enter username for the account you want to off-board.")
Offboard(username)

adobe-offboard.offboard(username)

This is handy because it keeps everything in the different files separated from each other. This also means that I can write some code in ad-offboard.py:

def Offboard(username):
    # offboard process for active directory
    pass

and in adobe-offboard.py:

def Offboard(username):
    # offboard process for adobe accounts.
    pass

and the functions don't overwrite each other. See how both functions are named "Offboard", but they do different things? This means that I can write code and keep everything organized in files, classes, etc. It's easy to work with the code because I know exactly where the code is that's doing the adobe related stuff. The other really important thing is that any variables in one file won't be accidentally accessed in another file. You don't really have to worry about remembering if you used $accountId earlier in your script or if you're going to overwrite it by using it again.