Thursday, March 5, 2009

I've been up for days!!

When was that machine last rebooted?


function Get-Uptime {
param([string]$computer = $env:computername)
$computer = $computer.toupper()
if (Test-Connection $computer -Count 1 -ea silentlycontinue) {
    $WMITimeString = (gwmi -Query "Select lastbootuptime from win32_operatingsystem" -computer $computer).lastbootuptime
    $bootTime = ([wmi]'').ConvertToDateTime($WMITimeString)
    $formattedTime = (Get-Date $bootTime).datetime
    Write-Host -fore green -Background black "Computer $computer has been up since:`n`n`t $formattedTime"
    }else {Write-Host -fore red -Background black "Computer $computer is not online."}
}

The script will check the local machine if no parameter is specified.

Tuesday, February 10, 2009

The new Ping from PS CTP3

Lately, because of my heavy work load I've been unable to write much about the CTP3 release. However there has been a lot of writings out there to more then quench your PoSh thirst. I'll try to do my part and get back on track with my post. 8)

There are a lot of things added to the CTP3 of Powershell, which I'll try write about over the next few weeks. Today I'll talk about one of the new cmdlets that really hasn't received much attention. I feel it's probably one of the most useful as a Sys Admin. The 'Test-Connection' cmdlet. It has some really useful parameters that can save you time before you try to execute some commands on that workstation that is powered off, or ping from a computer that is not your own.

The first useful parameter for the Test-Connection cmdlet is 'Count'. Not too surprising, what the count parameter does is determine how many ICMP requests to send to the destination address. Very useful if you want to only send 1 or 2 requests to the computer to check it's alive. If the count parameter is not specified a default of 4 request are sent.


Sjdelatorre@JDELATORRE {~} Test-Connection jdelatorrevm -Count 10

Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
JDELATORRE jdelatorrevm 192.168.249.82 32 5
JDELATORRE jdelatorrevm 192.168.249.82 32 81
JDELATORRE jdelatorrevm 192.168.249.82 32 1
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 22
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 4


Sjdelatorre@JDELATORRE {~}


The 'Delay' parameter is another very useful parameter to cut down on the amount of traffic sent over the wire. I use this when rebooting a server and checking to see when it's back up, but instead of consistently pinging the server every second as with Ping.exe -t, you can specify how far apart you want those request. By default the delay is 1 second.

Here I'm pinging my virtual machine 4 times with 10 seconds between each ping request.


Sjdelatorre@JDELATORRE {~} Test-Connection jdelatorrevm -Delay 10

Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 0
JDELATORRE jdelatorrevm 192.168.249.82 32 0


Sjdelatorre@JDELATORRE {~}


Probably the most interesting and sometimes most helpful is the 'Source' parameter. With the source parameter you can specify where your ping request originate from. So it's possible to test the connection speeds to other parts of your network from a server when typing the command at your computer. Here, I'm pinging my laptop but the requests are coming from my VM.


Sjdelatorre@JDELATORRE {~} Test-Connection -Source jdelatorrevm -Destination jdelatorre

Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
JDELATORREVM jdelatorre 192.168.249.54 192.168.249.54 32 0
JDELATORREVM jdelatorre 192.168.249.54 192.168.249.54 32 0
JDELATORREVM jdelatorre 192.168.249.54 192.168.249.54 32 0
JDELATORREVM jdelatorre 192.168.249.54 192.168.249.54 32 0


Sjdelatorre@JDELATORRE {~}

Notice the Source column.


There's quite a bit more to the Test-Connection cmdlet than what I've posted here, such as:

  • AsJob
  • Authentication
  • BufferSize
  • Credential
  • Impersonation
  • ThrottleLimit
  • TimeToLive
I've also created an alias to test-connection called 'ping' with the following command inside of my profile.

set-alias -name ping -value test-connection

So now you can call the Test-Connection cmdlet with just ping

ping jdelatorrevm

As a note. The data that is sent/returned is plain ol' WMI.



Wednesday, November 12, 2008

A MS SQL Form from PS

My custom form for quick MSSQL Queries via PS.























Thursday, November 6, 2008

PrimalForms and Lee Holmes Connect to Script

If you've used Lee Holmes' (a developer on the Windows PowerShell team) Connect to PS script you'll know it's pretty useful when connecting to a remote port from the PS console.

So with the recent release of Sapien's new free tool, PrimalForms, this gave me an excuse to convert Lee's great script to a GUI interface. PrimalForms did an awesome job in creating the GUI skeleton, so all I had to do was tie everything together.

Thanks goes out to Lee and the people at Sapien!!





















Monday, November 3, 2008

PrimalScript Releases Free WinForms Tool!

The good people at Sapien have released their free WinForms tool for Powershell today.

You can find it here.

Once I have some time I'll post back on some tips or maybe a WinForm Script of my own. 8)

Thursday, October 16, 2008

Idera PowerShell Plus WMI Intellisense Fix

I was having a problem with Idera's PowerShell Plus intellisense with WMI Classes. Only a few Win32 Classes would be recognized. In order to rebuild the WMI classes and have them recached into PowerShell plus you have to do a few things.

  • When the splash screen of PS+ starts be sure to press the right 'Shift' key. Pressing the left 'Shift' key wouldn't work for me.
  • Check the box to 'Delete cached WMI info'. This check box tells PS+ to rebuild the WMI Class Intellisense, similar to what happens when you first install PS+
  • After PS+ starts, the intellisense works as expected.


Custom IP Object

I recently needed a function that would get the 3rd octet from a group of computers to determine what subnet that computer was on. (Class C)

I had a VBScript that could do this but didn't have anything in PoSh. I decided the best way to do this was to create a custom object to easily get to the octet I needed. Doing it with a PS object provides the ability to select which octet you wanted back. Here is the function.


   1:  function get-ip {
   2:      param ([string]$computer = '.')
   3:      if ($computer -eq '.') {$computer = $env:computername}
   4:      
   5:      # Create custom object
   6:      $objIP = New-Object -typeName 'psobject'
   7:      
   8:      # Connect to WMI and retrieve IP from NetworkAdapterConfiguration Class
   9:      [string]$IP = (gwmi win32_networkadapterconfiguration -computerName $computer | ? { $_.IPEnabled -eq $true }).IPAddress
  10:      
  11:      # Split on the '.' to get each octet
  12:      $octets = $IP.split(".")
  13:  
  14:      # Add octets,ip and computername to the custom object
  15:      Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "1stOct" -value $octets[0]
  16:      Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "2ndOct" -value $octets[1]
  17:      Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "3rdOct" -value $octets[2]
  18:      Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "4thOct" -value $octets[3]
  19:      Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "FullIPv4" -value $ip
  20:      Add-Member -inputObject $objIP -memberType 'Noteproperty' -name "ComputerName" -value $computer
  21:  
  22:      return $objIP
  23:  }


So now you can get whatever octet by using select-object. The function can take an argument named Computer so you can retrieve remote computer addresses/octets as well.


Sjdelatorre@JDELATORRE {~} get-ip

1stOct : 192
2ndOct : 168
3rdOct : 251
4thOct : 46
FullIPv4 : 192.168.251.46
ComputerName : JDELATORRE

Sjdelatorre@JDELATORRE {~} get-ip | select 3rdoct

3rdOct
------
251

Sjdelatorre@JDELATORRE {~} get-ip | select 3rdoct,fullipv4,computername

3rdOct FullIPv4 ComputerName
------ -------- ------------
251 192.168.251.46 JDELATORRE

Sjdelatorre@JDELATORRE {~} get-ip -computer jdelatorreVPC | select 3rdoct,fullipv4,computername

3rdOct FullIPv4 ComputerName
------ -------- ------------
251 192.168.251.67 jdelatorreVPC

Sjdelatorre@JDELATORRE {~}



Monday, October 13, 2008

Codename Windows 7 is inroducted as Windows 7

The official name for the new Windows OS codename "Windows 7" is exactly that, Window 7.

Hmmm.... That was easy.

Friday, September 5, 2008

My FF Team

How do I measure up against you??

Wednesday, August 13, 2008