Archive for the ‘PowerShell scripts’ Category

 

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 

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!

(more…)

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.

(more…)

Yes you can!
Having the option to fully automate your work is the goal for most PowerShell enthusiasts. One step closer – one step closer 🙂

(more…)

When scripting different solutions, i prefere to have Powershell do most of the heavy lifting:-)

Adding a CER certificate to Trusted Root Certificate store.
Code:

CERTUTIL -addstore -enterprise -f -v root C:\MyCert.cer

Adding a PFX certificate to Personal Certificate store.
Code:

certutil –f –p "Password" –importpfx C:\myCert.pfx

When running a test environment and needing a certificate, one will not always have a CA (Certificate Authority lying around).  In Windows Server 2012 and Windows 8, there is a PowerShell command to create a multiple Subject named certificate:

(more…)

Hello world PowerShell

Posted: June 24, 2014 in PowerShell scripts

Open PowerShell and type in:
write-host -f cyan “Hello World”

Sh

🙂

 

This is a test post!