Random Numbers in Powershell

Powershell Header

In one of my last scripts I needed random numbers in Powershell. Now this Blog post should show you how you can generate random numbers in Powershell very simple.

To create random numbers in Powershell you can use the .NET Framework class “System.Random”. First you have to create a new object.

[Object]$Random = New-Object  System.Random

Lets checkout the $Random object:

$Random | Get-Member
   TypeName: System.Random

Name        MemberType Definition                                                              
----        ---------- ----------                                                              
Equals      Method     bool Equals(System.Object obj)                                          
GetHashCode Method     int GetHashCode()                                                       
GetType     Method     type GetType()                                                          
Next        Method     int Next(), int Next(int minValue, int maxValue), int Next(int maxValue)
NextBytes   Method     System.Void NextBytes(byte[] buffer)                                    
NextDouble  Method     double NextDouble()                                                     
ToString    Method     string ToString()    

Now with this object you can now create random numbers very simple:

Create a random double number:

$Random.NextDouble()

Create a random int number:

$Random.Next()

Create a random number between 1 or 10:

$Random.Next(1,11)

Create a random number between 12 or 25:

$Random.Next(12,26)

Create a random number 1 or 0:

$Random.Next(0,2)

Simple Bean Machine program done in Powershell

Powershell Header

In the last article I posted the C++ Code for a simple Bean Machine output. Now I did the same in Powershell. I know this is not really a fantastic Powershell script, but its good to show others how things get done in Powershell.

Like in the C++ bean machine it works like this:

Bean MachineAnd the Output should look like this:

Bean Machine outputAnd here is how you do this in Powershell:

#Config
[int]$ballCount = 100
[array]$box = @(0, 1, 2, 3, 4, 5)
[string]$line = " +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-->"
[string]$numbers = " 0     5     10     15     20     25     30     35     40     45     50"
[object]$random = New-Object  System.Random

#count
for([int]$i = 0; $i -lt $ballCount; $i++){
	[int]$counter = 0
	
	for([int]$j = 0; $j -lt 5; $j++){
	$leftorright = $random.next(0,2)
	$counter = $counter + $leftorright
	}
	$box[$counter] = $box[$counter] + 1
}

#Output
Write-Host $numbers
Write-Host $line
for ([int]$t = 0; $t -lt 6; $t++){
	[string]$Statusline = ""
	for ([int]$u = 0; $u -lt $box[$t]; $u++){
		[string]$Statusline += "#"
		}
	Write-Host $t "|" $Statusline $box[$t]
	Write-Host $line
}

Simple C++ Bean machine program @KTSI

This is a simple Bean machine program done in C++ for KTSI.

This is how it works, you drop 100 balls and they fall in 6 different boxes.

Bean MachineAnd this is how the C++ output should look like:

Bean Machine outputAnd here is how its done:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main (int argc, char * const argv[]) {
	
	
	// initial
	int ballCount = 100;
	int box[6] = { 0, 0, 0, 0, 0 };
	string line = " +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-->";
	string numbers = " 0     5     10     15     20     25     30     35     40     45     50";
	int leftorright = 0;

	// Need for Random Numbers
	srand ( time(NULL) );
	
	// Rund ballCount (100) zahl = rand() % 2;
	for (int i=0; i<ballCount; i++) {
			int counter = 0;
		
		for (int j=0; j<5; j++) {
			// left or right
			leftorright = rand() % 2;
			counter = counter + leftorright;
		}
		box[counter] = box[counter]++;
			
	}	
	
	// Output
	cout << numbers << endl << line << endl;
	for (int t=0; t<6; t++) {
		cout << t << " |";
		for (int u=0; u < box[t]; u++) {
			cout << "#";
		}
		cout << " " << box[t] << endl << line << endl;
	}
	return 0;
}

I also did this program in Powershell

Enable Tiger “Classic” Style Dock In Leopard and Snow Leopard

defaults write com.apple.dock no-glass -boolean NO; killall DockIf you like the old or classic Dock Style from Mac OS X 10.4 you can enable this in Mac OS X 10.5 (Leopard) and in Mac OS X 10.6 (Snow Leopard).

Mac OS X 10.4 (Tiger) Dock:

Mac OS X 10.4 Tiger DockMac OS X 10.6 (Leopard) Dock:

Mac OS X 10.6 Snow Leopard Dock

  1. Open Mac OS X Terminal in the Utilities folder
  2. Run the following command in the Terminal
    defaults write com.apple.dock no-glass -boolean YES; killall Dock
  3. Now you have the classic Dock activated

If you want to activate the new glass Dock again, you can use the following command:

defaults write com.apple.dock no-glass -boolean NO; killall Dock

Activate SSH on ESXi 4.1 via vSphere Client

Some months ago I wrote a post about how to activate SSH on a ESXi 4.1 via console. But you can also activate SSH with the vSphere Client.

  1. Login to the vSphere Client
  2. Click on the configuration tab
    VMware vSphere Client ESXi
  3. Click on Security profile
  4. VMware vSphere Client ESXi Click on Properties
    VMware vSphere Client ESXi
  5. Click on Remote Tech Support (SSH)
    VMware vSphere Client ESXi
  6. Click on Options and choose the startup policy and click start
    VMware vSphere Client ESXi
  7. Now you can verify that the daemon shows as running in the Services Properties window

Pagefile size limits on Windows Server 2003

Some moths ago I posted a Blogpost called “Pagefile size bigger than 4095MB on Windows Server 2003“. In this post I wrote about the pagefile size limit of 4096MB. Today I got a comment on this post which shows another solution for this.

If you use the /PAE (Wikipedia Link: Physical Address Extension) switch, the pagefile is not limited to 4096MB anymore. Using the PAE switch limits the pagefile to 16TB, this should be enough for the most Windows Server 2003 32-bit systems. If you don’t use PAE the limit of the Pagefile remains 4GB.

By the way, Windows Server 2003 x64 can also have pagefiles up to 16TB and Windows Server 2003 for IA64 systems can have pagefiles up to 32TB. And for all versions, Windows Server 2003 supports up to 16 pagefiles.

For more information on memory limits in Windows Server 2003 check out this TechNet Blog post.

Thank goes to Ethan Anderson for giving me this information.

Create Cisco VLAN Config with Powershell

Powershell HeaderSmall Script for creating a simple CISCO VLAN config.

# Create Cisco VLANs Config
#
# www.thomasmaurer.ch
# (c) 2010 Thomas Maurer
#
#

# First VLAN which should be created?
[int]$VLAN = 1201
# How many VLANs should be created?
[int]$VLANCount = 25
# Start IP Address
[int]$IP = 1
# Subnet
[string]$Subnetmaskv4 = "255.255.255.252"
[string]$Subnetmaskv6 = "64"
# Next IP (Its Important for Subnet)
[int]$AddtoIP = 4
# IPv4 Address Template
[string]$IPv4AddressTemplate = "192.168.1."
# IPv6 Address Template
[string]$IPv6AddressTemplate = "2002:1b50:251:22:192:168:1:"
# Config File Name
[String]$ConfigFile = "C:\Users\tm\Desktop\config.txt"


# Creats Config
for($counter = 0;$counter -le $VLANCount;$counter++) {
	[string]$interfaceconf = "interface Vlan" + $VLAN
	[string]$IPv4conf = "ip address " + $IPv4AddressTemplate + $IP + " " + $Subnetmaskv4
	[string]$IPv6conf = "ipv6 address " + $IPv6AddressTemplate + $IP + "/" + $Subnetmaskv6
	Add-Content $ConfigFile $interfaceconf
	Add-Content $ConfigFile "no shutdown"
	Add-Content $ConfigFile $IPv4conf
	Add-Content $ConfigFile $IPv6conf
	$IP = $IP + $AddtoIP
}