2

I have a PowerShell script which has couple of parameters including a switch parameter.

The below command executed within PowerShell works as expected:

  \\localhost\Test\Code\DataPullResults.ps1 -TestName 'Test survey' -QUser '[email protected]' -SqlServerInstanceName localhost -SqlDatabaseName MyDatabase -Load:$True -ErrorAction Continue;

But when I ran the same command from command prompt I get an error:

\\localhost\Test\Code\DataPullResults.ps1 : Cannot process
argument transformation on parameter 'FullLoad'. Cannot convert value
"System.String" to type "System.Management.Automation.SwitchParameter".
Boolean parameters accept only Boolean values and numbers, such as $True,
$False, 1 or 0.

Below is the command which I have used to execute from the command prompt:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe powershell.exe -ExecutionPolicy ByPass -File \\localhost\Test\Code\DataPullResults.ps1 -TestName 'Test survey' -QUser '[email protected]' -SqlServerInstanceName localhost -SqlDatabaseName MyDatabase -Load:$True -ErrorAction Continue;

Here Load is a Switch Parameter.

1 Answer 1

1

powershell.exe does not fully evaluate script arguments when the -File parameter is used (source). So you should use -command instead:

powershell.exe -Command \\localhost\Test\Code\DataPullResults.ps1 -TestName 'Test survey' -QUser '[email protected]' -SqlServerInstanceName localhost -SqlDatabaseName MyDatabase -Load:$True -ErrorAction Continue;

But beside that, note that you don't have to pass a boolean to a switch value. That means, if you omit -Load, $load will be set to $false. And if you pass -Load (without $true), it will get set to $true.

Aslo, did you noticed, that you are calling powershell.exe twice?

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe powershell.exe
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I have used Posershell.exe twice, Which is working fine for another Scripts.
I have tried the -Command Instead of -File, Though getting same error. Any Other help
How about omitting the $true value?
I came across this problem and omitting the :$True works. In my case I was including it in a (probably unnecessary) attempt to guard against inadvertently binding the next param name to the switch param's value. A bit like as described at stackoverflow.com/a/8526355/1754517

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.