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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Arguments passed to PowerShell commands should be separated by spaces, not commas. Change `MyLogger($mystr, $LogFile, $dir)` to `MyLogger $mystr $LogFile $dir` – Mathias R. Jessen Jan 28 '20 at 12:40
  • Take a look here [Parameters in Functions PS](https://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell) – DNKO Jan 28 '20 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 – andreapavia Jan 28 '20 at 12:56

1 Answers1

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**************"
f6a4
  • 1,684
  • 1
  • 10
  • 13