Category Archives: Uncategorized

Send-Mail

$Body = "<i>Enter your HTML Waffle here</i>"
$Creds = Get-Credential

$MailProps = @{
    To = "Joe.Bloggs@domain.com"
    From = "Joe.Bloggs@domain2.com"
    Body = $Body
    BodyAsHTML = $true

    Subject = "Test"
    UseSSL = $true
    Port = 587
    SMTPServer = "smtp.domain.com"
    Credential = $Creds
    Attachments = "C:\temp\file.txt"
}

Send-Mail @MailProps

Floating images with the Display Posts plugin

It’s a great plugin bit the view is a bit clunky if you are displaying thumbnails and exerpts. Try adding the CSS below:

/* ICENI CUSTOM CSS */
.display-posts-listing img{
    float: left;
    margin: 0px 10px 10px 10px;
}
.display-posts-listing li{
    clear: both;
    list-style: none;
}

This can be added at the bottom of a CSS file or through the GUI at Appearance > Customise > Additional CSS

Tags and Categories on Pages

I’ve thrown together this plugin to sort this!

<?php
/*
Plugin Name: Enable Tags in WordPress Pages
Plugin URI: http://www.sitepoint.com/
Description: Enables tags in all content
Version: 1.0
Author: Craig Buckler
Author URI: http://twitter.com/craigbuckler
License: Free to use and adapt
*/

// add tag support to pages
function tags_support_all() {
	register_taxonomy_for_object_type('post_tag', 'page');
}

// ensure all tags are included in queries
function tags_support_query($wp_query) {
	if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}

// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');

Excerpts on Pages

Add the following to the Functions.php file…

/**
 * Enables Excerpts on Pages
 */
function enable_page_excerpt() {
  add_post_type_support('page', array('excerpt'));
}
add_action('init', 'enable_page_excerpt');

Adding it manually is a bad idea and I should stop being lazy so here is the same thing as its own plugin!

<?php
/*
Plugin Name: Enable Excerpts in WordPress Pages
Plugin URI: http://www.icenidesign.com/
Description: Enables Excerpts on pages
Version: 1.0
Author: Tom Nickel
Author URI: http://twitter.com/icenidesign
License: Free to use and adapt
*/


function enable_page_excerpt() {
  add_post_type_support('page', array('excerpt'));
}
add_action('init', 'enable_page_excerpt');

Save it as a php file in a folder of the same name and upload it to the plugins folder.

Checking if a variable has been submitted to a function

$PSBoundParameters.ContainsKey('Username')

The line above will return true if the ‘Username’ has been provided to the function

https://stackoverflow.com/questions/48643250/how-to-check-if-a-powershell-optional-argument-was-set-by-caller

Function get-TPNUserState() {
    <#
    .Description
    Supplies some sommonly needed values for a user:
    PasswordLastSet, DisplayName, Description, LastLogonDate, Manager, DistinguishedName, whencreated

    .Example
    get-TPNUserState tp-nickel

    .Parameter Username
    The username that you want to look into. If you don't enter one then it will prompt you.

    #>

    [Cmdletbinding()]
    param(
        [Parameter(Position=0)]
        [string]$Username
        )
    process{
         
        if(!$PSBoundParameters.ContainsKey('Username')){ $Username = read-host -Prompt UserName } #if Username is not set then prompt for it.
        Get-ADuser $Username -properties PasswordLastSet, DisplayName, Description, LastLogonDate, Manager, DistinguishedName, whencreated | select SAMAccountName, DisplayName, Description, Enabled, PasswordLastSet, LastLogonDate, whencreated, Manager, DistinguishedName | fl
    }

}

Module Directory in PowerShell

$env:PSModulePath -split ';'

The command above will list the directories that PowerShell will search for installed modules.

There is a difference between VSCode and PowerShell ISE though!

VSCode
PowerShell ISE

Notethat VSCode looks in C:\Users\User\Documents\Powershell\Modules compared with C:\Users\User\Documents\WindowsPowershell\Modules

Credentials in Powershell

http://duffney.io/AddCredentialsToPowerShellFunctions

function Set-RemoteRegistryValue {
    param(
        $ComputerName,
        $Path,
        $Name,
        $Value,
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = [System.Management.Automation.PSCredential]::Empty        
    )

    if($Credential -ne [System.Management.Automation.PSCredential]::Empty) {
        Invoke-Command -ComputerName:$ComputerName -Credential:$Credential  {
            Set-ItemProperty -Path $using:Path -Name $using:Name -Value $using:Value
        }
    } else {
        Invoke-Command -ComputerName:$ComputerName {
            Set-ItemProperty -Path $using:Path -Name $using:Name -Value $using:Value
        }
    }
}

Debugging and Breakpoints in Powershell

Write-Debug

As long as a script is an advanced script then you can use Write-Debug.

Write-Debug -Message "Enter debug text here, variables can be included."

The script will pause at each Write-Debug and display the debug text, including any variables. This will display the current content of any variables in the debug text.

To run a script with debugging turned on you must run the script with the debug switch.

.\MyTestSwitch.ps1 -debug

Set-PSBreakpoint

Another method for debugging is to set breakpoints prior to running a script.

Set-PSBreakPoint -Script .\MyTestSwitch.ps1 -Line 5
Set-PSBreakPoint -Script .\MyTestSwitch.ps1 -Variable ComputerName 

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-psbreakpoint?view=powershell-6

Removing PS-Breakpoints

Get-PSBreakPoint | Remove-PSBreakPoint