{"id":953,"date":"2013-01-07T12:43:18","date_gmt":"2013-01-07T16:43:18","guid":{"rendered":"http:\/\/wehuberconsultingllc.com\/wordpress\/?p=953"},"modified":"2013-01-07T13:34:49","modified_gmt":"2013-01-07T17:34:49","slug":"powershell-snippets","status":"publish","type":"post","link":"https:\/\/wehuberconsultingllc.com\/wordpress\/2013\/01\/07\/powershell-snippets\/","title":{"rendered":"Powershell Snippets"},"content":{"rendered":"<p><b>First\/End of Month Date calculations<\/b><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\"> \r\n$dNow = get-date \r\n$dNowMonth = $dNow.Month \r\n$dNowYear = $dNow.Year \r\n#Use for beginning of month datetime search, first day at 00 am \r\n$firstDayOfMonth = get-date &quot;$dNowMonth\/1\/$dNowYear&quot; \r\n#Use for end of month datetime search, first day of next month at 00 am \r\n$lastDayOfMonth = $firstDayOfMonth.AddMonths(1) \r\n#Use this end of month search when you do not need the time value \r\n$ReallastDayOfMonth = $lastDayOfMonth.AddDays(-1) \r\nwrite-host &quot;FirstDay = $firstDayOfMonth LastDay = $lastDayOfMonth RealLastDayOfMonth = $ReallastDayOfMonth&quot; \r\n<\/pre>\n<p><b>Create alias if does not exist<\/b><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\"> \r\nif ( -not (get-alias zip -ErrorAction SilentlyContinue)) \r\n    {new-item -path alias:zip -value &quot;C:\\Program Files\\7-Zip\\7z.exe&quot;} \r\n<\/pre>\n<p><b>Map the network drive if it does not exist( with &#8220;filesystem&#8221; fix for Test-Path on a network drive).<\/b><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\"> \r\n$drive = &quot;H:&quot; \r\n$remotedir = &quot;\\\\myserver\\SYSBACKUPS&quot; \r\n$localdir = &quot;F:\\SYSBACKUPS\\&quot; \r\n$filesel = &quot;sql_db_&quot; + (get-Date -uformat &quot;%Y%m%d&quot;) + &quot;.7z&quot; \r\nif (-not(Test-Path ($localdir+$filesel))) \r\n    {\r\n    #We do not have the backup file so lets see if network drive is mapped \r\n    if (-not (Test-Path -path &quot;filesystem::H:\\&quot;)) \r\n        { \r\n        $net = new-object -ComObject WScript.Network\r\n        $net.MapNetworkDrive($drive, $remotedir, $false, &quot;username&quot;, &quot;password&quot;)\r\n        }\r\n#... Insert rest of the code here\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>First\/End of Month Date calculations $dNow = get-date $dNowMonth = $dNow.Month $dNowYear = $dNow.Year #Use for beginning of month datetime search, first day at 00 am $firstDayOfMonth = get-date &quot;$dNowMonth\/1\/$dNowYear&quot; #Use for end of month datetime search, first day of next month at 00 am $lastDayOfMonth = $firstDayOfMonth.AddMonths(1) #Use this end of month search when &hellip; <a href=\"https:\/\/wehuberconsultingllc.com\/wordpress\/2013\/01\/07\/powershell-snippets\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Powershell Snippets&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[85],"tags":[143],"class_list":["post-953","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4iN3d-fn","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/posts\/953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":5,"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"predecessor-version":[{"id":959,"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/posts\/953\/revisions\/959"}],"wp:attachment":[{"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wehuberconsultingllc.com\/wordpress\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}