If you are familiar with Powershell scripting, you surely know that variables can really mess up your day.

I’ve written a little function you can dot source into your script and run at specific points in your script to show the current variables and their types and values. It’s not perfect, and I’ll probably make it prettier in time. But here you go.

newdl

 


 Function CheckMyVariables
 {
    Param
        ([Switch]$Full)

    Function CheckVariableType
    {
        Param
            ($Inputdata,$Inputname)

        Begin
        {
            Write-Host ""
            Write-Host -ForegroundColor DarkGray "*Started*"
        }

        Process
        {
            #Check what kind of type the variable is and if it is empty
            If(!($InputData)){Write-Host -ForegroundColor Yellow "The variable $Inputname is Null or Empty ";Return}
            If($InputData -is [String] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is a: " -NoNewline;Write-Host "[String]"}
            Elseif ($InputData -is [Int] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Int]"}
            Elseif ($InputData -is [Array] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Array]"}
            Elseif ($InputData -is [System.Boolean] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Boolean]"}
            Elseif ($InputData -is [System.Collections.Arraylist] -eq $true){Write-Host -ForegroundColor Green ""$"$Inputname is an: " -NoNewline;Write-Host "[Arraylist]"}

            #Formatting output of Arrays and Arraylists
            If($InputData -is [Array] -eq $true -or $InputData -is [System.Collections.Arraylist] -eq $true)
            {
                Write-Host -ForegroundColor Green ""$"$Inputname value(s) is/are:"
                $InputData
            }

            #Normal formatting on everything else
            Else
            {
                Write-Host -ForegroundColor Green ""$"$Inputname value(s) is/are: " -NoNewline
                Write-Host $InputData
            }

            #If switch is enabled then run the following
            If($Full -eq $true)
            {
                Write-Host -ForegroundColor Green ""$"$Inputname type confirmed with .GetType() cmlet: " -NoNewline
                Write-Host $InputData.GetType()
            }
        }

        End
        {
            Write-Host -ForegroundColor DarkGray "*Finished*"
        }
    }

    #A function to remove multiple values from an array
    Function RemoveArrayValues ($Array,$RemoveValues)
    {
        Foreach ($value in $RemoveValues)
        {
            $Array = $Array | ? {$_ -ne $value}
        }
        Return $Array
    }

    #Here I list environment variables and other variables i want to exclude from view..
    $ExcludeVar = '$','?','^','args','ConfirmPreference','ConsoleFileName','DebugPreference','Error','ErrorActionPreference','ErrorView','ExecutionContext','false','FormatEnumerationLimit',
    'HOME','Host','InformationPreference','input','MaximumAliasCount','MaximumDriveCount','MaximumErrorCount','MaximumFunctionCount','MaximumHistoryCount','MaximumVariableCount','MyInvocation',
    'NestedPromptLevel','null','OutputEncoding','PID','profile','ProgressPreference','PSBoundParameters','PSCommandPath','PSCulture','PSDefaultParameterValues','PSEmailServer','PSHOME','psISE',
    'PSScriptRoot','PSSessionApplicationName','PSSessionConfigurationName','PSSessionOption','PSUICulture','psUnsupportedConsoleApplications','PSVersionTable','PWD','ShellId','StackTrace',
    'true','VerbosePreference','WarningPreference','WhatIfPreference','x64','foreach','ExcludeVar','checkvar','var','allvariables','Full'

    #Get all variables
    $allVariables = (Get-Variable).Name

    #Remove excludevariableslist
    $allVariables = RemoveArrayValues $allVariables $ExcludeVar
    Foreach ($var in $allVariables)
    {
        $checkvar = Get-Variable -Name $var
        CheckVariableType $checkvar.Value $var
    }

    #Default value of the switch is false
    $Full = $false
}

Run as follows


CheckMyVariables 

Advertisements

I cant believe that I didn’t know about this until today, even though I have been Power-shelling for a few years now!

In the Windows PowerShell Integrated Scripting Environment (ISE) you are able to insert logical snippets by setting your curser in the script pane and pressing the keys: Ctrl + j.

screen screen2

This means you can use less time remembering syntax and more time solving your tasks.

Happy Powershelling!

When posting code on your wordpress blog, you will quite often find yourself editing your posts 3 seconds after publishing them. You might have noticed like me that WordPress alters certain characters in posted code, which can be really annoying when you post like 300 lines of Powershell code:-)

To get past this problem, I created a script that corrects the characters that has been changed by Wordpress.
Simply copy your edited text into a text file and save it anywhere on your computer. Then run the code under, point to your file when the “open file dialog box” appears. I hope this saves you a bit of frustration, it has for me!

Read the rest of this entry »

Often when automating your SharePoint solutions, you will find yourself adding custom customizations, like copying a picture file to a specific location on all your SharePoint servers. To do this we can for example use the following PowerShell code.

Read the rest of this entry »

I think that title will be my longest ever:-)

Here you will find 3 Functions for starting,stopping and restarting windows services that can target specific servers, local servers and all sharePoint Servers.

Read the rest of this entry »

When configuring your shiny new Service Applications using PowerShell, depending on the size of your SharePoint farm. You may want to be running some services on multiple SharePoint Servers. The code bellow consists of two functions to start and stop service instances per server.

Read the rest of this entry »

Useful Powershell code to quickly find the database sizes in your SharePoint farm.

Get-SPDatabase | Sort-Object disksizerequired -desc | %{write-host  “Size: $($_.disksizerequired/1mb)MB – Database: $($_.Name)”}

Whether you are running a large custom-packed-WSP filled SharePoint 2013 Farm or a simple farm with a few different Web Applications, it could be nice to automate the deployment and removal process of WSP-packages to avoid any kind of manual work!

Here’s a way to deploy and remove groups of WSP packages to both -all Web Applications and -spesific Web Applications. The sky is the limit!

Read the rest of this entry »

When setting up new Web Applications left and right, it can be very frustrating to enter the email settings on the Web Applications. Under is a code-snippet and a Function to set email settings on your Web Applications using PowerShell.

Read the rest of this entry »

When being an active PowerShell user it can be handy to have your favourite modules and Snapin’s available on PowerShell Launch!
Every user on the server will have his/her own “location” ps1 file that probably does not exist at the moment. You will probably have to create it first.

Read the rest of this entry »