Saturday, June 19, 2010

Some good links to Powershell

Good reading
http://www.techotopia.com/index.php/Using_COM_with_Windows_PowerShell

http://www.youdidwhatwithtsql.com/automating-internet-explorer-with-powershell/467
http://www.pvle.be/2009/06/web-ui-automationtest-using-powershell/
http://powershell.com/Mastering-PowerShell.pdf

Active Directory
http://blogs.technet.com/b/benp/archive/2007/04/10/creating-an-ad-shell.aspx
http://blogs.technet.com/b/benp/archive/2007/03/26/searching-the-active-directory-with-powershell.aspx

http://blogs.technet.com/b/benp/archive/2007/03/05/benp-s-basic-guide-to-managing-active-directory-objects-with-powershell.aspx

Good book to download
http://blogs.technet.com/b/chitpro-de/archive/2008/02/28/free-windows-powershell-workbook-server-administration.aspx

RAM Count : Poweshell

RAM in MB
$mem = Get-WmiObject -Class Win32_ComputerSystem
# Display memory
"This system has {0} MB Free Memory" -f $($mem.TotalPhysicalMemory/1mb)
Processor cOUNT
$system = Get-WmiObject -Class Win32_ComputerSystem

Non Working devices
Get-WmiObject Win32_PNPEntity | where {$_.ConfigManagerErrorcode -ne 0}

Friday, June 18, 2010

Determine whether a computer is a tower, a mini-tower, a laptop, and so on: Powershell

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
For Each objItem in objChassis.ChassisTypes
Wscript.Echo "Chassis Type: " & objItem
Next
Next

Right way to read a file in itration. Powershell

Foreach ($name in (Get-Content c:\names.txt))
{
# do something with $name
}

Service Pack in OS. Powershell

Get-WmiObject Win32_OperatingSystem |
Format-List BuildNumber,CSName,ServicePackMajorVersion

UPTIME of server in powershell

UPTIME
$myobj.uptime = (Get-Date) - [System.DateTime]::ParseExact($OSInfo.LastBootUpTime.Split(".")[0],'yyyyMMddHHmmss',$null)
$myobj.uptime = "$($myobj.uptime.Days) days, $($myobj.uptime.Hours) hours," +`
" $($myobj.uptime.Minutes) minutes, $($myobj.uptime.Seconds) seconds"

Adding data to database with Powershell

DaTA base: Add data
$ds = new-object System.Data.DataSet
$ds.Tables.Add("person")
[void]$ds.Tables["person"].Columns.Add("name",[string])
[void]$ds.Tables["person"].Columns.Add("last",[string])
$dr = $ds.Tables["person"].NewRow()
$dr["name"] = "ullas"
$dr["last"] = "t"
$ds.Tables["person"].Rows.Add($dr)
$dr

name last
---- ----
ullas t

Try Catch template for Powershell

Try Catch template
try
{
try
{
# Command that uses -erroraction stop or $erroractionpreference = 'Stop'
}
# Here we catch the Exception generated by the ErrorAction "Stop"
# Only necessary if there is any processing we want to do if the
# exception is of type ActionPreferenceStopExecution,
# otherwise this block can be deleted
catch [System.Management.Automation.ActionPreferenceStopException]
{
# Error Handling specific to ActionPreferenceStopException goes here
# Rethrow the "real" exception as a terminating error
Throw $_.exception
}
# All errors are caught and rethrown to the outer try/catch block
# as terminating errors to be handled.
catch
{
Throw $_.exception
}
}
# Here we can resume exception type handling as usual
catch #[System.Management.Automation.ItemNotFoundException]
{
"Got it!"
}

WMI Info on Excel

$strComputer = “.”

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = “Computer”
$Sheet.Cells.Item(1,2) = “Drive Letter”
$Sheet.Cells.Item(1,3) = “Description”
$Sheet.Cells.Item(1,4) = “FileSystem”
$Sheet.Cells.Item(1,5) = “Size in GB”
$Sheet.Cells.Item(1,6) = “Free Space in GB”

$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True

$intRow = 2
$colItems = Get-wmiObject -class “Win32_LogicalDisk” -namespace “root\CIMV2? `
-computername $strComputer

foreach ($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1) = $objItem.SystemName
$Sheet.Cells.Item($intRow,2) = $objItem.DeviceID
$Sheet.Cells.Item($intRow,3) = $objItem.Description
$Sheet.Cells.Item($intRow,4) = $objItem.FileSystem
$Sheet.Cells.Item($intRow,5) = $objItem.Size / 1GB
$Sheet.Cells.Item($intRow,6) = $objItem.FreeSpace / 1GB

$intRow = $intRow + 1

}
$WorkBook.EntireColumn.AutoFit()
Clear