nbalonso.com

Scripting Evolution

| Comments

Anyone that write scripts to automate things, soon ends up with a folder filled with many old written script that did specific tasks.

This is always a handy thing, because you can re-use scripts or parts to accomplish different tasks quicker, but as it happens when you are learning a new language, reading thing you wrote long time ago makes you realize of mistakes you did. This is usually because you didn’t know better back then.

When this happens to me it causes me to:

  1. Feel ashamed thinking that someone probably did realize your code was crappy
  2. Feel proud that at least now you are at a stage when you see it by yourself.

The reason behind this post is that recently I’ve been trying to autome something with Powershell and felt like this

So yeah. Here some Powershell that I won’t be proud of (hopefully soon).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# ChangePassword.ps1

#Usage eg.
# .\ChangePassword.ps1 -key 1,0,5,9,6,34,254,211,4,4,192,23,168,54,33,2,1,34,2,7,6,9,35,37

# We must get the decryption key
Param(
  $key
)

######Target user########
$user = "ITadmin"
#########################

if (-not $key) {
  write-host "`r`nYou must pass the decryption key as parameter`r`n"
  exit
}

# Don't display errors!
$ErrorActionPreference = "SilentlyContinue"
# Stop on errors
# $ErrorActionPreference = "stop"

$password = (Get-Content $PSScriptRoot/Encrypted.bin | ConvertTo-SecureString -key $key)
if (-not $password) {
  write-host "`r`nThe provided decryption key is incorrect`r`n"
  exit
}
$computer = $env:COMPUTERNAME


# Returns a SecureString as a String.
function ConvertTo-String {
  param(
    [System.Security.SecureString] $secureString
  )
  $marshal = [System.Runtime.InteropServices.Marshal]
  try {
    $intPtr = $marshal::SecureStringToBSTR($secureString)
    $string = $marshal::PtrToStringAuto($intPtr)
  }
  finally {
    if ( $intPtr ) {
      $marshal::ZeroFreeBSTR($intPtr)
    }
  }
  $string
}

##First check wether user exists
$objComputer = [ADSI]"WinNT://$computer,computer"
$colUsers = ($objComputer.psbase.children | Where-Object {$_.psBase.schemaClassName -eq "User"} | Select-Object -expand Name)
$userFound = $colUsers -contains $user
if (! $userFound) {
  write-host "The $user account did not exist."
  write-host "Creating $user"
  $objOu = [ADSI]"WinNT://$computer"
  $objUser = $objOU.Create("User", $user)
  $objUser.setpassword((ConvertTo-String $password))
  $objUser.SetInfo()
}
else {
  write-host "The $user account exists."
  write-host "Changing it's password"
  $objUser = [ADSI]"WinNT://$computer/$user,user"
  try {
   $objUser.SetPassword((ConvertTo-String $password))
   $objUser.SetInfo()
  }
  catch [System.Management.Automation.MethodInvocationException] {
   write-host "Cannot reset password for '$computer\$user' due the following error: '$($_.Exception.InnerException.Message)'"
   exit
  }
}

#Now that the user exists and has the new password:
$objUser = [ADSI]("WinNT://$computer/$user")
$objGroup = [ADSI]("WinNT://$computer/Administrators")
#Adding to admin group
$objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path)
# ADS_UF_DONT_EXPIRE_PASSWD http://support.microsoft.com/kb/305144
$objUser.UserFlags = 65536
$objUser.SetInfo()

write-host "`r`nSuccess!`r`n"

Blog Migration

| Comments

If you ever visited this site before you might have noticed a slight change in the looks of it.

I recently decided to move the blog to Octopress.

Initially it all started with a RapidWeaver template in a paid shared hosting; then I moved to a Wordpress and now to Octopress in Red Hat’s OpenShift.

The website is hosted in a free small diy cartridge with a custom install of Nginx, cached for free by CloudFare. All for just static content! so yeah speed :)

And here is a graphical view of the evolution over these years

The website has lost the built-in search capability but… meh

Create VNC Shortcuts

| Comments

If you want to create a shortcut where you can double click and launch a VNC connection, you can just modify this template and save it as servername.vncloc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>URL</key>
  <string>vnc://192.168.1.200</string>
  <key>restorationAttributes</key>
  <dict>
      <key>autoClipboard</key>
      <true/>
      <key>controlMode</key>
      <integer>0</integer>
      <key>isFullScreen</key>
      <false/>
      <key>quality</key>
      <integer>3</integer>
      <key>scalingMode</key>
      <true/>
  </dict>
</dict>
</plist>