PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
43.56k stars 7.06k forks source link

Bool Parameter Value Not Imported Correctly by Import-Csv Cmdlet #21606

Closed yotsuda closed 1 week ago

yotsuda commented 2 weeks ago

Prerequisites

Steps to reproduce

In cmdlet implementation with c#, following code doesn't work as expected.

    [Cmdlet(VerbsCommon.New, "MyItem")]
    public class NewMyItemCmdlet : Cmdlet
    {
        [Parameter(ValueFromPipelineByPropertyName = true)]
        public string? Label { get; set; }

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool BoolValue { get; set; }

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public bool? NullableBoolValue { get; set; }

        [Parameter(ValueFromPipelineByPropertyName = true)]
        public SwitchParameter SwitchParamValue { get; set; }

        protected override void ProcessRecord()
        {
            WriteWarning($"Label: {Label}  BoolValue: {BoolValue}  NullableBoolValue: {NullableBoolValue}  SwitchParamValue: {SwitchParamValue}");
        }
    }

Sample input.csv:

Label,BoolValue,NullableBoolValue,SwitchParamValue
#1 $false,$false,$false,$false
#2 $true,$true,$true,$true
#3 false,false,false,false
#4 true,true,true,true
#5 FALSE,FALSE,FALSE,FALSE
#6 TRUE,TRUE,TRUE,TRUE

Run it as follows: PS> Import-Csv input.csv | New-MyItem

Expected behavior

PS> Import-Csv input.csv | New-MyItem
WARNING: Label: #1 $false  BoolValue: False  NullableBoolValue:   SwitchParamValue: False
WARNING: Label: #2 $true  BoolValue: True  NullableBoolValue: True  SwitchParamValue: True
WARNING: Label: #3 false  BoolValue: False  NullableBoolValue: False  SwitchParamValue: False
WARNING: Label: #4 true  BoolValue: True  NullableBoolValue: True  SwitchParamValue: True
WARNING: Label: #5 FALSE  BoolValue: False  NullableBoolValue: False  SwitchParamValue: False
WARNING: Label: #6 TRUE  BoolValue: True  NullableBoolValue: True  SwitchParamValue: True

Actual behavior

PS> Import-Csv input.csv | New-MyItem
WARNING: Label: #1 $false  BoolValue: False  NullableBoolValue:   SwitchParamValue: False
WARNING: Label: #2 $true  BoolValue: False  NullableBoolValue:   SwitchParamValue: False
WARNING: Label: #3 false  BoolValue: False  NullableBoolValue:   SwitchParamValue: False
WARNING: Label: #4 true  BoolValue: False  NullableBoolValue:   SwitchParamValue: False
WARNING: Label: #5 FALSE  BoolValue: False  NullableBoolValue:   SwitchParamValue: False
WARNING: Label: #6 TRUE  BoolValue: False  NullableBoolValue:   SwitchParamValue: False

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

rhubarb-geek-nz commented 2 weeks ago

The output from Import-CSV has every field as a string field. For example "$True" is just a string, not a boolean.

Look at output with

PS> Import-Csv input.csv | ConvertTo-JSON
[
  {
    "Label": "$false",
    "BoolValue": "$false",
    "NullableBoolValue": "$false",
    "SwitchParamValue": "$false"
  },
  {
    "Label": "$true",
    "BoolValue": "$true",
    "NullableBoolValue": "$true",
    "SwitchParamValue": "$true"
  },
...etc...

If you want a data file that keeps its PowerShell types then look at Import-PowerShellDataFile

yotsuda commented 2 weeks ago

@rhubarb-geek-nz Thank you for your prompt response. I apologize for the incorrect report. I will refer to the information you provided. Thanks.

kilasuit commented 2 weeks ago

@yotsuda are you happy for this to closed and labeled as resolution-answered?

yotsuda commented 2 weeks ago

@kilasuit Yes, I'm happy for this to be closed and labeled as resolution-answered. Thank you for your assistance!

microsoft-github-policy-service[bot] commented 1 week ago

📣 Hey @yotsuda, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

Microsoft Forms