ExportedFunctions/New-VCard.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<#
.SYNOPSIS
    Builds a VCard

.DESCRIPTION
    Uses the data passed in as parameters to build a valid VCard string.

.PARAMETER Name
    The name to be put in the "N" VCard field.

.PARAMETER FormattedName
    The name to be put in the "FN" VCard field.

.PARAMETER Nickname
    The name to be put in the "NICKNAME" VCard field.

.PARAMETER Birthday
    The birthday to be put in the "BDAY" VCard field.

.PARAMETER Address
    The address to be put in the "ADR" VCard field.

.PARAMETER Telephone
    The phone number to be put in the "TEL" VCard field.

.PARAMETER Email
    The email address to be put in the "EMAIL" VCard field.
#>

function New-VCard {
    [OutputType([string])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
    param(
        $Name,
        [Parameter(Mandatory=$true)]
        $FormattedName,
        $Nickname,
        $Birthday,
        $Address,
        $Telephone,
        $Email,
        $Title,
        $Organization,
        $Note,
        $Url,
        $Uid,
        $Twitter,
        $Skype,
        $Properties
    )
    process {
        $VCardBuilder = new-object Text.StringBuilder("BEGIN:VCARD`r`nVERSION:4.0`r`n")

        $ParameterMapping = @{
            "Name"="N";
            "FormattedName"="FN";
            "Birthday"="BDAY";
            "Address"="ADR";
            "Telephone"="TEL";
            "Organization"="ORG";
            "Twitter"="X-TWITTER";
            "Skype"="X-SKYPE";
        }

        $PSBoundParameters.Keys | Where-Object {$_ -ne "Properties"} | ForEach-Object {
            if($ParameterMapping.ContainsKey($_)) {
                $Name = $ParameterMapping[$_]
            } else {
                $Name = $_.ToUpper()
            }
            $Value = $PSBoundParameters[$_]
            $VCardBuilder.AppendLine( ("{0}:{1}" -f $Name, $Value) ) | Out-Null
        }
        if($Properties) {
            $Properties.Keys | ForEach-Object {
                $VcardBuilder.AppendLine( ("{0}:{1}" -f $_, $Properties[$_]) ) | Out-Null
            }
        }
        $VCardBuilder.Append("END:VCARD") | Out-Null
        $VCardBuilder.ToString()
    }
}
Export-ModuleMember -Function New-VCard