I was converting one of my AD solutions from PowerShell to Python (hobby that I have developed recently to pass time on holidays )
That solution required getting current DC & current directory which I have found is available in os library.
So, I thought lets blog about all the common things that you can do with this library, as these are common values required in day-to-day scripting.
How to fetch the domain name, domain controller, hostname, current directory, current login etc.?
Get HostName:
Import os
os.environ[‘COMPUTERNAME’]
Get Logon Domain Controller:
Import os
os.environ[‘LOGONSERVER’]
Get Domain:
Import os
os.environ[‘USERDNSDOMAIN’]
Get Current User:
Import os
os.environ[‘USERNAME’]
or
os.getlogin()
You will be able to get pretty much everything you can get with set command on cmd.
Python:
Get Current Working Directory:
Import os
os.getcwd()
Change Directory:
Import os
os.chdir(‘c:\\temp’)
Make Directory:
Import os
os.mkdir(‘testing’)
List Directories:
Import os
os.listdir()
To find all the commands in OS module just type dir(os):
os library is very handy for a system administrator working with python.
I hope examples I have quoted here are helpful for the community.
Thanks for reading …
Tech Wizard