Capture & Send System Info by Email in Windows Powershell

Powershell is Microsoft’s version of shell-scripting in *nix systems. Many years back, VBScript was the only language available to automate various tasks, but it was of limited use. Then they introduced Windows Management Instrumentation (WMI) and that made scripting much more practical and powerful. Powershell continues in that direction and offers powerful features and the ability to do almost any administration or automation task in Windows.

Below is a script which is very useful if you want to get the details of a system and send it off by email . The script works for the local system only but it can be changed to work with a remotely-accessible system . The mailing part will need the relevant mail server,  port and authentication details.

function sysinfo {
    $output = ""
	$machine = "."
	$compInfo = Get-wmiobject win32_computersystem -comp $machine
	$output += "COMPUTER INFO `r`n"
	$output += "===============`r`n"
	
	$output += "Name :" + $compinfo.name + "`r`n"
	$output += "Domain :" + $compinfo.domain + "`r`n"
	$output += "Model :" + $compinfo.model + "`r`n"
	$output += "RAM :" + "{0:n2} GB" -f ($compinfo.TotalPhysicalMemory/1gb )
	$output += "`r`n"
	
	return $output
}

function biosinfo {
    $output = ""
	$machine = "."
	$output += "BIOS INFO `r`n"
	$output += "===============`r`n"
	$biosInfo = Get-wmiobject win32_bios -comp $machine
	$output += "Name :" + $biosinfo.Name + "`r`n"
	$output += "Manufacturer :" + $biosinfo.Manufacturer + "`r`n"
	$output += "Serial No. :" + $biosinfo.SerialNumber+ "`r`n"
	
	return $output
}


function osinfo {
	$output = ""
	$machine = "."
	$output += "OS INFO `r`n"
	$output += "===============`r`n"
	
	$osInfo = get-wmiobject win32_operatingsystem -comp $machine
	
	$output += "OS Name:" + $osInfo.Caption + "`r`n"
	$output += "Service Pack:" + $osInfo.ServicePackMajorVersion + "`r`n"
	$output += "Windows Serial No.:" + $osInfo.SerialNumber + "`r`n"

	return $output
}

function driveInfo{
    $output = ""
	$machine = "."
	$output += "DISK INFO `r`n"
	$output += "===============`r`n"

    $logicalDisk = Get-WmiObject Win32_LogicalDisk -Filter "DriveType=3" -ComputerName $machine
	
    foreach($disk in $logicalDisk)
    {
        $diskObj = "" | Select-Object Disk,Size,FreeSpace
        $diskObj.Disk = $disk.DeviceID
        $diskObj.Size = "{0:n0} GB" -f (($disk | Measure-Object -Property Size -Sum).sum/1gb)
        $diskObj.FreeSpace = "{0:n0} GB" -f (($disk | Measure-Object -Property FreeSpace -Sum).sum/1gb)

        $text = "{0}  {1}  Free: {2}" -f $diskObj.Disk,$diskObj.size,$diskObj.Freespace
        $output += $text + "`r`n"
    }
    return $output
}

function servicesInfo {
	$machine = "."
	$output = ""
	$output += "SERVICE LIST `r`n"
	$output += "===============`r`n"
	
	foreach($srv in (get-wmiobject win32_service -computername $machine)) {
		$output += $srv.DisplayName + " - " + $srv.State + "`r`n"
	}
		
	return $output
}

function cpuInfo {
	$machine = "."
	$output = ""
	$output += "CPU INFO LIST `r`n"
	$output += "===============`r`n"
	
	foreach($cpu in (get-wmiobject win32_processor -computername $machine)) {
		$output += "id:" + $cpu.deviceid+ ", Name:" +  $cpu.name +  ", Manufacturer:" + $cpu.manufacturer +", Address Width:" + $cpu.AddressWidth +  "`r`n"
	}
		
	return $output
}



$data1 = sysinfo
$data2 = biosInfo
$data3 = osinfo
$data4 = driveInfo
$data5 = servicesInfo
$data6 = cpuInfo

$finaloutput = $data1 + "`r`n" + $data2  + "`r`n" +  "`r`n" + $data4 +  "`r`n"  + $data5 +  "`r`n" + $data6

write-host $finaloutput

$EmailTo = "recipient@somedomain.com"
$EmailFrom = "sender@somedomain.com"
$Subject = "System Info"
$Body = $finaloutput 
$SMTPServer = "mail.somedomain.com" 
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("sender@somedomain.com","somepassword"); 
$SMTPClient.Send($SMTPMessage)

This is how the data looks when it comes in the mail:

COMPUTER INFO
===============
Name :MADRID109
Domain :CHANGEME
Model :G31M-S2L
RAM :1.99 GB

BIOS INFO
===============
Name :Award Modular BIOS v6.00PG
Manufacturer :Award Software International, Inc.
Serial No. :


DISK INFO
===============
C:  466 GB  Free: 75 GB

SERVICE LIST
===============
Application Experience Lookup Service - Running
Alerter - Running
Application Layer Gateway Service - Running
Application Management - Running
Remote Server Manager - Running
ASP.NET State Service - Stopped
AT&T Labs Natural Voices Text to Speech 1.4 - Stopped
Windows Audio - Running
Background Intelligent Transfer Service - Running
Computer Browser - Stopped
Indexing Service - Running
ClipBook - Stopped
.NET Runtime Optimization Service v2.0.50727_X86 - Stopped
Microsoft .NET Framework NGEN v4.0.30319_X86 - Stopped
COM+ System Application - Stopped
Cryptographic Services - Running
DCOM Server Process Launcher - Running
Distributed File System - Stopped
DHCP Client - Running
Logical Disk Manager Administrative Service - Stopped
Logical Disk Manager - Running
DNS Server - Running
DNS Client - Running
Web Element Manager - Running
Error Reporting Service - Running
Event Log - Running
COM+ Event System - Running
Windows Presentation Foundation Font Cache 3.0.0.0 - Stopped
Help and Support - Running
HID Input Service - Running
HTTP SSL - Running
Intel(R) Matrix Storage Event Monitor - Running
Windows CardSpace - Stopped
IIS Admin Service - Running
IMAPI CD-Burning COM Service - Stopped
Intersite Messaging - Stopped
Kerberos Key Distribution Center - Stopped
Server - Running
Workstation - Running
License Logging - Stopped
TCP/IP NetBIOS Helper - Running
Intel(R) Management and Security Application Local Management Service - Running
Messenger - Stopped
NetMeeting Remote Desktop Sharing - Stopped
Distributed Transaction Coordinator - Running
FTP Publishing Service - Stopped
Windows Installer - Stopped
Message Queuing - Running
MySQL55 - Running
Network DDE - Stopped
Network DDE DSDM - Stopped
Net Logon - Stopped
Network Connections - Running
Net.Tcp Port Sharing Service - Stopped
Network Location Awareness (NLA) - Running
File Replication - Stopped
NT LM Security Support Provider - Running
Removable Storage - Stopped
Plug and Play - Running
IPSEC Services - Running
Microsoft POP3 Service - Running
Protected Storage - Running
Remote Access Auto Connection Manager - Stopped
Remote Access Connection Manager - Running
Remote Desktop Help Session Manager - Stopped
Routing and Remote Access - Stopped
Remote Registry - Running
Remote Procedure Call (RPC) Locator - Stopped
Remote Procedure Call (RPC) - Running
Resultant Set of Policy Provider - Stopped
Special Administration Console Helper - Stopped
Security Accounts Manager - Running
Smart Card - Stopped
Task Scheduler - Running
Secondary Logon - Running
System Event Notification - Running
Windows Firewall/Internet Connection Sharing (ICS) - Running
Shell Hardware Detection - Running
Simple Mail Transfer Protocol (SMTP) - Running
SNMP Service - Running
SNMP Trap Service - Stopped
Print Spooler - Running
Remote Administration Service - Running
Windows Image Acquisition (WIA) - Stopped
Microsoft Software Shadow Copy Provider - Stopped
Performance Logs and Alerts - Stopped
Telephony - Running
Terminal Services - Running
Themes - Stopped
Telnet - Stopped
Distributed Link Tracking Server - Stopped
Distributed Link Tracking Client - Running
Terminal Services Session Directory - Stopped
Windows User Mode Driver Framework - Stopped
Intel(R) Management and Security Application User Notification Service - Running
Uninterruptible Power Supply - Stopped
Virtual Disk Service - Stopped
Volume Shadow Copy - Stopped
Windows Time - Running
World Wide Web Publishing Service - Running
WebClient - Stopped
Windows Event Collector - Running
WinHTTP Web Proxy Auto-Discovery Service - Stopped
Windows Management Instrumentation - Running
Windows Remote Management (WS-Management) - Running
Portable Media Serial Number Service - Stopped
Windows Management Instrumentation Driver Extensions - Stopped
WMI Performance Adapter - Stopped
Windows Presentation Foundation Font Cache 4.0.0.0 - Stopped
Windows Search - Running
Automatic Updates - Running
Wireless Configuration - Running
Network Provisioning Service - Stopped

CPU INFO LIST
===============
id:CPU0, Name:Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz, Manufacturer:GenuineIntel, Address Width:32
id:CPU1, Name:Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz, Manufacturer:GenuineIntel, Address Width:32
id:CPU2, Name:Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz, Manufacturer:GenuineIntel, Address Width:32
id:CPU3, Name:Intel(R) Core(TM)2 Quad CPU    Q6600  @ 2.40GHz, Manufacturer:GenuineIntel, Address Width:32

 

 

4 Comments

  1. HI,

    i would like to know how to add the output of
    Get-NetIPConfiguration -All -Detailed
    to this script . i tried it myself but power shell keeps yelling at me in red .

    Thank you .

    • @Net-Guy

      Get-NetIPConfiguration only exists from Powershell 4.0 onwards. The older versions do not recognise the command. To get an idea of your installed Powershell version just type Get-Host in your Powershell console.

  2. @Anil Kumar. Yes you can use the built in function Export-CSV to export any data to a csv file. The csv file can then be opened in Excel.

    The only thing is that you will have to format the data as comma separated before exporting it as csv.

    Eg. $finalOuput | Export-Csv c:\data\sysinfo.csv

Leave a Reply to amit Cancel reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.