0

To start off, I'm just going to paste the result of $PSVersionTable.PSVersion as, apparantly, it shows the version of powershell...

You are now entering PowerShell : <name> PS D:\aDirectory> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 4 0 -1 -1 

Below I have a function, DoSomeSqlForMe, which defines two parameters, $sql and $logFile.

When function is called, $sql is passed into it. $logFile is inherited from a parent script (but may also be passed in).

function DoSomeSqlForMe { param ( [string]$sql, [string]$logFile ) #Do some stuff with $sql and $logfile } 

EDIT - function is being called as below (this is a direct copy + typical of other calls to same function):

DoSomeSqlForMe $sql 

My issue is when $logFile is set as a parameter but not passed in when method is called ($logFile is still available as an inherited var) I get Exception message : The argument is null or empty.

However when I simply remove $logFile as a parameter, my log file is filled with what looks to me (uneducated in eastern languages) like Mandarin - 渮浡 repeated over and over again.

Any ideas?

4
  • First of all is the missing comma between $sql and [string] a copy paste error? And can you please show how you call your function? Commented Feb 9, 2018 at 10:48
  • Hi rufer7, yes comma was a copy paste error apologies. Added method call to question. Commented Feb 9, 2018 at 10:50
  • You do not pass anything to $logFile so it will have default value (not the value from outer scope). BTW you define DoSomeSqlForMe but calling DoSql. Commented Feb 9, 2018 at 10:54
  • Apologies, rufer7 kindly edited for me. Commented Feb 9, 2018 at 11:01

1 Answer 1

1

The problem is, that if you don't provide $logFile parameter when calling the function (i.e. DoSomeSqlForMe $arbitrarySql) by default $null gets assigned to $logFile parameter. To assign the before defined $logFile variable by default to the function parameter you have to adjust your function as follows.

function DoSomeSqlForMe { param ( [string]$sql, [string]$logFile = $logFile ) #Do some stuff with $sql and $logfile } 

However I suggest to instead call your function with two parameters (i.e. DoSomeSqlForMe -sql $arbitrarySql -logFile $logFile)

Sign up to request clarification or add additional context in comments.

6 Comments

This worked, thanks. This job is currently running every night on a Win2003 server with the code above - and it works. We are migrating to Win2012 and it doesn't work :) I guess this could be a powershell version issue? I do not have access to the old production server to check unfortunately.
If I do [string]$logFile = $logFile it works ok, however as I'm sure you know it limits what can be passed in. When I pass in $logFile I get the lines of mandarin though :(
What do you mean by "the code above"? The code you provided or mine? Concerning limiting I don't understand what you mean.
The code I provided is currently running in production on a Win2003 server.
@xeon48 If my answer correctly answered your question please rate it mark it as answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.