Home > Blockchain >  Powershell function not working if it has to put data in a variable
Powershell function not working if it has to put data in a variable

Time:09-30

I used some functions before in Powershell (mostly copied from the web).

I am working on a project in which data will change a lot and therefore I need to refil variables from time to time and I wanted to use a function for that.

Below is a sample data of what I want to do - I have an array of data.

$data = @(
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service1';Propertyx=1;Price='5'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service2';Propertyx=1;Price='4'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service4';Propertyx=2;Price='4'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service4';Propertyx=2;Price='1'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service2';Propertyx=2;Price='3'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service1';Propertyx=1;Price='17'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service2';Propertyx=1;Price='13'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service3';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service1';Propertyx=1;Price='2'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service1';Propertyx=1;Price='3'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service2';Propertyx=1;Price='11'}
   [pscustomobject]@{Id1='4';Id2=42112521;Service='Service1';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service3';Propertyx=1;Price='5'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service2';Propertyx=1;Price='4'}
   [pscustomobject]@{Id1='4';Id2=42112521;Service='Service2';Propertyx=1;Price='12'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service3';Propertyx=1;Price='8'}
   [pscustomobject]@{Id1='4';Id2=42112521;Service='Service1';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service5';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='4';Id2=42112521;Service='Service3';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service1';Propertyx=1;Price='3'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service2';Propertyx=1;Price='11'}
   [pscustomobject]@{Id1='4';Id2=42112521;Service='Service1';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='2';Id2=11112314;Service='Service3';Propertyx=1;Price='5'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service2';Propertyx=1;Price='4'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service4';Propertyx=1;Price='12'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service5';Propertyx=1;Price='8'}
   [pscustomobject]@{Id1='4';Id2=42112521;Service='Service1';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service5';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='5';Id2=53252352;Service='Service1';Propertyx=1;Price='7'})

   $data

I search for unique data for each property and fill it in variables. Because the data will change during the script I want to make a function that on each imput of data refreshes unique data. Below is my function but it doesn't work. I tested and it works if I don't put data in a variables. Why is that and how can I solve it?

   function func-test
{
$Id1 = @($data | select -expand Id1 -unique | sort)
$Id2 = @($data | select -expand Id2 -unique | sort)
$Service = @($data | select -expand Service -unique | sort)
}

func-test


    function func-test1
{
@($data | select -expand Id1 -unique | sort)
@($data | select -expand Id2 -unique | sort)
@($data | select -expand Service -unique | sort)
}

func-test1

CodePudding user response:

You are using a function but leaving the data inside the function. When you do not use any variables, you can actually see the output from within the function. See my example:

$data = @(
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service1';Propertyx=1;Price='5'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service2';Propertyx=1;Price='4'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service4';Propertyx=2;Price='4'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service5';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='5';Id2=53252352;Service='Service1';Propertyx=1;Price='7'})

function func-test{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$false)]
        $Data,
    
        [Parameter(Mandatory=$false)]
        [ref]
        $Id1,
        
        [Parameter(Mandatory=$false)] 
        [ref] 
        $Id2,

        [Parameter(Mandatory=$false)] 
        [ref] 
        $Service
    )
    $Id1.Value = @($data | select -expand Id1 -unique | sort)
    $Id2.Value = @($data | select -expand Id2 -unique | sort)
    $Service.Value = @($data | select -expand Service -unique | sort)
}

$id1 = New-Object PSCustomObject
$id2 = New-Object PSCustomObject
$service = New-Object PSCustomObject
func-test -Data $data -Id1 ([ref]$id2) -Id2 ([ref]$id1) -Service ([ref]$service)

In the above example the data is returned and now accessible outside the function in the variables: $id1, $id2 and $service.

Another approach would be to leave it outside the function:

$data = @(
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service1';Propertyx=1;Price='5'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service2';Propertyx=1;Price='4'}
   [pscustomobject]@{Id1='1';Id2=51213412;Service='Service4';Propertyx=2;Price='4'}
   [pscustomobject]@{Id1='3';Id2=12512521;Service='Service5';Propertyx=1;Price='7'}
   [pscustomobject]@{Id1='5';Id2=53252352;Service='Service1';Propertyx=1;Price='7'})

$Id1 = @($data | select -expand Id1 -unique | sort)
$Id2 = @($data | select -expand Id2 -unique | sort)
$Service = @($data | select -expand Service -unique | sort)

Hope this makes sense.

CodePudding user response:

Thank you for all the answers.

I want to make a GUI form for searching and entering data. The GUI will have comboboxes. I figured that everytime I write some data in a combobox the combobox dropdown must refresh with new entries, thus I need to refresh all data in saving the form.

The above solutions work for me. The second part of solution was my initial solution, but I wanted to make a function to shorten the script.

  • Related