Category Archives: Uncategorized

User prompts in PowerShell

This first example is a simple Yes / No…..

#region select search
$Correct = "N"
while($Correct -ne "Y"){

    $SelectedSearch = Read-Host -Prompt "Enter the name of the search you wish to action"
    $Correct = Read-Host -Prompt "Is this the correct Search '$SelectedSearch' (y/n)"
}
#endregion select search

This second example nests another confirmation…..



#region hard or soft
$Correct = "N"
while($Correct -ne "Y"){

    $Action = "X"
    $Actions = "h", "s"
    while($Actions -notcontains $Action){

        $Action = Read-Host -Prompt "Hard Delete or Soft Delete (h/s)"
    }
    
    if($Action -eq "s"){$ActionDesc = "Soft Delete"}
    elseif($Action -eq "h"){$ActionDesc = "Hard Delete"}
    $Correct = Read-Host -Prompt "Is this the correct action '$ActionDesc' (y/n)"
}
#endregion hard or soft

Setting Modern or Basic Auth


# Basic and Modern Authentication Policies

$email = "test@test.ac.uk"
Connect-ExchangeOnline

$AuthPolicies = Get-AuthenticationPolicy | select name, allow*
$DefaultAuthPolicy = Get-OrganizationConfig | Select DefaultAuthenticationPolicy
$User = get-user $email | select UserPrincipalName, AuthenticationPolicy


$AuthPolicies | fl
$DefaultAuthPolicy
$User | fl

#Set User Level Auth Policy
#Set-User -Identity $Email -AuthenticationPolicy "Block Basic Authentication"

#Set Tenant Level Auth Policy
#Set-OrganizationConfig -DefaultAuthenticationPolicy "Block Basic Authentication"

This will list all of the available Auth Policies, show which one is assigned to $email and then change the policy applied to $email.

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
        }
    }
}