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 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:

$trigger = New-JobTrigger -Daily -At 3am
Register-ScheduledJob -Name DailyBackup -Trigger $trigger -ScriptBlock {Copy-Item c:\ImportantFiles d:\Backup$((Get-Date).ToFileTime()) -Recurse -Force -PassThru}

Once the trigger has fired and the job has run, you can work with it the same way you do with regular background jobs:

Get-Job -Name DailyBackup | Receive-Job

You can start a scheduled job manually, rather than waiting for the trigger to fire:

Start-Job -DefinitionName DailyBackup

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:

Register-ScheduledJob -Name Backup -ScriptBlock {Copy-Item c:\ImportantFiles d:\Backup$((Get-Date).ToFileTime()) -Recurse -Force -PassThru} -RunNow

How to generate complex password

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.

$Assembly = Add-Type -AssemblyName System.Web
[System.Web.Security.Membership]::GeneratePassword(8,3)

How do we know all that? With a little help from the Get-Member cmdlet (and simplified where syntax):

[System.Web.Security.Membership] | Get-Member -MemberType method -Static| where name -match password

Dynamic method invocation

Windows PowerShell 4.0 supports method invocation using dynamic method names.

$fn = "ToString"
"Hello".ToString()
"Hello".$fn()
"Hello".("To" + "String")()
#Select a method from an array:
$index = 1; "Hello".(@("ToUpper", "ToLower")[$index])()


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *