{"id":290,"date":"2019-02-26T22:03:35","date_gmt":"2019-02-26T22:03:35","guid":{"rendered":"http:\/\/michaelschultz.net\/tech\/?p=290"},"modified":"2019-02-26T22:42:52","modified_gmt":"2019-02-26T22:42:52","slug":"configmgr-log-collector-run-script","status":"publish","type":"post","link":"https:\/\/michaelschultz.net\/tech\/configmgr-log-collector-run-script\/","title":{"rendered":"ConfigMgr Log Collector Run Script"},"content":{"rendered":"\n<p>In most environment I have worked in, I have not had access or easy access to some systems I was expected to troubleshoot ConfigMgr issues on especially servers.&nbsp; I would normally have to ask for someone to send me logs and half the time needed to explain where to find that logs.&nbsp; I wrote a quick ConfigMgr Run Script that I can target against a system or collection and no longer have to ask for logs because the script will get them for me.<\/p><!--more-->\n\n\n\n<p>The script collection logs from CCMSETUP, CCM, and Windows Update.\u00a0 You can also expand the script to grab other logs easily.\u00a0 The only thing you need to add to the script is the location to send the log files.<\/p>\n\n\n\n\n<div class=\"codecolorer-container powershell default\" style=\"overflow:auto;white-space:nowrap;width:500px;\"><div class=\"powershell codecolorer\"><span class=\"coMULTI\">&lt;#<br \/>\n.SYNOPSIS<br \/>\nCopy Logs from system to share<br \/>\n<br \/>\n.DESCRIPTION<br \/>\nThis script will create a directory on a share and copy CCM and other logs from the system to the share. &nbsp;Deisgned to be used a a CM Run Script<br \/>\n<br \/>\n.NOTES<br \/>\nFileName: log_collector.ps1<br \/>\nAuthor: Michael Schultz<br \/>\nContact: mschultz@necro-monkey.com<br \/>\nCreated: 20190221<br \/>\nModified: <br \/>\nVersion: 1<br \/>\n<br \/>\n.EXAMPLE<br \/>\nlog_collector.ps1<br \/>\n<br \/>\n#&gt;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#-----Parameters-----<\/span><br \/>\n<br \/>\n<span class=\"kw3\">Param<\/span> <span class=\"br0\">&#40;<\/span><br \/>\n<br \/>\n<br \/>\n<span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"co1\">#-----Initializations and Module Imports-----<\/span><br \/>\n<br \/>\n<span class=\"co1\">#-----Variables-----<\/span><br \/>\n<span class=\"re0\">$pcname<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$env<\/span>:COMPUTERNAME<br \/>\n<span class=\"re0\">$savepath<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">&quot;&quot;<\/span> <span class=\"co1\">#specify you UNC for the share to copy logs<\/span><br \/>\n<span class=\"co1\"># remove commenting from next to lines to add date to folder name<\/span><br \/>\n<span class=\"co1\"># $datecollected = Get-Date -UFormat &quot;%Y%m%d&quot;<\/span><br \/>\n<span class=\"re0\">$foldername<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$pcname<\/span> <span class=\"co1\"># + &quot;-&quot; + $datecollected<\/span><br \/>\n<span class=\"re0\">$destinationRoot<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$savepath<\/span> <span class=\"sy0\">+<\/span> <span class=\"re0\">$foldername<\/span><br \/>\n<span class=\"re0\">$Logfile<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$destinationRoot<\/span> <span class=\"sy0\">+<\/span> <span class=\"st0\">'\\log_collector.log'<\/span><br \/>\n<br \/>\n<span class=\"re0\">$ccmlogs<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'C:\\Windows\\CCM\\Logs'<\/span><br \/>\n<span class=\"re0\">$CCMSetuplogs<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'C:\\Windows\\CCMsetup\\Logs'<\/span><br \/>\n<span class=\"re0\">$updatelogs<\/span> <span class=\"sy0\">=<\/span> <span class=\"st0\">'C:\\Windows\\WindowsUpdate.log'<\/span><br \/>\n<br \/>\n<span class=\"co1\">#-----Functions-----<\/span><br \/>\n<br \/>\n<span class=\"co1\">#-----Logging-----<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Remove log file if bigger than 1MB to prevent bloat<\/span><br \/>\n<span class=\"kw3\">If<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw1\">Test-Path<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">Get-Item<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"br0\">&#41;<\/span>.Length <span class=\"kw4\">-gt<\/span> 1MB<span class=\"br0\">&#41;<\/span> <br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">remove-item<\/span> <span class=\"re0\">$Logfile<\/span> <br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">else<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">new-item<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$Logfile<\/span> \u2013itemtype file<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#-----Execution-----<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Creates log directory<\/span><br \/>\n<span class=\"kw3\">If<\/span><span class=\"br0\">&#40;<\/span><span class=\"sy0\">!<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">test-path<\/span> <span class=\"re0\">$destinationRoot<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; <span class=\"kw1\">New-Item<\/span> <span class=\"kw5\">-ItemType<\/span> Directory <span class=\"kw5\">-Force<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$destinationRoot<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#get the date\/time and add some items to the log file<\/span><br \/>\n<span class=\"re0\">$mydate<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Date<\/span><br \/>\n<span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;*******************&quot;<\/span><br \/>\n<span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;$mydate Starting Log Collecting&quot;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Grab and copy CCM logs<\/span><br \/>\n<span class=\"kw3\">If<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">test-path<\/span> <span class=\"re0\">$ccmlogs<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$mydate<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Date<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;$mydate Collecting CCM Logs&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">New-Item<\/span> <span class=\"kw5\">-ItemType<\/span> Directory <span class=\"kw5\">-Force<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$destinationRoot<\/span>\\ccm<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Copy-Item<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$ccmlogs<\/span>\\<span class=\"sy0\">*<\/span> <span class=\"kw5\">-Recurse<\/span> <span class=\"kw5\">-Destination<\/span> <span class=\"re0\">$destinationRoot<\/span>\\ccm <span class=\"kw5\">-force<\/span> <span class=\"kw5\">-ErrorVariable<\/span> ProcessError<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$ProcessError<\/span> <span class=\"kw4\">-ne<\/span> <span class=\"re0\">$null<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> \u201c<span class=\"re0\">$ProcessError<\/span>\u201d<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$ProcessError<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$null<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Grab and copy CCM Setup logs<\/span><br \/>\n<span class=\"kw3\">If<\/span><span class=\"br0\">&#40;<\/span><span class=\"kw1\">test-path<\/span> <span class=\"re0\">$CCMSetuplogs<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$mydate<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Date<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;$mydate Collecting CCM Setup Logs&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">New-Item<\/span> <span class=\"kw5\">-ItemType<\/span> Directory <span class=\"kw5\">-Force<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$destinationRoot<\/span>\\ccmsetup<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Copy-Item<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$CCMSetuplogs<\/span>\\<span class=\"sy0\">*<\/span> <span class=\"kw5\">-Recurse<\/span> <span class=\"kw5\">-Destination<\/span> <span class=\"re0\">$destinationRoot<\/span>\\ccmsetup <span class=\"kw5\">-force<\/span> <span class=\"kw5\">-ErrorVariable<\/span> ProcessError<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$ProcessError<\/span> <span class=\"kw4\">-ne<\/span> <span class=\"re0\">$null<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> \u201c<span class=\"re0\">$ProcessError<\/span>\u201d<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$ProcessError<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$null<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#Grab and copy Winndows Update log<\/span><br \/>\n<span class=\"kw3\">If<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#91;<\/span>System.Environment<span class=\"br0\">&#93;<\/span>::OSversion.Version.Major <span class=\"kw4\">-ge<\/span> <span class=\"st0\">'10'<\/span><span class=\"br0\">&#41;<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$mydate<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Date<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;$mydate Collecting Windows Update Logs&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">New-Item<\/span> <span class=\"kw5\">-ItemType<\/span> Directory <span class=\"kw5\">-Force<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$destinationRoot<\/span>\\wu<br \/>\n&nbsp; &nbsp; Get<span class=\"sy0\">-<\/span>WindowsUpdateLog <span class=\"sy0\">-<\/span>LogPath <span class=\"re0\">$destinationRoot<\/span>\\wu\\WindowsUpdate.log <span class=\"kw5\">-ErrorVariable<\/span> ProcessError<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$ProcessError<\/span> <span class=\"kw4\">-ne<\/span> <span class=\"re0\">$null<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> \u201c<span class=\"re0\">$ProcessError<\/span>\u201d<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$ProcessError<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$null<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"kw3\">else<\/span><br \/>\n<span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$mydate<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Date<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;$mydate Collecting Windows Update Logs&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw1\">New-Item<\/span> <span class=\"kw5\">-ItemType<\/span> Directory <span class=\"kw5\">-Force<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$destinationRoot<\/span>\\wu<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">Copy-Item<\/span> <span class=\"kw5\">-Path<\/span> <span class=\"re0\">$updatelogs<\/span> <span class=\"kw5\">-Recurse<\/span> <span class=\"kw5\">-Destination<\/span> <span class=\"re0\">$destinationRoot<\/span>\\wu <span class=\"kw5\">-force<\/span> <span class=\"kw5\">-ErrorVariable<\/span> ProcessError<br \/>\n&nbsp; &nbsp; <span class=\"kw3\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"re0\">$ProcessError<\/span> <span class=\"kw4\">-ne<\/span> <span class=\"re0\">$null<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> \u201c<span class=\"re0\">$ProcessError<\/span>\u201d<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$ProcessError<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$null<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"co1\">#finalizing log<\/span><br \/>\n<span class=\"re0\">$mydate<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw1\">Get-Date<\/span><br \/>\n<span class=\"kw1\">Add-content<\/span> <span class=\"re0\">$Logfile<\/span> <span class=\"kw5\">-value<\/span> <span class=\"st0\">&quot;$mydate Log collection completed&quot;<\/span><\/div><\/div>\n\n\n\n\n<p>Script on my GitHub &#8211; <a href=\"https:\/\/github.com\/NecroMonkey\/vault\/blob\/master\/ConfigMgr-Run-Scripts\/log_collector.ps1\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">https:\/\/github.com\/NecroMonkey\/vault\/blob\/master\/ConfigMgr-Run-Scripts\/log_collector.ps1<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In most environment I have worked in, I have not had access or easy access to some systems I was expected to troubleshoot ConfigMgr issues on especially servers.&nbsp; I would normally have to ask for someone to send me logs and half the time needed to explain where to find that logs.&nbsp; I wrote a &hellip; <a href=\"https:\/\/michaelschultz.net\/tech\/configmgr-log-collector-run-script\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;ConfigMgr Log Collector Run Script&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,14,16,13],"tags":[],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-configmgr","category-powershell","category-run-script","category-scripting"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p98a2r-4G","post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/comments?post=290"}],"version-history":[{"count":3,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":295,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/posts\/290\/revisions\/295"}],"wp:attachment":[{"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelschultz.net\/tech\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}