locked
type case json key value to PSobject RRS feed

  • Question

  • I have json like saved as a.json

    "p":{
    cred:"$cred",
    env:"$env:name",
    hash:[]
    }
    
    

    I want to convert the values into poweshell objects to pass into function further but getting error on type casting 

    $b= "<<a.json path>>"

    $c= get-content $b | convertfrom-json 

    $c | convertTo-json

    $d=[pscredential].p[0].Cred

    getting error can not convert system.string into system.automation.pscredential, I have hash table and other powershell custom objects that I want to type cast, getting PSON information but its not helping here

    Thursday, June 7, 2018 2:08 PM

All replies

  • Your Json is attempting the impossible.  You cannot store a cred object in Json.

    Here is how to create a credential object:

    [pscredential]::New('username','secure string')


    \_(ツ)_/

    Thursday, June 7, 2018 2:20 PM
  • function var { [cmdletbindings] param( [sting] $ss="M:\mypath", [hash] $hashtable=@{}, [path]$mypath, [string]folder, [sting]$fullpath= join-path $ss -childpath $folder [string]$branch ='master' ) $a={ ss=$($ss) fullpath=$($fullpath) hashtable=$($hashtable) branch=$($branch)

    mypath=$($mypath) } return $a }

    In case when I try to pass $a.branch say in below command 
    git pull origin '$a.branch' >>*  $a.fullpath (its not working, without '' quotes also gives err)[gives exception and exit]
    In general if I declare varible as $name='master' locally and pass in same command then its working fine
    I observed when I convert any variable in PSobject or PScustomobject and even using out-string to convert into string and pass its not getting executed at all while passing any function

    example 2

    function var{
    [cmdletbindings]
    param(
    
    [sting] $ss="M:\mypath",
    [hash] $hashtable=@{},
    [path]$mypath,
    [string]folder,
    [sting]$fullpath= join-path $ss -childpath $folder 
    [string]$branch ='master'
    )
    
    return $hashtable,$branch,$fullpath
    }

    $m=var
    $m[0], $m[1], m[2] -- I can get params in array but how to get them more readable  
    like instead 0,1,2 I want something like $m['hastable'], m['branch'] or $hash=$m[0] like this in output

    here I create a module and now I want to declare all variable in single file .json or .ps1 or any text file and want to import in my individual script but getting type casting error so doing above exercise  



    Friday, June 8, 2018 6:04 PM