Posts Tagged wmi
Determining System Uptime
Posted by admin in Powershell on October 28, 2009
Using WMI to determine when a system last booted up
1 2 | $a = (get-wmiobject win32_operatingsystem) $a.ConvertToDateTime($a.lastbootuptime) |
Terminate Process
Posted by admin in Powershell on October 11, 2009
While wanting to kill a process, i decided why use the task manager when i can use powershell. I will be using wmi and the Win32_Process class for this task.
First i look for the process name i would like to terminate:
1 | gwmi win32_process | select-object name |
I then select the process name from this list to confirm this is the process i want to terminate:
1 | gwmi win32_process | where {$_.name -eq "AdobeUpdater.exe"} |
Then i terminate it:
1 | (gwmi win32_process | where {$_.name -eq "AdobeUpdater.exe"}).terminate() |
We are looking for a return value of 0 indicating this was successfull:
__GENUS : 2
__CLASS : __PARAMETERS
__SUPERCLASS :
__DYNASTY : __PARAMETERS
__RELPATH :
__PROPERTY_COUNT : 1
__DERIVATION : {}
__SERVER :
__NAMESPACE :
__PATH :
ReturnValue : 0For other return values, visit MSDN for more information.
Determining Client OS Version
Posted by admin in Powershell on October 5, 2009
Using powershell to query the registry, either local or remote, to determine the OS version being used:
1 2 3 4 | $MachineName = $Args[0]
$regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $MachineName)
$regKey= $regKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" ,$False)
$regkey.getvalue('ProductName') |
Or by using WMI and also discovering whether the client is 32 or 64 bit:
1 | gwmi Win32_OperatingSystem -computer "." | select-object Caption , OSArchitecture |
Export to Excel – Ping Status
Posted by admin in Cisco, Powershell on September 30, 2009
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() |
Powershell Ping
Posted by admin in Powershell on September 26, 2009
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
} |
Get Logged On User
Posted by admin in Powershell on September 21, 2009
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