{"id":145,"date":"2017-09-18T13:28:08","date_gmt":"2017-09-18T12:28:08","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=145"},"modified":"2017-09-24T16:30:16","modified_gmt":"2017-09-24T15:30:16","slug":"powershell-tips-tricks","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2017\/09\/18\/powershell-tips-tricks\/","title":{"rendered":"Powershell Tips &#038;Tricks"},"content":{"rendered":"<p>Hi Guys today will just share some useful tips and tricks! <\/p>\n<p>Just save the IMG to check it out, or check it below .<\/p>\n<p>Enjoy<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/PSTips1-300x211.jpg\" alt=\"\" width=\"300\" height=\"211\" class=\"alignnone size-medium wp-image-146\" srcset=\"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/PSTips1-300x211.jpg 300w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/PSTips1-600x422.jpg 600w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/PSTips1-768x540.jpg 768w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/PSTips1-1024x720.jpg 1024w, http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/PSTips1.jpg 1141w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><strong>How to Scheule a job<\/strong> <\/p>\n<p>Job scheduling allows you to schedule execution of a PowerShell background job for a later time. The first thing you do is create a job trigger. This defines when the job will execute. Then you use the Register-ScheduledJob cmdlet to actually register the job on the system. In the following example, every day at 3 am we back up your important files:<\/p>\n<p><code class=\"\" data-line=\"\">$trigger = New-JobTrigger -Daily -At 3am<br \/>\nRegister-ScheduledJob -Name DailyBackup -Trigger $trigger -ScriptBlock {Copy-Item c:\\ImportantFiles d:\\Backup$((Get-Date).ToFileTime()) -Recurse -Force -PassThru}<\/code><\/p>\n<p>Once the trigger has fired and the job has run, you can work with it the same way you do with regular background jobs:<\/p>\n<p><code class=\"\" data-line=\"\">Get-Job -Name DailyBackup | Receive-Job<\/code><\/p>\n<p>You can start a scheduled job manually, rather than waiting for the trigger to fire:<\/p>\n<p><code class=\"\" data-line=\"\">Start-Job -DefinitionName DailyBackup<\/code><\/p>\n<p>The RunNow parameter for Register-ScheduledJob and Set-ScheduledJob eliminates the need to set an immediate start date and time for jobs by using the -Trigger parameter:<\/p>\n<p><code class=\"\" data-line=\"\">Register-ScheduledJob -Name Backup -ScriptBlock {Copy-Item c:\\ImportantFiles d:\\Backup$((Get-Date).ToFileTime()) -Recurse -Force -PassThru} -RunNow<\/code><\/p>\n<p><strong>How to generate complex password<\/strong><\/p>\n<p>There is no built-in cmdlet to generate a password, but we can leverage a vast number of .NET Framework classes. System.Web.Security.Membership class has a static method GeneratePassword(). First, we need to load an assembly containing this class, and then we use GeneratePassword() to define password length and a number of non-alphanumeric characters. <\/p>\n<p><code class=\"\" data-line=\"\">$Assembly = Add-Type -AssemblyName System.Web<br \/>\n[System.Web.Security.Membership]::GeneratePassword(8,3)<\/code><\/p>\n<p>How do we know all that? With a little help from the Get-Member cmdlet (and simplified where syntax):<\/p>\n<p><code class=\"\" data-line=\"\">[System.Web.Security.Membership] | Get-Member -MemberType method -Static| where name -match password<\/code><\/p>\n<p><strong>Dynamic method invocation<\/strong><\/p>\n<p>Windows PowerShell 4.0 supports method invocation using dynamic method names.<\/p>\n<p><code class=\"\" data-line=\"\">$fn = &quot;ToString&quot;<br \/>\n&quot;Hello&quot;.ToString()<br \/>\n&quot;Hello&quot;.$fn()<br \/>\n&quot;Hello&quot;.(&quot;To&quot; + &quot;String&quot;)()<br \/>\n#Select a method from an array:<br \/>\n$index = 1; &quot;Hello&quot;.(@(&quot;ToUpper&quot;, &quot;ToLower&quot;)[$index])()<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi Guys today will just share some useful tips and tricks! Just save the IMG to check it out, or check it below . Enjoy How to Scheule a job Job scheduling allows you to schedule execution of a PowerShell background job for a later time. The first thing you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":149,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-michael-goulart"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/tips-tricks-hero-img.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":4,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":153,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/145\/revisions\/153"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/149"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}