Skip to main content

0210 | Introducing Scripting and Toolmaking

info

ISE stands for MS Windows PowerShell Integrated Scripting Environment

Tricks for ISE

  • set: | show script pane maximalized
  • use ctrl + r to switch between the panes
  • once in a new file --> press ctrl + j for the written templates!

Example | Get free space from C

# old fashioned way
get-wmiobject win32_logicaldisk -filter "DeviceID='c:'" | select freespace
# give it in gb -- use 1gb (built-in) -- use int for rounding
get-wmiobject win32_logicaldisk -filter "DeviceID='c:'" | select @{n='freegb'; e={$_.freespace / 1gb -as [int]}}

# use [ctrl + space] to list your options (namespace)
get-ciminstance win32_logicaldisk

Issue | Hard coded Computer Name

# specify the target computer -- issue: hard coded computername
get-wmiobject -computername localhost -class win32_logicaldisk -filter "DeviceID='c:'"

Fixing the previous issue | New Issue: Still needs editing to properly run it

# create the var
$ComputerName = 'localhost'

Get-WmiObject -ComputerName $ComputerName win32_logicaldisk -filter "DeviceID='c:'" |
select @{n="freeGB";e={$PSItem.freespace / 1gb -as [int]}}

Doing it with Parameters | Parameterized Script

# create the parameter -- use [string[]] to force a string array and not object -- srongly type it
param(
[string[]]$ComputerName = 'localhost'
)

Get-WmiObject -ComputerName $ComputerName win32_logicaldisk -filter "DeviceID='c:'" |
select @{n="freeGB";e={$PSItem.freespace / 1gb -as [int]}}
# running it with `.\test.ps1 -ComputerName localhost`
# powershell is already dinamically building the help file
# result: diskinfo.ps1 [[-ComputerName] <string[]>]
get-help .\diskinfo.ps1

Turning on Parameter Attributes with Parameter Binding

  • allows us to use parameter attributes | like making things mandatory
# making the parameter computername mandatory
[CmdletBinding()]
param(
# this parameter attribute only affects the next parameter and not bogus
[Parameter(Mandatory=$True)]
[string[]]$ComputerName,
$bogus
)

Get-WmiObject -ComputerName $ComputerName win32_logicaldisk -filter "DeviceID='c:'"

Adding Help/Documentation to the Script

<#
.Synopsis
This is the short explanation.
Short info: HeyHo

.Description
This part should be the description.
Let's see if it works or not.

.Parameter ComputerName
This is for remote computers

.Example
DiskInfo -ComputerName localhost
This is for a remote computer.

#>

[CmdletBinding()]
param(
# this parameter attribute only affects the next parameter and not bogus
[Parameter(Mandatory=$True)]
[string[]]$ComputerName,
$bogus
)

Get-WmiObject -ComputerName $ComputerName win32_logicaldisk -filter "DeviceID='c:'"

Adhere to the PS cmdlets Naming | "verb - noun"

<#
.Synopsis
This is the short explanation.
Short info: HeyHo

.Description
This part should be the description.
Let's see if it works or not.

.Parameter ComputerName
This is for remote computers

.Example
DiskInfo -ComputerName localhost
This is for a remote computer.

#>

function Get-DiskInfo{
[CmdletBinding()]
param(
# this parameter attribute only affects the next parameter and not bogus
[Parameter(Mandatory=$True)]
[string[]]$ComputerName,
$bogus
)

Get-WmiObject -ComputerName $ComputerName win32_logicaldisk -filter "DeviceID='c:'"
}

# Try to run it
# results in no output
.\diskinfo.ps1
# this would result in an error
Get-DiskInfo
# reason: script run --> made function up in memory -- creates a new stack --> when script ended -- stack goes away -- everything is cleared up --> discarded everything

# A not so nice solution -- more like a quick fix
# first source it
. .\diskinfo.ps1
# then run it
Get-DiskInfo
# explanation: when run -- does not create a new stack -- because of the "." it runs in the current stack --> created variables stick around even when the script exits

creating a module -- the CHEESY way

  • no need to source it to run it
  • but you will need to import it every time you want to run it
# 1 - change the file extension from ".ps1" to "psm1"

# 2 - import it as a module
# -Verbose:to display what exactly is happening
Import-Module .\diskinfo.psm1 -Force -Verbose

# 3 - run it
Get-DiskInfo

Creating a Module | The Right Way

  • to load the module dynamically
  • idea
    • put the module onto the ps module path (or add your path to it)
  • note
    • do NOT put the module onto "C:\Windows\system32\WindowsPowerShell\v1.0\Modules"
    • that's only for MS
  • Steps:
    • Step-1 | Create/Use the path C:\Users\<username>\Documents\WindowsPowerShell\Modules
      • if not exist, create the folders
    • Step-2 | Create a folder with the exact same name as your module (like "diskinfo")
    • Step-3 | Copy the source file and put it under the module
      • example: C:\Users\<username>\Documents\WindowsPowerShell\Modules\diskinfo\diskinfo.psm1
  • will automatically load everytime ps is loaded