PoshCode Archive  Artifact Content

Artifact 3318beb0c086ca3289b63084ea2a34b1a61a79eb3b79e8e67d7257545ab50db9:

  • File Sj-strand.ps1 — part of check-in [6fc5e3db9f] at 2018-06-10 14:12:56 on branch trunk — MyTools is a scriptmodule used as an example in a powershell course delivery. (user: David size: 4264)

# encoding: ascii
# api: powershell
# title: Sjöstrand
# description: MyTools is a scriptmodule used as an example in a powershell course delivery.
# version: 0.1
# type: script
# author: David
# license: CC0
# function: Test-MyConnection
# x-poshcode-id: 6315
# x-archived: 2016-04-24T07:00:30
# x-published: 2016-04-22T10:25:00
#
#
<#

    .SYNOPSIS
    sends a ping to computers
    .DESCRIPTION
    ping computers on the network ...
    .EXAMPLE
    "lon-dc1","lon-cl1" | Test-MyConnection
    .EXAMPLE
    Test-MyConnection -ComputerName "lon-dc1"
#>
function Test-MyConnection
{
    [cmdletbinding()]
    param(
        #The computer to ping
        [Parameter(
            Mandatory=$true,
            HelpMessage='Supply the dns name or ip address of the system to ping.',
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true
        )]
        [Alias('CN','Address','dnshostname')]
        [ValidateNotNullOrEmpty()]
        [string[]]$ComputerName,
        #timeout in milliseconds
        [int]$timeout=100,
        #time in milliseconds to wait between each ping
        [int]$wait=10
    )

    BEGIN
    {
        $starttime = [datetime]::utcnow #get the utc start time 
        $count = 0
        $colJob = @()
        Write-Verbose "timeout = $timeout"
    }
    PROCESS
    {
        foreach ($address in $ComputerName)
        {
            $count++
            start-sleep -Milliseconds $wait
            $colJob += Get-WmiObject -AsJob -Query "Select address,statuscode,responsetime FROM win32_PingStatus WHERE address='$address' AND timeout=$timeout"
        }
    }
    END
    {
        foreach ($job in $colJob)
        {
            $result = Wait-Job $job | Receive-Job
            if ($result.statuscode -eq 0)
            {
                $h = @{ComputerName=$result.address;ResponseTime=$result.responsetime}
                New-Object -TypeName PSObject -Property $h
            }
            Remove-Job $job
        }
        Write-Verbose "Pinged $count addresses in $(([datetime]::utcnow - $starttime).totalseconds) seconds."
    }

}

function Get-MySum
{
    [cmdletbinding()]
    param(
        [string]$Property,
        [Parameter(ValueFromPipeline=$true)][object]$InputObject
    )
    BEGIN
    {
        $sum = 0
    }
    PROCESS
    {
        $sum += $InputObject.$Property
    }
    END
    {
        $sum
    }
}

function Get-MyUser
{
    if ((Test-Path c:\myusers.xml))
    {
        $colUser = Import-Clixml c:\myusers.xml
    }
    else
    {
        $colUser = @{}
    }
    foreach ($username in $colUser.keys)
    {
        New-Object -TypeName psobject -Property @{
            username=$username
            givenname=$colUser[$username]['givenname']
            surname=$colUser[$username]['surname']
        }
    }
}

function Add-MyUser 
{
    [cmdletbinding(
        SupportsShouldProcess=$true,
        ConfirmImpact='Medium'
    )]
    param([string]$username,[string]$givenname,[string]$surname)
    if ((Test-Path c:\myusers.xml))
    {
        $colUser = Import-Clixml c:\myusers.xml
    }
    else
    {
        $colUSer = @{}
    }
    if ($colUser.ContainsKey($username))
    {
        throw "A user with that name already exists."
    }
    else
    {
        if ($PSCmdlet.ShouldProcess("user $username",'Create user'))
        {
            $colUser[$username] = @{givenname=$givenname;surname=$surname}
            $colUser | Export-Clixml c:\myusers.xml
        }
    }
    
}

function Remove-MyUser 
{
    [cmdletbinding(
        SupportsShouldProcess=$true,
        ConfirmImpact='High'
    )]
    param([string]$username)
    if ((Test-Path c:\myusers.xml))
    {
        $colUser = Import-Clixml c:\myusers.xml
    }
    else
    {
        $colUSer = @{}
    }
    if ($colUser.ContainsKey($username))
    {
        if ($PSCmdlet.ShouldProcess("User $username",'Remove user'))
        {
            $colUser.Remove($username)
            $colUser | Export-Clixml c:\myusers.xml
        }
    }
    else
    {
            throw "A user with that name does not exist."
    }
}