Skip to content

Instantly share code, notes, and snippets.

View schbrongx's full-sized avatar
🐽

Schbrongx schbrongx

🐽
View GitHub Profile
@schbrongx
schbrongx / get-vms-by-os.ps1
Created April 4, 2023 15:01
PS1: Connect to vSphere, fetch VMs, output grouped by OS: Linux, Windows and Others
if (Get-Module -ListAvailable -Name VMware.PowerCLI) {
Import-Module VMWare.PowerCLI
}
else {
try {
Install-Module -Name VMware.PowerCLI
}
catch {
exit 1
}
def setup_yaml():
""" add a yaml representer which preservers the order when dumping an OrderedDict """
representer_ordered_dict = lambda self, content: self.represent_mapping('tag:yaml.org,2002:map', content.items())
yaml.add_representer(collections.OrderedDict, representer_ordered_dict)
def replace_in_nested_dict(dict_in, str_old, str_new):
''' loop over ALL levels of a dictionary, replacing str_old with str_new
i am working with collections.OrderedDict(), which may be replaced with
an ordinary dict. i have tested it with both.
'''
if not isinstance(dict_in, dict):
raise TypeError('First argument has to be of type dict. Is type: ' + type(dict_in))
if not isinstance(str_old, str):
raise TypeError('Second argument has to be of type str. Is type: ' + type(str_old))
if not isinstance(str_new, str):
def print_nested(_obj, _iterator=0):
''' iterate through an iterable object
- print_nested if parameter is another iterable object
- print value if parameter is not iterable
DEPENDENCY: is_iterable.py: https://gist.github.com/schbrongx/ad66fdd50d72b0042cd26c2689b0c10f
'''
for param in _obj:
if is_iterable(_obj[param]):
log.debug(' ' * _iterator + param + ': ')
def is_iterable(_obj):
''' Returns True when _obj is iterable and not a string '''
import types
if not isinstance(_obj, types.StringTypes):
try:
iterable = iter(_obj)
except:
iterable = False
else:
iterable = False