Friday, August 14, 2009

Using System.Math and System.Convert to do some basic stuff in Powershell

Using System.Math and System.Convert to do some basic stuff in Powershell


So I was getting a large number back from a Win32_LogicalDisk query and I didn’t want to say there was 135.261981964111Gb free. I wanted it to say 135.2 Gb free, or something of the sort.

PS>
PS> $cdrive = Get-WMIObject -class win32_logicaldisk | where {$_.DeviceID -match "C:"}
PS> $cdrive.Freespace
145236447232
PS> $cdrivegb = $cdrive.Freespace/1gb
PS> $cdrivegb
135.261981964111
PS>



So using the Round function isn’t too hard to use.

PS> $cdrivegb = [math]::Round($cdrive.Freespace/1gb,2)
PS> $cdrivegb
135.26
PS>


Perfect…. Just what I wanted. Now I have a number that looks like I want.

PS> $cdrivegb = [math]::Round($cdrive.Freespace/1gb,3)
PS> $cdrivegb
135.262
PS> $cdrivegb = [math]::Round($cdrive.Freespace/1gb,5)
135.26198
PS>


As you can see the 2 parameters and syntax are Round(Number to be rounded, digits to round to).

Lets explore [math] a bit more….

You can also call System.Math like this …

PS> $cdrivegb = [system.math]::Round($cdrive.Freespace/1gb,3)
PS> $cdrivegb
135.262


Lets look at how System.Math.Round handles Single vs. Double vs. UInt64 vs. UInt32 vs. Decimal….
Single
Double
Int16 (Short)
Int32 (Integer)
Int64 (Long)
UInt32 These are for unsigned 32bit integers
UInt64 These are for unsigned 64bit integers
Decimal

See here for the Methods and Properties of all the System.Math.Class

Lets also look at System.Convert which will give us the ability to change types from one to another.

See here for the MS Type Conversion Tables

See here for the MS Conversion Summary

PS> $cdrive

DeviceID : C:
DriveType : 3
ProviderName :
FreeSpace : 145236447232
Size : 200046518272
VolumeName : C Drive

PS> $c = $cdrive.Freespace
PS> $c | gm

TypeName: System.UInt64

Output Truncated…

PS>
PS> $c
145236447232

At this point $c = 145236447232 and its Type is a System.UInt64 (unsigned 64 bit integer).
What if we wanted to make that into a Type:System.String

PS>
PS> $c_string = [Convert]::ToString($c)
PS> $c_string
145236447232
PS> $c_string | gm

TypeName: System.String

Output Truncated…

PS>

So using [System.Convert] we were able to change that long number into a String.

But now that it is a Type:String it is NOT a number anymore…. Looks like you can not do math on a Type:String.
PS> $c_string
145236447232
PS> $c_string+1
1452364472321
PS>

You can do math with Type:UInt64
PS> $c
14523644722
PS> $c+1
14523644723
PS>



You can also do math with Type:Decimal. $c is still an UInt64
PS> $c_decimal = [convert]::ToDecimal($c)
PS> $c_decimal
PS>

Lets review our the variables we have already setup.
PS> $c | gm

TypeName: System.UInt64
Output Truncated…

PS> $c
145236303872
PS> $c_decimal | gm

TypeName: System.Decimal
Output Truncated…

PS> $c_decimal
145236303872

Here are a few basic arithmetic operations

PS>
PS> $c_decimal*10
1452363038720
PS\> $c_decimal*15
2178544558080
PS> $c_decimal
145236303872
PS> $c_decimal+1
145236303873
PS\> $c_decimal+1,222,333,444,555,666
Cannot convert "System.Object[]" to "System.Decimal".
At line:1 char:12
+ $c_decimal+1 <<<< ,222,333,444,555,666 PS> $c_decimal+1222333444555666
1222478680859538
PS> $c_decimal*32513413421341
4722127991577844645732352
PS> $c_decimal*32513413421341351341341342123124235134
Cannot convert value "3.25134134213413E+37" to type "System.Decimal". Error: "Value was either too large or too small f
or a Decimal."
At line:1 char:12
+ $c_decimal*3 <<<<>
PS> [int64]$c_d ecimal*32513413421341351341341342123124235134
4.7221279915779E+48
PS>
PS> 2*2
4
PS>10*10
100
PS>13*13
169

Here we do some multiplication with both the $c_decimal Type:Decimal and $c Type:UInt64. Good we get the same results eh….
PS> $c_decimal*13
1888071950336
PS> $c*13
1888071950336



OK, so lets so more with [System.Math].

Square Root anyone?
PS> $cmath = [System.Math]::Sqrt($c)
PS> $cmath
380913.354557175
PS>



So, I’m wondering how PS interprets numbers that I enter in. I’m checking out
http://en.wikipedia.org/wiki/Integer_(computer_science)#Common_integral_data_types
in order to figure out more about the different number types.

Check this out, wiki says the high mark of Int64 is +9,223,372,036,854,775,807 and the high mark of Int32 is +4,294,967,295. Lets add a 1,000 over the Int64 high mark.

PS> $num
PS>
PS> $num = 9,223,372,036,854,775,807,000
PS> $num | gm

TypeName: System.Int32
Output Truncated…

PS>


HUH ? That number is larger than a Int64… Why is it being classified as an Int32? Ok, PS doesn’t like the thousands mark comma.

PS> $num
9
223
372
36
854
775
807
0
PS> $num = 9223372036854775807000
PS> $num | gm

TypeName: System.Decimal
Output Truncated…

PS>


Lets get rid of the extra 1,000 and present PS with the highest possible Int64 number, then one higher.

PS> $num
9223372036854775807000
PS>
PS> $num = 9223372036854775807
PS> $num | gm

TypeName: System.Int64
Output Truncated…

PS>
PS>$num = 9223372036854775808
$num | gm

TypeName: System.Decimal
Output Truncated…

PS>

You can specify the type of variable if you want … as long as it fits in the Type you specify….

PS> [double] $num1 = 12345678
PS> $num1
12345678
PS> $num1 | gm

TypeName: System.Double
Output Truncated…

PS>


Normally this would create an Int32 Type variable.

PS> $num2 = 12345678
PS> $num2 | gm

TypeName: System.Int32
Output Truncated…

PS>


Lets create an Int32 variable that breaks the Int32 high limit and see the error.

PS> [Int32] $num32 = 42949672959
Cannot convert value "42949672959" to type "System.Int32". Error: "Value was either too large or too small for an Int32."
At line:1 char:15
+ [Int32] $num32 <<<< = 42949672959
PS>



Need to use PI or e.
PS>
PS> $pi = [math]::Pi
PS> $pi
3.14159265358979
PS> $pi | gm

TypeName: System.Double
Output Truncated…

PS>
PS>
PS> $e = [System.Math]::E
PS> $e
2.71828182845905
PS> $e | gm

TypeName: System.Double
Output Truncated…

PS>

If you have noticed Im using [math] and [System.Math]. They can be used interchangeably. Same with [Convert] and [system.convert]. You probably know that PS is almost never case sensitive.

Thats enough for now. Thanks for playing in the sandbox with me.

Monday, August 10, 2009

HSRP Limitations on 6509’s

HSRP Limitations on 6509’s

Well I had to create some new VLANs on a pair of 6509’s with dual Sup 2’s with MSFC2’s. These 6509’s had a few dozen VLANS already configured and lots of the VLANS had HSRP configured.

So I created the new VLANs and then the VLAN interfaces on the MSFC. I added the IP Address and then went to setup the HSRP config and received an error.


MSFC-A-PRI(config)# int vlan229
MSFC-A-PRI(config-if)# description VLAN for Database Servers
MSFC-A-PRI(config-if)# ip address 172.25.229.2 255.255.255.0
MSFC-A-PRI(config-if)# ip access-group vlan229 in
MSFC-A-PRI(config-if)# standby 229 ip 172.25.229.1
More than 16 standby groups not supported in this platform.


WTF?

I then realized I was running into this limitation.

I had previously configured each VLAN # with the same standby group #. See here…

MSFC-A-PRI#sh runn int vlan165
Building configuration...

Current configuration : 321 bytes
!
interface Vlan165
description Wireless VLAN , SSID:public-access
ip address 172.25.165.2 255.255.255.0
ip access-group vlan165 in
ip helper-address 172.25.224.255
no ip redirects
no ip unreachables
ip route-cache policy
ip policy route-map 165-vlan
standby 165 ip 172.25.165.1
standby 165 priority 110
standby 165 preempt
standby 165 authentication hsrppass
end

MSFC-A-PRI#sh runn int vlan166
Building configuration...

Current configuration : 304 bytes
!
interface Vlan166
description Wireless VLAN , SSID:guest-internet
ip address 172.25.166.4 255.255.255.0
ip access-group vlan166 in
ip helper-address 172.25.224.255
no ip redirects
no ip unreachables
ip route-cache policy
ip policy route-map 166-vlan
standby 166 ip 172.25.166.1
standby 166 priority 108
standby 166 authentication hsrppass
end

MSFC-A-PRI#
MSFC-A-PRI# sh standby
….output truncated….
Vlan165 - Group 165
Local state is Active, priority 110, may preempt
Hellotime 3 sec, holdtime 10 sec
Next hello sent in 2.088
Virtual IP address is 172.25.165.1 configured
Active router is local
Standby router is 172.25.165.3 expires in 7.584
Virtual mac address is 0000.0c07.aca5
Authentication text "hsrppass"
1 state changes, last state change 36w5d
IP redundancy name is "hsrp-Vl165-165" (default)
Vlan166 - Group 166
Local state is Listen, priority 108
Hellotime 3 sec, holdtime 10 sec
Virtual IP address is 172.25.166.1 configured
Active router is 172.25.166.2, priority 110 expires in 8.616
Standby router is 172.25.166.3 expires in 7.836
Authentication text "hsrppass"
0 state changes, last state change never
IP redundancy name is "hsrp-Vl166-166" (default)

See how the standby groups are the same integer as the interface number. This is preferred, however 6509’s with a Sup2\ PFC2 is limited to 16 HSRP groups. Apparently the 3550 is also limited in this way. Most other 6500 Supervisors are limited to 256 groups which is nicer than the 16 that we are here.

3550 limitation link


Luckily I only have to deal with this on one platform, today… 

So here is how I’ll be changing the config on all 4 of the MSFC’s. In the new config all the interfaces will share the same standby Group number where before each interface had its own.


!
! MSFC-A-PRI
!
interface Vlan165
no standby 165 ip 172.25.165.1
no standby 165 priority 110
no standby 165 preempt
no standby 165 authentication hsrppass
standby 10 ip 172.25.165.1
standby 10 priority 110
standby 10 preempt
standby 10 authentication hsrppass
!
! MSFC-A-SEC
!
interface Vlan165
no standby 165 ip 172.25.165.1
no standby 165 priority 109
no standby 165 authentication hsrppass
standby 10 ip 172.25.165.1
standby 10 priority 109
standby 10 authentication hsrppass
!
! MSFC-B-PRI
!
interface Vlan165
no standby 165 ip 172.25.165.1
no standby 165 priority 108
no standby 165 authentication hsrppass
standby 10 ip 172.25.165.1
standby 10 priority 108
standby 10 authentication hsrppass
!
! MSFC-B-SEC
!
interface Vlan165
no standby 165 ip 172.25.165.1
no standby 165 priority 107
no standby 165 authentication hsrppass
standby 10 ip 172.25.165.1
standby 10 priority 107
standby 10 authentication hsrppass
!

Now that I’ve reconfig’ed the 2 interfaces (actually I’ve grouped as many as 10 VLANs into a single HSRP Group) I have the ability to create more VLAN interfaces and configure them for HSRP.

See the changes to the HSRP group numbers. The two VLAN interfaces are sharing the same HSRP Group Number , 10.

MSFC-A-PRI#
MSFC-A-PRI#sh standby vlan 165
Vlan165 - Group 10
Local state is Active, priority 110, may preempt
Hellotime 3 sec, holdtime 10 sec
Next hello sent in 2.220
Virtual IP address is 172.25.165.1 configured
Active router is local
Standby router is 172.25.165.3 expires in 8.636
Virtual mac address is 0000.0c07.ac0a
Authentication text "hsrppass"
2 state changes, last state change 02:33:12
IP redundancy name is "hsrp-Vl165-10" (default)
MSFC-A-PRI#sh standby vlan 166
Vlan166 - Group 10
Local state is Listen, priority 108
Hellotime 3 sec, holdtime 10 sec
Virtual IP address is 172.25.166.1 configured
Active router is 172.25.166.2, priority 110 expires in 8.268
Standby router is 172.25.166.3 expires in 8.836
Authentication text "hsrppass"
5 state changes, last state change 02:31:12
IP redundancy name is "hsrp-Vl166-10" (default)
MSFC-A-PRI#
MSFC-A-PRI#


There are lots of caveats listed in the links that may affect you, me, or our friend. One that could affect me, doesn’t on this router but may on another is having multiple HSRP processes running on a router.

Q. Can I only configure a total of 16 HSRP VLAN interfaces or 16 HSRP processes in the Supervisor Engine 2-based system?
A. No. You can use the 16 unique group IDs on as many interfaces as you like. 16 HSRP groups does not mean that you can have only 16 HSRP processes or 16 VLAN interfaces with HSRP enabled. The only caveat is that you can only define up to 16 HSRP processes per interface. However, it is very unlikely that you would need more than 16 HSRP processes per interface in a well-designed network.


On some of our routers we have configured secondary interfaces….. eg….

3845_Access1#sh runn interface FastEthernet2/0.80
Building configuration...

Current configuration : 281 bytes
!
interface FastEthernet2/0.80
description VLAN Link for Outside-Internet
encapsulation dot1Q 80
ip address 1.2.4.4 255.255.255.0 secondary
ip address 1.2.3.4 255.255.255.0
standby 80 ip 1.2.3.1
standby 80 ip 1.2.4.1 secondary
standby 80 priority 210
end

3845_Access1#

Ok, oops, never mind. I am using the same HSRP process for multiple IP addresses that are assigned to the interface. Doesn’t matter anyway, this router is a, , router. I don’t think it has this limitation that the 6509 and 3550, eh, , switches have. If this had been on a 6509 it would be safe(r) since both IP ranges are running on the same HSRP process.

Some other things to note….

Q. What is the implication of the use of the same HSRP group ID on multiple interfaces?
A. When you define the same HSRP group ID on multiple interfaces, they all share the same HSRP virtual MAC address. In most modern LAN switches, there are no issues because they maintain a per-VLAN MAC address table. However, if your network contains any third party switches which maintain a system-wide MAC address table regardless of VLAN, you may experience problems. If VLANs are not specified to a HSRP group, the VLANs default to Group 0.

Q. What does the HSRP MAC address look like?
A. The HSRP MAC address is derived from the group number, and looks like this:
0000.0c07.acXX
XX is the HSRP group number.


Captain says, Route Responsibly

Tuesday, August 4, 2009

Timo codes to use Powershell for sending SMTP with AUTH

Timo codes to use Powershell for sending SMTP with AUTH

I needed a way to send SMTP email using Powershell but my SMTP server requires authentication. Sending email to a SMTP server that requires authentication isn’t much harder than sending email to a server that doesn’t. We basically call 2 .NET objects instead of one. Of course we need to set some properties for each .NET object.

The 2 .NET objects are
System.Net.NetworkCredential
System.Net.Mail.SmtpClient

Here is the PS code with comments.

# Set the SMTP server, this needs to be done before you can call the .NET object
$smtpServer = "smtpserver.yourdomain.net"

# Call the .NET object for sending email System.Net.Mail.SmtpClient
# http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
# not too helpful eh... it'd be nice if there were some PS examples.
# instead you can type this into a PS prompt $smtp | gm and at least get the methods and properties
$smtp = new-object System.Net.Mail.SmtpClient($smtpServer)

# Call the .NET object that will turn on Authentication functionality System.Net.NetworkCredential
$smtpauth = new-object System.Net.NetworkCredential

# set the properties of the .NET.NetworkCredential object
# see the available properties, methods etc of this object by
# type this at the PS prompt $smtpauth | gm
$smtpauth.UserName = "username"
$smtpauth.Password = "password"

# Here we are associating the .NET.NetworkCredential object and it's properties with
# the other .NET object we are using System.Net.Mail.SmtpClient.
# .Credentials is a property of the .NET object System.Net.Mail.SmtpClient
# Here we are associating the variable $smtpauth with the Properties we set
$smtp.Credentials = $smtpauth

# Lets set the From, To, Subject and Body in variables for scalability
$smtpFrom = "powershell-script@yourdomain.net"
$smtpTo = "timo@yourdomain.net"
$smtpSubject = "Powershell Email Test"
$smtpBody = "This is just some junk for the body of the email"

# Here we call the method .Send from System.Net.Mail.SmtpClient and fill in its parameters.
# You can see all the methods & properties by hammering this into the keyboard $smtp | gm
$smtp.Send($smtpFrom, $smtpTo, $smtpSubject, $smtpBody)

Tuesday, February 13, 2007

Enough with Anna Nicole Smith

So I have heard enough about Anna Nicole Smith.

1. She was a whore.
2. She married a 90 man for $$
3. He died, she ate and became a fat whale.
4. She took speed to slim down
5. Her son died
6. She died.

End of story. END. END.

Jeez, its been several days and its still the biggest thing on the news, CNN is still talking about it on TV. This morning on Good Morning America they had this big bumbling idiot Howard K Stern, again WHO CARES!!!


NO I DO NOT SEE RESEMBLANCE TO MARILYN MONROE EITHER.
Anna Nicole was a whore, Marilyn was a movie star, actress, calendar girl. Oh yea, they were both blond too so maybe there is a resemblance...???...

Monday, February 12, 2007

FIOS TV

Ive had my FIOS Internet service for about a year and a half now. Its been fine since day one. I was so glad to drop Comcrap (Comcast) .

Now I just got Verizon FIOS TV!! Its awesome. I have several more SD and more HD channels than I had with Comcrap. I do have to rent more cable boxes now but Im not too concerned with that because my cable bill is about $20 less than it was before. I was paying $115 for Basic Digital Cable, HBO, 1 HD box, 1SD box. Thats right $115. With Verizon TV I have Basic Digital (lots more channels) , 3HD boxes and 1 SD box and my bill is $89. Easy choice.


Check out how Comcrap is screwing their HSI Internet customers. You better not be a heavy user. Check this here

Great US Government news from this week.

Over the past week or so I have read several items that have bugged me about the current government. Most seem like decisions that just meet the "common sense" criteria. Lets check em out here.

1. US Sends pallets of cash to Baghdad.
Who the fuck sends tons \ billions of US dollars into a war zone. I know Iraq is a cash economy but come on. How much waste can we account for?

Heres a great example of this mess.
Money also disappeared in truckloads and by helicopter. The CPA reportedly distributed funds to contractors in bags off the back of a truck. In one notorious incident in April 2004, $1.5 billion in cash that had just been delivered by three Blackhawk helicopters was handed over to a courier in Erbil, in the Kurdish region, never to be seen again. Afterwards, no one was able to recall the courier’s name or provide a good description of him.

$1.5 billion dollars ??? WTF??? Accountability ?

2. Pentagon Manipulates Intelligence Data (pdf link)
Next I hear that the Pentagon manipulated the data GWB used to tell the Americans we needed to go to war with Iraq. The Acting Inspector General Thomas F. Gimble wimps out and says that the Pentagon used "Inappropriate but not Illegal" data. I guess everyone in the Inspector General's office and in the US government forgets about accountability.

Saturday, January 27, 2007

01-27-2007 Bush wants gas usage cut by 20%

http://www.cnn.com/2007/POLITICS/01/23/bush.sotu/index.html

or here

WASHINGTON (CNN) -- President Bush, in Tuesday's State of the Union address, will propose a plan to cut U.S. gasoline consumption by 20 percent while bolstering inventory in the Strategic Petroleum Reserve, Republican sources say.
The president's plan to cut gasoline use includes tightening fuel economy standards on automakers and relying on alternative energy sources, such as hybrid cars, the sources say.


TIMO: Yea, how about you reduce the work week to 4 days. See its easy, that cuts my gas usage 20%.
While driving to work this morning , I saw a car that has a GW Bush \ Cheney bumper sticker on the back. I wanted to stop and ask the person what the fuck was wrong with them. Who in their right mind would drive around with a GWB sticker.