Wednesday, 29 January 2014

Powershell fun

runas /user:Admin powershell
   --> does not give you an elevated admin powershell console
start-Process powershell -Verb runAs 
  --> UAC promt --> Elevated.
Set-ExecutionPolicy RemoteSigned
  --> Allow executation of home-made powershell script (off by default)
Example powershell script to find find who log on to what in domain (similar to psloggedon) by querying HKEY_USERS:

Import-Module ActiveDirectory
#$output = "PSLoggedOn_Results.csv"
$domainname = 'dc=test,dc=com,dc=au'
$allComputers=@(Get-ADComputer -SearchBase $domainname -Filter '*' | Select-Object -ExpandProperty Name)
foreach ($computername in $allComputers) {
write-warning "Connecting to $computername"
Trap {
write-warning "Something went wrong with $computername"
write-warning $_.Exception.Message
Continue
}
$regKey=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::Users, $computername)
$allSid = $regKey.GetSubKeyNames() | where {$_ -match "Classes"}
foreach ($sidC in $allSid) {
$sid = $sidC.Substring(0,46)
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])
$username=$objUser.Value
$computername+","+$username
}
}


Tuesday, 28 January 2014

PythonFu

Example class in python (implementing some awesome underscore classes (http://www.rafekettler.com/magicmethods.html) :


class test:
    def __init__(self,input1, input2):
        self.var1=input1
        self.var2=input2
    def __call__(self,param):
        print "hello "+param
    def __iadd__(self,val):
        self.var1 += val
        self.var2 += val
        return self
    def __str__(self):
        return str(self.var1)+" "+str(self.var2)

obj=test(1,2) #initialize object obj
obj("san")         #equivalent with obj.__call__("san")
obj += 2      #adding 2 to self.var1 and self.var2
print str(b)  #print out self.var1 and self.var2 by calling __str__



Saturday, 25 January 2014

Let's do ... django!

pgmodeler is a sweet tool for creating database in postgresql from scratch. However it doesn't export to django model file, so what do you do?

First, export the database to your postgresql database

Then in your django project modify your settings.py with the database name, credentials, host, port, etc...and run the following command:


python manage.py inspectdb > APP_NAME/model.py

and voila! Here is an example:


from __future__ import unicode_literals
from django.db import models

class Family(models.Model):
#    familyid = models.BigIntegerField(db_column='familyId', primary_key=True)  
    familyid = models.AutoField(db_column='familyId', primary_key=True)     #Change BigIntegerField to AutoField -> Auto increment field!
    familyname = models.CharField(db_column='familyName', max_length=255)
    familydescription = models.CharField(db_column='familyDescription', max_length=1000)
    publicviewsettingid = models.SmallIntegerField(db_column='publicViewSettingId', blank=True, null=True) 
    createon = models.DateTimeField(db_column='createOn', blank=True, null=True) 
    class Meta:
        managed = False         ####<-------------- NOTE THAT FOR "manage.py syncdb"  to work, you need to REMOVE THIS LINE
        db_table = 'Family'


After modifying the model.py to your need and remove the "managed=False" line for the tables that you want to create, you can now drop the table and recreate with "manage.py syncdb"

Now that you already have the models in place, having some dummy data would be nice  so let's use autofixture for that!
Install autofixture:

pip install django-autofixture

Add "autofixture" into the list of INSTALLED_APPS in "settings.py",
run this command to generate 5 Family dummy data:

python manage.py loadtestdata PROJECT.Family:5

And now, for future usage we can put it into "fixture" folder under family application, let's call it initial_data.json:

python manage.py dumpdata > family/fixtures/initial_data.json