0

Consider this json file(TESTKill.json):

{
    "CLT_PROD": {
        "CLRDIR": "C:\\Test",
        "CLREXE": "notepad.exe"
    },
    "CLT_PROD_GHOST": {
        "CLRDIR": "C:\\Kontrolle",
        "CLREXE": "powershell.exe"
    },
    "CLTc_PROD_Test1": {
        "CLRDIR": "D:\\Kontrolle",
        "CLREXE": "powershell.exe"
    },
    "CLT_PROD_Test2_GHOST": {
        "CLRDIR": "F:\\Kontrolle",
        "CLREXE": "powershell.exe"
    },
    "CLT_PROD_Test3_GHOST": {
        "CLRDIR": "J:\\ABU",
        "CLREXE": "powershell.exe"
    }
}

I want to give only the object names(CLT_PROD, CLT_PROD_GHOST, etc.) in a dropdown list in the GUI. I have the following code for this purpose:

$CLRJson = Get-Content -Raw -Path "C:\Temp\TESTKill.json" | ConvertFrom-Json
$CLRObjects = $CLRJson.psobject.Properties.name #select only the object names.

$objDropDownDBSchma = New-Object System.Windows.Forms.ComboBox
$objDropDownDBSchma.Location = New-Object System.Drawing.Size(40,40)
$objDropDownDBSchma.Size = New-Object System.Drawing.Size(200,20)
$objForm.Controls.Add($objDropDownDBSchma)
#Objekte aus der json-Datei in die DropDown Liste hinzufügen
Foreach ($CLRObjekte in $CLRObjects) {
    $objDropDownDBSchma.items.Add($CLRObjekte) 
}

It works till here. I can select one object from the dropdown menu. The thing I want to have now is, after i choosed one object, i want to click a button and as a result, i should be able to get the values of the two variables "CLRDIR" & "CLREXE" of the appropriate object. For instance: if i had chosen the object "CLT_PROD_GHOST", i want to show the the value of those variables of this object in a text box. I don't know, how to script this. Anyone got any Idea ?

Thx for your help:)

1 Answer 1

2

You read the value of the selected item from the dropdown menu. This will give the name of the property you want to access. Then powershell can dynamically access the property from $CLRJson. I don't remember exactly how to access the selected item of the drowdown menu. You have also to register a scriptblock in the OnClick event handler for the Button you speak about that may contain something like this:

$name = $objDropDownDBSchma.SelectedItem.Value
$CLRObj = $CLRJson.$name
Write-Host $CLRObj.CLRDIR
Write-Host $CLRObj.CLREXE
Sign up to request clarification or add additional context in comments.

1 Comment

thank you. This is exactly what i was doing. However, i wrote the variable name wrong. That's why it did not work. Now i got it. Thx man...

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.