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) |
Recursive Listing of Files by Date
Posted by admin in Powershell on October 18, 2009
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"} |
Windows 7 Powershell Pack
Posted by admin in Powershell on October 15, 2009
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:
Change File Modified Date
Posted by admin in Powershell on October 14, 2009
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) |
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.
Setting SMTP Message Priority in Powershell
Posted by admin in Powershell on October 8, 2009
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) |
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 |
Executing Unsigned Scripts
Posted by admin in Powershell on October 2, 2009
By default powershell does not run unsigned scripts. To let powershell run unsigned scripts:
1 | Set-ExecutionPolicy unrestricted |
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
} |