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.

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Nicely done; note that PowerShell (Core) 7+ 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.

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.