PowerShell Basics: Functions

Exported on 11-Nov-2021 11:23:54

POWERSHELL FUNCTIONS ON ATTUNE

This is a Blueprint on Attune of how to work with Functions in PowerShell.

A Function is a list or a group of PowerShell statements that has a name assigned to it.

When a Function is been executed, you type in the Function name and the statements run in the command prompt.

Functions can return values that can be displayed, assigned to variables, or passed to other functions or cmdlets.

Parameters

Name Type Script Reference Default Value Comment
Attune Node Windows Server attuneNode This is an Attune Node
Attune Node Credentials Windows OS Credential attuneNodeCredentials This is my Attune Node Credential

1 - Functions

Below shows the basic Function syntax:


function <function-name> {statements list}

Below shows a basic Function syntax using named parameters:

function <function-name> {
    param ($parameter1, $parameter2)
    <statement list>
  }

The Function Name (function-name): This is the name of the function been created.

Parameter Section [param ($parameter1, $parameter2)]: This is where any number of named parameters are inputed (this is optional).

The Statement List ({}): This is where all PowerShell commands are been written for the function to execute.

The Example in the Blueprint has two functions (one a basic function and the second a basic function with named parameters).

The Fist Function (FunctionOne) has two commands that write out a welcome message and timestamp of the Server Attune is running on.

When the FunctionOne is called it runs all the commands in the Script Block "{}" (statement list).

The Second Function (FunctionTwo) has named parameters that are assigned default values of the Variables ($FirstVariable & $SecondVariable) which are defined in the script.

When the Function (FunctionTwo) is called it also runs all the commands in the Script Block "{}".

The connection details have changed from the last step.

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credentials}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
#Region Function

#Region Function One 
#==============================================================================
# Declaring FunctionOne
function FunctionOne {
    # This writes a welcome message from ATTUNE 
    # This is a print to screen CMDLET [Write-Host].
    Write-Host "Welcome to Attune by AttuneOps"

    # This gets and write the timestamp on the current server
    # This is a print to screen CMDLET [Write-Host].
    Write-Host "Timestamp : " (Get-Date).DateTime
}
#EndRegion Function One
#==============================================================================


#Region Function Two
#==============================================================================
# Creates a Variable called UserName and saves the Username of the user signed into the current Server
$UserName = ($env:USERNAME).Split('.')

# Creates a Variable called ServerName and saves the current Server name in it.
$ServerName = ($env:computername).Split('.')

# Declaring FunctionTwo
function FunctionTwo {
    # This is a parameter section that has declared named parameters and they are assigned default values of the Variables ($UserName & $ServerName)
    param (
        # First named parameter assigned a variable $UserName
        $FirstVariable = $UserName,

        # Second named parameter assigned a variable $ServerName
        $SecondVariable = $ServerName
    )
    # This writes out the values in the Variables ($FirstVariable & $SecondVariable) to screen.
    # This is a print to screen CMDLET [Write-Host].
    Write-Host $FirstVariable "you are logged into" $SecondVariable
}

# Calling the Function name FunctionOne to execute
FunctionOne

# Calling the Function name FunctionOne to execute
FunctionTwo

#EndRegion Function Two
#==============================================================================
#EndRegion Function