2

I'm trying to run a PowerShell Script from a .cmd File. Here are the script parameters I've defined:

Param(
    [string]$Customer,
    [string]$EntryPointINT,
    [string]$EntryPointPRD,
    [string]$EntryPointVDI,
    [string]$LicenseServer,
    [bool]$onlyVDI,
    [bool]$hasVDI,
    [int]$insertHelper
)

So as you see, onlyVDI and hasVDI are from the type bool. So far so good. When I start the script from the PowerShell console I can do the following thing (changed customername and the servername here):

.\kpi.ps1 -Customer "CustName" -EntryPointVDI "servername" -onlyVDI 0 -hasVDI 1 -insertHelper 1

This works perfectly as intended. So now I want to start the same script from a .cmd file. It looks like the following:

powershell.exe -File %~dp0%kpi.ps1 -Customer "Custname" -EntryPointVDI "servername" -LicenseServer "sc005019" -onlyVDI 0 -hasVDI 0 -insertHelper 1

This results in the following error:

C:\***\***\***\kpi.ps1 : Cannot process argument transformation on parameter
'onlyVDI'. Cannot convert value "System.String" to type "System.Boolean".
Boolean parameters accept only Boolean values and numbers, such as $True,
$False, 1 or 0.
    + CategoryInfo          : InvalidData: (:) [kpi.ps1], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,cop_kpi.ps1

Can someone explain me why the 0 on onlyVDI is a string? I've tried everything, tried to replace it with $False ... can't get it to work. Right now I've defined it as a string in the PowerShell Script and pass it as a string from the cmd file, it works as a workaround, but it isn't clean. I would like to get it running with the [bool] datatype.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Twinfriends
  • 1,972
  • 1
  • 14
  • 34

1 Answers1

4

In CMD basically everything is a string. CMD particularly doesn't recognize PowerShell builtin constants like $true and $false. Since there is no automatic conversion of the strings '0', '1', '$true', or '$false' to a boolean value your code errors out when you call it from CMD/batch.

A better approach to handling boolean arguments would be to define them as switch parameters:

Param(
    [string]$Customer,
    [string]$EntryPointINT,
    [string]$EntryPointPRD,
    [string]$EntryPointVDI,
    [string]$LicenseServer,

    [switch]$onlyVDI,
    [switch]$hasVDI,

    [int]$insertHelper
)

That way you can set them to true or false by passing or omitting the parameter:

powershell.exe -File %~dp0%kpi.ps1 -Customer "foo" -EntryPointVDI "bar" -hasVDI
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Nicely done; note that [_PowerShell (Core) 7+_](https://github.com/PowerShell/PowerShell/blob/master/README.md) now _does_ support Boolean literals in CLI calls with `-File`, namely the following verbatim values: `$true` / `true` and `$false` / `false`. But, as your answer shows, with `[switch]` parameters passing Booleans is normally not necessary. – mklement0 Jun 29 '23 at 14:36