Sunday, October 17, 2010

Powershell - Get windows service status on a remote computer

Command to get the remote server service status through powershell is listed below

Get-service  -computername "Servername" -displayname "servicename"

Saturday, October 16, 2010

Powershell to get server uptime

I dont remember from which site i got the below script but it is very useful in my day to day admin life

$wmi=Get-WmiObject -class Win32_OperatingSystem -computer "Computername"
$LBTime=$wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
Write-host $server “Uptime: ” $uptime.days “Days” $uptime.hours “Hours” $uptime.minutes “Minutes” $uptime.seconds “Seconds”

Get-WmiObject -class Win32_OperatingSystem
This will invoke the Operating System wmi object


ConvertToDateTime
Will convert the output to human readable form


[TimeSpan]$uptime=New-TimeSpan $LBTime $(get-date)
This will compare the output time with the current time and store the result to the string as object

Powershell - Search a string inside a text file

Get-Content cmd let will allow reading content from a text file and the following piped cmdlet can be used to search a string inside the text file given as input


(Get-Content "Full path of the text file") | ? {$_ -match 'searchstring''}

Uninstalling Previous Versions of PowerShell

Uninstalling Previous Versions of PowerShell
PowerShell v1 RTM shipped as updates to the OS. Therefore, it will show up only in the updates section. To look for previous versions, do the following:
 On Windows XP/Server 2003:
·         Go to Add/Remove Programs
·         Select Show Updates
·         Look for any of the following KB numbers which indicate earlier versions of PowerShell
o   KB926139 – Windows PowerShell v1.0 RTM – English Language Version
o   KB926140 – Windows PowerShell v1.0 RTM – Localized Installation Package
o   KB926141 – Windows PowerShell v1.0 RTM – MUI pack
o   KB925228 – Windows PowerShell v1.0 RC2
·         Versions of V1 prior to RC2 are MSI based installations and they will appear as Windows PowerShell in the programs list
·         Uninstall the previous version that's installed
On Windows Vista:
·         Go to Programs and Features
·         Click on View Installed Updates
·         Look for any of the following KB numbers which indicate earlier versions of PowerShell
o   KB928439 – Windows PowerShell 1.0 RTM
o   KB923569 – Windows PowerShell 1.0 RC2
·         Version of V1 prior to RC2 are MSI based installations and they will appear as Windows PowerShell in the installed programs list
·         Uninstall the previous version that's installed
On Windows Server 2008:
·         Launch Server Manager
·         Go to Features
·         Click on Uninstall Features. This brings up a list of installed features to choose from
·         Select Windows PowerShell and uninstall