Converting Epoch Time With Powershell

Converting Epoch Time to Human Readable Time

1
2
Function get-epochdate ($epochdate) { [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($epochdate)) }
get-epochdate 1295113860

No Comments

Determining System Uptime

Using WMI to determine when a system last booted up

1
2
$a = (get-wmiobject win32_operatingsystem)
$a.ConvertToDateTime($a.lastbootuptime)

,

No Comments

Recursive Listing of Files by Date


Using powershell to recursively list files modified before or after a certain date is an easy one line powershell script.

1
get-childitem -Recurse |where {$_.lastwritetime -gt (date).adddays(-10)}

or to list by a certain date:

1
 get-childitem -Recurse |where {$_.lastwritetime -gt "10/2/09"}

,

No Comments

Windows 7 Powershell Pack


The Windows 7 Powershell Pack has been released by Microsoft. A brief feature list as well as the download can be found at the following link:

Windows 7 Resource Kit Powershell Pack

, ,

No Comments

Change File Modified Date

Using Powewrshell Set_LastWriteTime, we are able to modify a files last modifed date and time.

1
2
$date = date
(get-item c:\temp\list.txt).set_LastWriteTime($date)

To adjust the date to a day in the past just change the $date line:

1
2
$date = (date).AddDays(-10)
(get-item c:\temp\list.txt).set_LastWriteTime($date)

This just moved the date back 10 days from today. To go forward you would do:

1
2
$date = (date).AddDays(10)
(get-item c:\temp\list.txt).set_LastWriteTime($date)

,

No Comments

Terminate Process

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      : 0

For other return values, visit MSDN for more information.

, ,

No Comments

Setting SMTP Message Priority in Powershell


To expand on my previous post regarding how to send email from Powershell, below is an example that includes forcing plain text (or html), setting mesage priority, setting notification options, as well as adding CC: and BCC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Setting SMTP Priority using Powershell V1
# Yattaa.com 10/07/2009
#
$email = New-Object System.Net.Mail.MailMessage
$email.From = "user@yourdomain.com"
$email.To.Add("user@yourdomain.com")
$email.To.Add("user@yourdomain.com")
$email.CC.Add("user@yourdomain.com")
$email.BCC.Add("user@yourdomain.com")
$email.DeliveryNotificationOptions = [System.Net.Mail.DeliveryNotificationOptions]::OnSuccess
$email.IsBodyHtml = $false
$email.Priority = [System.Net.Mail.MailPriority]::High
$email.Subject = "Subect Here"
$email.Body = "Body Here"
$smtp = New-Object System.Net.Mail.SmtpClient
$smtp.Host = "SMTP Server Here"
$smtp.Send($email)

, ,

No Comments

Determining Client OS Version

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

, ,

No Comments

Executing Unsigned Scripts

By default powershell does not run unsigned scripts. To let powershell run unsigned scripts:

1
Set-ExecutionPolicy unrestricted

No Comments

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