{"id":49793,"date":"2010-05-16T00:01:00","date_gmt":"2010-05-16T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/05\/16\/hey-scripting-guy-weekend-scripter-scripting-microsoft-security-essentials\/"},"modified":"2010-05-16T00:01:00","modified_gmt":"2010-05-16T00:01:00","slug":"hey-scripting-guy-weekend-scripter-scripting-microsoft-security-essentials","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-scripting-microsoft-security-essentials\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Scripting Microsoft Security Essentials"},"content":{"rendered":"<p><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Today, I finally had time to do something I have been wanting to do for a long time: I played around with <a href=\"http:\/\/www.microsoft.com\/security_essentials\/\"><font face=\"Segoe\">Microsoft Security Essentials<\/font><\/a> (the free downloadable anti-malware program from Microsoft). When I say &ldquo;played around with,&rdquo; I mean I began to look at seeing what I could do from a scripting perspective. I think Microsoft Security Essentials is pretty cool, and I have even installed it on my mom&rsquo;s computer, which should let you know that I think it is an awesome program. The fact that it is free is just icing on the cake. <\/p>\n<p class=\"MsoNormal\">I also have it installed on computers in my lab, and because those computers are not always turned on, it is inconvenient when I power them on to have to sit and wait while they download signature updates, do scans, and so on. I wanted the ability to update the virus signature from a script. If I could also launch a quick scan, that would be even better. <\/p>\n<p class=\"MsoNormal\">As it turns out, there is not an API for Microsoft Security Essentials; however, there is a command-line utility. When using <a href=\"http:\/\/www.microsoft.com\/windowspowershell\">Windows PowerShell<\/a>, having a command-line utility available to you is just about as good as having an API. I came up with the Invoke-SecurityEssentials.ps1 script seen here to update signatures, and to kick off default scans, quick scans, and full scans. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>Invoke-SecurityEssentials.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">&lt;#<br \/><span>&nbsp; <\/span>.Synopsis<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Runs Microsoft Security Essentials to scan or update anti-virus pattern <br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -UpdateSignature<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern<br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -DefaultScan<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern and performs default scan<br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -quickScan<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern and performs a quick scan<br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -fullScan<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern and performs a full scan<br \/><span>&nbsp;&nbsp; <\/span>.Notes<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>NAME:<span>&nbsp; <\/span>Invoke-SecurityEssentials.ps1<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>AUTHOR: Ed Wilson<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>LASTEDIT: 4\/30\/2010<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>KEYWORDS: Windows PowerShell, Scripting Guy, security, antivirus, WES-5-16-10<br \/><span>&nbsp;&nbsp; <\/span>.Link<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/www.ScriptingGuys.com<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/bit.ly\/hsgblog<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/bit.ly\/WeekendScripter<br \/><span>&nbsp;<\/span>#Requires -Version 2.0<br \/><span>&nbsp;<\/span>#&gt;<br \/>Param(<br \/><span>&nbsp;<\/span>[switch]$updateSignature,<br \/><span>&nbsp;<\/span>[switch]$defaultScan,<br \/><span>&nbsp;<\/span>[switch]$quickScan,<br \/><span>&nbsp;<\/span>[switch]$fullScan<br \/>)<\/p>\n<p>Function Invoke-SecurityEssentials<br \/>{<br \/><span>&nbsp;<\/span>Param($action)<br \/><span>&nbsp;<\/span>$path = &#8220;c:program filesmicrosoft security essentialsMPCMDRUN.EXE&#8221;<br \/><span>&nbsp;<\/span>Switch ($action)<br \/><span>&nbsp; <\/span>{<br \/><span>&nbsp;&nbsp; <\/span>$updateSignature { &amp;$path -signatureUpdate }<br \/><span>&nbsp;&nbsp; <\/span>$defaultScan { &amp;$path -scan }<br \/><span>&nbsp;&nbsp; <\/span>$quickScan { &amp;$path -scan -scantype 1 }<br \/><span>&nbsp;&nbsp; <\/span>$fullScan { &amp;$path -scan -scantype 2 }<br \/><span>&nbsp; <\/span>} #end switch<br \/>} #end function Invoke-SecurityEssentials<\/p>\n<p>Function Get-Results<br \/>{<br \/><span>&nbsp;<\/span>Get-EventLog -LogName system -Source &#8220;Microsoft Anti-Malware&#8221; -Newest 2 | <br \/><span>&nbsp;<\/span>Format-Table -Property timewritten, message -Wrap -auto<br \/>} # end function Get-Results<\/p>\n<p># *** entry point to script ***<br \/>$quickScan = $true<\/p>\n<p>If($updateSignature)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $updateSignature ;<span>&nbsp; <\/span>Exit }<br \/>If($defaultScan)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $defaultScan ; Get-Results ; Exit }<br \/>If($quickScan)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $quickScan ; Get-Results ; Exit }<br \/>If($fullScan)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $fullScan ; Get-Results ; Exit }<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">The script itself uses command-line parameters to allow you to perform the different actions. An <b>If<\/b> statement looks for the command-line parameters and passes the appropriate action to the <b>Invoke-SecurityEssentials<\/b> function. This portion of the script is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\"># *** entry point to script ***<\/p>\n<p>If($updateSignature)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $updateSignature ;<span>&nbsp; <\/span>Exit }<br \/>If($defaultScan)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $defaultScan ; Get-Results ; Exit }<br \/>If($quickScan)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $quickScan ; Get-Results ; Exit }<br \/>If($fullScan)<br \/><span>&nbsp;<\/span>{ Invoke-SecurityEssentials -action $fullScan ; Get-Results ; Exit }<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">Inside the <b>Invoke-SecurityEssentials<\/b> function, a <b>Switch<\/b> statement is used to parse th\ne input action and choose the appropriate command line. This is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">Param($action)<br \/><span>&nbsp;<\/span>$path = &#8220;c:program filesmicrosoft security essentialsMPCMDRUN.EXE&#8221;<br \/><span>&nbsp;<\/span>Switch ($action)<br \/><span>&nbsp; <\/span>{<br \/><span>&nbsp;&nbsp; <\/span>$updateSignature { &amp;$path -signatureUpdate }<br \/><span>&nbsp;&nbsp; <\/span>$defaultScan { &amp;$path -scan }<br \/><span>&nbsp;&nbsp; <\/span>$quickScan { &amp;$path -scan -scantype 1 }<br \/><span>&nbsp;&nbsp; <\/span>$fullScan { &amp;$path -scan -scantype 2 }<br \/><span>&nbsp; <\/span>} #end switch<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">After the appropriate command line has completed, control of the script returns to the calling code. When the function runs, no feedback is produced on the command line. The event log seen in the following image records the start time and the end time of the Security Essentials scan. <\/p>\n<p class=\"MsoNormal\"><span><img decoding=\"async\" title=\"Image of event log that records start time and end time of scan\" alt=\"Image of event log that records start time and end time of scan\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/may\/hey0516\/wes-5-16-10-01.jpg\" width=\"600\" height=\"420\"><\/span><\/p>\n<p class=\"MsoNormal\">The <b>Get-Results<\/b> function is used to query for the two most recent events related to the antivirus program. This code is shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">Get-EventLog -LogName system -Source &#8220;Microsoft Antimalware&#8221; -Newest 2 | <br \/>Format-Table -Property timewritten, message -Wrap -auto<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">When the script has run, the results seen in the following image are displayed. <\/p>\n<p class=\"MsoNormal\"><span><img decoding=\"async\" title=\"Image of results of script running\" alt=\"Image of results of script running\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/may\/hey0516\/wes-5-16-10-02.jpg\" width=\"600\" height=\"431\"><\/span><\/p>\n<p class=\"MsoNormal\">Because I used help tags when I was writing the script, you can receive command-line assistance from the <b>Get-Help<\/b> cmdlet. The help tags are shown here:<\/p>\n<p class=\"CodeBlockScreened\"><font><font face=\"Lucida Sans Typewriter\">&lt;#<br \/><span>&nbsp; <\/span>.Synopsis<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Runs Microsoft Security Essentials to scan or update anti-virus pattern <br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -UpdateSignature<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern<br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -DefaultScan<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern and performs default scan<br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -quickScan<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern and performs a quick scan<br \/><span>&nbsp;&nbsp; <\/span>.Example<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Invoke-SecurityEssentials.ps1 -fullScan<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Updates antivirus and malicious software pattern and performs a full scan<br \/><span>&nbsp;&nbsp; <\/span>.Notes<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>NAME:<span>&nbsp; <\/span>Invoke-SecurityEssentials.ps1<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>AUTHOR: Ed Wilson<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>LASTEDIT: 4\/30\/2010<br \/><span>&nbsp;&nbsp;&nbsp; <\/span>KEYWORDS: Windows PowerShell, Scripting Guy, security, antivirus, WES-5-16-10<br \/><span>&nbsp;&nbsp; <\/span>.Link<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/www.ScriptingGuys.com<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/bit.ly\/hsgblog<br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/bit.ly\/WeekendScripter<br \/><span>&nbsp;<\/span>#Requires -Version 2.0<br \/><span>&nbsp;<\/span>#&gt;<\/p>\n<p><\/font><\/font><\/p>\n<p class=\"MsoNormal\">When you call the script with <b>Get-Help<\/b>, the output shown in the following image appears. <\/p>\n<p class=\"MsoNormal\"><span><img decoding=\"async\" title=\"Image of output of calling script with Get-Help\" alt=\"Image of output of calling script with Get-Help\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/may\/hey0516\/wes-5-16-10-03.jpg\" width=\"600\" height=\"424\"><\/span><\/p>\n<p class=\"MsoNormal\">\n<p>&nbsp;<\/p>\n<\/p>\n<p class=\"MsoNormal\">Well, that is about all there is to playing around with Microsoft Security Essentials and Windows PowerShell. If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">FaceBook<\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post them on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/span><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft Scripting Guy Ed Wilson here. Today, I finally had time to do something I have been wanting to do for a long time: I played around with Microsoft Security Essentials (the free downloadable anti-malware program from Microsoft). When I say &ldquo;played around with,&rdquo; I mean I began to look at seeing what I could [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[143,3,63,61,45],"class_list":["post-49793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-antivirus","tag-scripting-guy","tag-security","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Microsoft Scripting Guy Ed Wilson here. Today, I finally had time to do something I have been wanting to do for a long time: I played around with Microsoft Security Essentials (the free downloadable anti-malware program from Microsoft). When I say &ldquo;played around with,&rdquo; I mean I began to look at seeing what I could [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/49793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=49793"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/49793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=49793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=49793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=49793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}