Archive for September, 2009

Export to Excel – Ping Status

Here is a modified version of my previous post. This takes Win32_PingStatus, pings a subnet, and writes the output to Excel. All using Powershell.

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
#
# Powershell: Export Ping Status to Excel
# Exports pings of address range to excel
# Yattaa.com 9/28/09
$erroractionpreference = "SilentlyContinue"
 
$a = New-Object -comobject Excel.Application
$a.visible = $True 
 
$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)
 
$c.Cells.Item(1,1) = "Host"
$c.Cells.Item(1,2) = "Response"
$c.Cells.Item(1,3) = "Return Time"
 
($c.UsedRange).Interior.ColorIndex = 36
($c.UsedRange).Font.ColorIndex = 1
($c.UsedRange).Font.Bold = $True
 
$intRow = 2
 
$i = 0
while ($i -lt 255){
$i += 1
$a = get-WmiObject "Win32_PingStatus" -filter "Address='192.168.1.$i' and Timeout=100"
Write-Host $a.Address, $a.statuscode
	$c.Cells.Item($intRow,1) = $a.Address
	$c.Cells.Item($intRow,2) = $a.StatusCode
 
	If($a.StatusCode -eq 0){
		$c.Cells.Item($intRow,2).Interior.ColorIndex = 4
		$c.Cells.Item($intRow,3) = $a.ResponseTimeToLive
	}
	Else{
		$c.Cells.Item($intRow,2).Interior.ColorIndex = 3
	}
 
$intRow = $intRow + 1
}
($c.UsedRange).EntireColumn.AutoFit()

, ,

No Comments

Powershell Ping

This is a basic powershell script for pinging a subnet to see which hosts respond.  Status 0 indicates the host responds while status 11003 indicates that no host is responding on that address.  Over the next couple of days i will be expanding on this script to export the results to excel and add more user variables

1
2
3
4
5
6
$i = 0
while ($i -lt 255){
   $i += 1
   $a = get-WmiObject Win32_PingStatus -f "Address='192.168.1.$i'"
   Write-Host $a.Address, $a.statuscode
   }

,

No Comments

Test Path

Test-Path is used to determine if a file/folder/registry key etc… exists and returns a value of True or False

PS C:\>test-path hklm:\software\microsoft\windows\currentversion\run
True

PS C:\> test-path c:\windows\
True

PS C:\> test-path c:\windows\system31
False

PS C:\> test-path c:\autoexec.bat
True

1
2
3
4
5
6
7
if (Test-Path c:\temp){
write-host "folder exists"
}
else {
mkdir c:\temp
write-host "folder created"
}

No Comments

Get Logged On User

A couple of different ways to get the currently logged on user using powershell

(gwmi -class win32_computersystem).username

gwmi -class win32_computersystem | select-object username

To query on a specific computer:

(gwmi -class win32_computersystem -computer ".").username

gwmi -class win32_computersystem -computer "." | select-object username

,

No Comments

sending email in powershell

Version 1:

$emailFrom = “user@domain.com”
$emailTo = “user@domain.com”
$subject = “subject”
$body = “body”
$smtpServer = “smtp server”
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

Version 2:

PS C:\Windows\system32> get-help send-mailmessage

NAME
    Send-MailMessage

SYNOPSIS
    Sends an e-mail message.
SYNTAX
    Send-MailMessage [-To] <string[]> [-Subject] <string> -From <string> [[-Body] <string>] [[-SmtpServer] <string>] [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Cc <string[]>] [-Credential <PSCredential>] [-DeliveryNotificationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Priority {Normal | Low | High}] [-UseSsl] [<CommonParameters>]
DESCRIPTION
    The Send-MailMessage cmdlet sends an e-mail message from within Windows PowerShell.
RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=135256

REMARKS
    To see the examples, type: “get-help Send-MailMessage -examples”.
    For more information, type: “get-help Send-MailMessage -detailed”.
    For technical information, type: “get-help Send-MailMessage -full”.

, ,

No Comments

Powershell V2

get-hotfix returns the installed hotfixes on local or remote computers

PS C:\Windows\system32> get-help get-hotfix

NAME
Get-HotFix

SYNOPSIS
Gets the hotfixes that have been applied to the local and remote computers.

SYNTAX
Get-HotFix [[-Id] ] [-ComputerName ] [-Credential ] []

Get-HotFix [-Description ] [-ComputerName ] [-Credential ] []

DESCRIPTION
The Get-Hotfix cmdlet gets the hotfixes that have been applied to the local computer or to remote computers by Component-Based Servicing.

RELATED LINKS
Online version: http://go.microsoft.com/fwlink/?LinkID=135217
Get-ComputerRestorePoint

REMARKS
To see the examples, type: “get-help Get-HotFix -examples”.
For more information, type: “get-help Get-HotFix -detailed”.
For technical information, type: “get-help Get-HotFix -full”.

No Comments

Reboot

utils system restart

No Comments