Creating Powershell Script Tips Reflection - jwells24/Tech-Journal GitHub Wiki
Creating Powershell Script Tips and Reflection
Tips
- If you are unsure of the type of an object, use Get-Type to find the object type.
$ds.GetType()
- When trying to use greater than or less than in powershell, make sure to use the proper alias. Below is the alias for greater than.
if(index -gt 0)
{
}
-
Make sure to keep your variables in mind. Variables inside of a function are not global, and can only be used in said function. Variables inside of a specific file, such as 480driver.ps1, are special to that file and cannot be used outside of that file.
-
If you want to display text with a color, use Write-Host like shown below.
Write-Host -ForegroundColor "Green" "Hello World!"
- Use this syntax for creating a function with parameteres.
Function HelloWorld([string] $text)
- Read input from the user with Read-Host, like below.
$name = Read-Host "Enter Name Here"
Reflection
- Starting from the beginning, I think that my initial struggle was getting the handle of coding in powershell. I was slow at first in terms of knowing the syntax but I picked it up and got faster as the project went on. My first big issue I ran into was accidentally naming one of my functions as the name of a VMWare function. This meant that in my 480driver, I was calling another function instead of mine. This took me a while to figure out until I saw it and fixed it. Another tough spot I ran into was the variable types of a VM, datastore, snapshot, and VMHost. I was confused as to what to put in the parameters for the type of these objects, but I was eventually able to find the documentation on VMWare where I was able to obtain the types of these objects. Once I had the correct types, I just worked through my original linked clone script to work out my functions.