user3235631
user3235631

Reputation: 79

Windows PowerShell invoking function with parameters

I am totally new to PowerShell, and trying to write a simple script to produce log file. I searched forums and could not find the answer for my question. I found the example in the net, that I thought would be useful, and applied it to my script:

## Get current date and time. In return, you’ll get back something similar to this: Sat January 25 10:07:25 2014
$curDateTime = Get-Date
$logDate = Get-Date -format "MM-dd-yyyy HH:mm:ss"
$LogPath = "C:\Temp\Log"
$LogName = "log_file_" + $logDate + ".log"
$sFullPath = $LogPath + "\" + $LogName

<#
    param(
    ## The path to individual location files
    $Path,

    ## The target path of the merged file
    $Destination, 

    ## Log path
    $LogPath,

    ## Log name   
    $LogName

    ## Full LogFile Path
    ## $sFullPath = $LogPath + "\" + $LogName
)
#>

Function Log-Start {
    <#
    .SYNOPSIS
        Creates log file
    .DESCRIPTION
        Creates log file with path and name that is passed. 
        Once created, writes initial logging data
    .PARAMETER LogPath
        Mandatory. Path of where log is to be created. Example: C:\Windows\Temp
    .PARAMETER LogName
        Mandatory. Name of log file to be created. Example: Test_Script.log
    .INPUTS
        Parameters above
    .OUTPUTS
        Log file created
    #>

    [CmdletBinding()]
    Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LogName)

    Process {        
        ## $sFullPath = $LogPath + "\" + $LogName

        # Create file and start logging
        New-Item -Path $LogPath -Value $LogName –ItemType File

        Add-Content -Path $sFullPath -Value "***************************************************************************************************"
        Add-Content -Path $sFullPath -Value "Started processing at [$([DateTime]::Now)]."
        Add-Content -Path $sFullPath -Value "***************************************************************************************************"
        Add-Content -Path $sFullPath -Value ""
    }       
}

Set-StrictMode -Version "Latest"

Log-Start
....

The question is how can I make the Log_Start function to use variables I assigned in the beginning of the script, or it is not possible with declaration of [CmdletBinding()] and function itself. If I try to run it the way it is coded it is prompting me to enter the path and logname, I thought it should have used what I already defined. Apparently I am missing the concept. I surely can just assign the values I need right in the param declaration for the function, but I am planning to use couple of more functions like log-write and log-finish,and would not want to duplicate the same values. What am I missing?

Upvotes: 0

Views: 1377

Answers (2)

Raf
Raf

Reputation: 10097

If you want to include settings from a config file, one approach is hashtables. Your config file would look like this:

logpath=c:\somepath\
server=server.domain

Then your script would have an extra var pointing to a config file and a function to import it:

$configFile = "c:\some.config";  
function GetConfig(){  
    $tempConfig = @{};  
    $configLines = cat $configFile -ErrorAction Stop;  
    foreach($line in $configLines){         
        $lineArray = $line -split "=";          
        $tempConfig.Add($lineArray[0].Trim(), $lineArray[1].Trim());        
    }  
    return $tempConfig;
}  
$config = GetConfig  

You can then assign config values to variables:

$LogPath = $conifg.Item("logpath")  
$server = $conifg.Item("server")  

Or use them access directly

$conifg.Item("server")  

Upvotes: 0

Raf
Raf

Reputation: 10097

You defined your custom parameters at the top of your script and now you must pass them them to the function by changing

Log-Start

line to read

Log-Start $LogPath $LogName

Though you would be better off naming your parameters differently to avoid confussion. You don't really need CmdletBinding() declaration unless you plan to utilise common parameters like -Verbose or -Debug with your function so you could get rid of the following 2 lines:

[CmdletBinding()]
Param ([Parameter(Mandatory=$true)][string]$LogPath, [Parameter(Mandatory=$true)][string]$LogName)

and your script would still work.

Upvotes: 1

Related Questions