locked
Add string and int on psobject VALUE RRS feed

  • Question

  • Hi,

    i've this code, i want to add a value on a psobject

    The value have to be composed from: $CurrentTape +  'These are last days'

    give me this error:
    
    + CategoryInfo          : InvalidArgument: (:) [Add-Member], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddMemberCommand

    [int]$CurrentTape = 12
    
    $MyObject = New-Object -TypeName PSObject
    Add-Member -InputObject $MyObject -Type NoteProperty -Name 'FIELD-1' -Value ($CurrentTape.DaysUsed + 'These are last days')


    Sunday, September 3, 2017 2:28 PM

Answers

  • Or just use quotes:

    "$CurrentTape hello world"

    P.S. - ToString(0 is a Net method and not a C# method.  It can be called from any CLR language.


    \_(ツ)_/

    • Marked as answer by Ramses147 Sunday, September 3, 2017 11:01 PM
    Sunday, September 3, 2017 8:17 PM

All replies

  • $CurrentTape.DaysUsed  does not exist. $CurrentTape is an [int].


    \_(ツ)_/

    Sunday, September 3, 2017 6:36 PM
  • My fault

    [int]$CurrentTape = 12
    
    $MyObject = New-Object -TypeName PSObject
    Add-Member -InputObject $MyObject -Type NoteProperty -Name 'FIELD-1' -Value ($CurrentTape + 'These are last days')
    The velue result have to be: 12 These are last days

    Sunday, September 3, 2017 7:09 PM
  • Hi,

    To concatenate int and string you have to used the c# .ToString() method. Your code should be :

    [int]$CurrentTape = 12
    
    $MyObject = New-Object -TypeName PSObject
    Add-Member -InputObject $MyObject -Name 'FIELD-1' -Value ($CurrentTape.ToString() + ' These are last days') -MemberType NoteProperty

    Hope it helps,


    The key of learning is practice.

    Sunday, September 3, 2017 7:34 PM
  • Or just use quotes:

    "$CurrentTape hello world"

    P.S. - ToString(0 is a Net method and not a C# method.  It can be called from any CLR language.


    \_(ツ)_/

    • Marked as answer by Ramses147 Sunday, September 3, 2017 11:01 PM
    Sunday, September 3, 2017 8:17 PM
  • Sure. Sorry for being not exactly correct. It was a language abuse.

    The key of learning is practice.

    Monday, September 4, 2017 8:45 AM
  • Sure. Sorry for being not exactly correct. It was a language abuse.

    The key of learning is practice.

    You weren't wrong.  I was just clarifying a bit and posting one of the other ways to do this in PowerShell.


    \_(ツ)_/

    Monday, September 4, 2017 8:50 AM