Turning Commands into Scripts
Now we are going to make any commands into scripts for
automation.
See into above image. Get-WmiObject command is used to get
Total size and Free size of computer.
Here I hardcoded ComputerName and Drive. So see
into below image how I use variables to remove hardcode.
Now I am going to add parameters into my script.
We can use param() to make our script parameterize.
Now we are going to make parameterize function into our
script. Following image shows the structure of function.
Here the line [CmdletBinding()] is used. This is that next click
in formalization. This is where you really transform a function to full cmdlet.
You are done with scripting. Now save your script.
Test your script with PowerShell. Run PowerShell as
Administrator. Go to directory where you save your scripts and simply just run
script. And try to use function you make it.
Oops It gives error as you show in picture.
So here what happen when you run your script new line in
stack is created and your function is stored into that stack. When script is
finished the stack is cleared and your function is cleared.
So what’s next??
The solution for that is make PowerShell not to remove your
stack data until you tell to remove or close the Powershell, even-if your
script is finished.
To do that as shown in following picture use ‘.’ (dot) before
you run the script.
0 comments:
Post a Comment