Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Generate a Function Script

$
0
0

OK, something along these lines should get you started:

[CmdletBinding()]
param (
    [string]
    $Path = '.',

    [Parameter(Mandatory = $true)]
    [string]
    $UserName,

    [Parameter(Mandatory = $true)]
    [string]
    $FirstName,

    [Parameter(Mandatory = $true)]
    [string]
    $LastName,

    [Parameter(Mandatory = $true)]
    [string]
    $PhoneNumber,

    [Parameter(Mandatory = $true)]
    [string]
    $City,

    [Parameter(Mandatory = $true)]
    [string]
    $State,

    [Parameter(Mandatory = $true)]
    [string]
    $Zip
)

$targetFolder = Join-Path -Path $Path -ChildPath $UserName

if (Test-Path -LiteralPath $targetFolder)
{
    throw "'$targetFolder' already exists."
}

$null = New-Item -Path $targetFolder -ItemType Directory -ErrorAction Stop

$text = @"
Name: $FirstName $LastName
Phone: $PhoneNumber
City: $City
State: $State
Zip: $Zip
"@

$fileName = Join-Path -Path $targetFolder -ChildPath 'TextFile.txt'

$text | Out-File -LiteralPath $fileName -ErrorAction Stop

I made all of the contact information parameters mandatory, but you can change that if you’d like. Calling the script would look something like this:

.\ScriptName.ps1 -Path $home\Documents\ContactInfo -UserName SomeUser -FirstName Dave -LastName Wyatt -PhoneNumber 555-555-5555 -City Nowhere -State Michigan -Zip 12345

Viewing all articles
Browse latest Browse all 13067

Trending Articles