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 'sdasch@gmail.com' -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 'sdasch@gmail.com' -SqlServerInstanceName localhost -SqlDatabaseName MyDatabase -Load:$True -ErrorAction Continue;

Here Load is a Switch Parameter.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Roshan Nuvvula
  • 803
  • 1
  • 8
  • 28

1 Answers1

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 'sdasch@gmail.com' -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
Community
  • 1
  • 1
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172