Live Cyber Attack Lab 🎯 Watch our IR team detect & respond to a rogue insider trying to steal data! Choose a Session

X

Getting Started with PowerShell Option Inputs

IT Pros, PowerShell

PowerShell is the dominant method of automating tasks and scripting changes for Windows sysadmins. This article covers getting started with some basic PowerShell usage and how to pass optional customization values into scripts.

At the command prompt arguments

Arguments

Arguments refer to the options that are passed to a script that can then be acted upon. A common usage pattern with Varonis products is that a rule is triggered which in turn calls a script – passing along the relevant details as arguments.

Example: When a DatAlert rule for X is bypassed the DatAlert passes [USER] [FOLDER_PATH] to the specified script.

This lets you codify your responses to user actions and can help make your life as a sysadmin easier.

PowerShell scripts are launched and edited from the PowerShell Interactive Script Editors (ISE)

Write-Host "Arguments:" $args.Length;
foreach ($arg in $args){
Write-Host "Argument: $arg";
}

Arguments are very flexible in that you don’t need to know how many will be passed in ahead of time. This is useful for situations where you’ll be taking the same action and applying it to a set of passed in things (ex. a series of usernames to grant or remove file access to). Arguments are accessed within your script in the order they were passed in (denoted in array syntax).

$arg[0] returns the value of the first argument $arg[1] returns the second.

Parameters

Parameters are arguments more formal big brother: they help make your scripts easier to maintain by explicitly assigning incoming options to variables.

To function, the parameters definition must be the first executable line in the script.

Example parameters block:

Param(
[string]$userName,
[string]$filePath
)

Further into your script you can then reference the $userName and $filePath variables where needed.

If you’re interested in seeing what can be done with PowerShell and Varonis, please checkout our Varonis PowerShell Resource Kit.

Michael Buckbee

Michael Buckbee

Michael has worked as a syadmin and software developer for Silicon Valley startups to the US Navy and everything in between.

 

Does your cybersecurity start at the heart?

Get a highly customized data risk assessment run by engineers who are obsessed with data security.