andreapavia
andreapavia

Reputation: 1

powershell function: why more values are returned?

Good Morning everybody, i'm writing my firts ps script: a function with 3 input string and a returned output string but something sound strange for me: this is my code:

function MyLogger($MyString, $MyFileName, $MyFilePath)
{

    $FullLogFile = ($MyFilePath +"\"+ $MyFileName)
    $DateForLog =  Get-Date -Format "yyyy-MM-dd HH:mm:ss" 
    $LogRow = ($DateForLog + " " + $MyString)
    return $LogRow
}

$LogFile = "logname.txt"

$dir = "C:\psscript\workingwithfiles"

$mystr = "stringgggg"

$ret = MyLogger($mystr, $LogFile, $dir)

Write-Host "***************print function res**************"

Write-Host $ret

Write-Host "***************end print function res**************"

why function output (return $LogRow) is this:

2020-01-28 13:11:03 stringgggg logname.txt C:\psscript\workingwithfiles

and not only this?

2020-01-28 13:11:03 stringgggg 

Full output:

***************print function res**************

2020-01-28 13:11:03 stringgggg logname.txt C:\psscript\workingwithfiles

***************end print function res**************

Thank's everybody

Upvotes: 0

Views: 40

Answers (1)

f6a4
f6a4

Reputation: 1782

Better use PowerShell syntax:

function MyLogger
{
    param(  [string]$MyString, 
            [string]$MyFileName, 
            [string]$MyFilePath)


    $FullLogFile = ($MyFilePath +"\"+ $MyFileName)
    $DateForLog =  Get-Date -Format "yyyy-MM-dd HH:mm:ss" 
    $LogRow = ($DateForLog + " " + $MyString)
    return $LogRow
}

$LogFile = "logname.txt"

$dir = "C:\psscript\workingwithfiles"

$mystr = "stringgggg"

$ret = MyLogger -MyString $mystr -MyFileName $LogFile -MyFilePath $dir

Write-Host "***************print function res**************"

Write-Host $ret

Write-Host "***************end print function res**************"

Upvotes: 1

Related Questions