0

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

3
  • Arguments passed to PowerShell commands should be separated by spaces, not commas. Change MyLogger($mystr, $LogFile, $dir) to MyLogger $mystr $LogFile $dir Commented Jan 28, 2020 at 12:40
  • Take a look here Parameters in Functions PS Commented Jan 28, 2020 at 12:47
  • Mathias R. Jessen - Thank you so much, didn't know that, it's my first script ever and i have to end it this evening so i just copied from the internet, i don't have time to study it... Thank you so much Commented Jan 28, 2020 at 12:56

1 Answer 1

1

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**************"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.