0
votes

I want to create a script to create VDIs. Basically it's ready, but now I want to modify it to work with gridviews to make it more usable.

My goal is, that you may get a gridview like this code:

$id = 1

$table = @( @{'ID'=$id ++; 'VM Name'="VM0001"; 'Assigned User'=$null},
            @{'ID'=$id ++; 'VM Name'="VM0002"; 'Assigned User'=$null},
            @{'ID'=$id ++; 'VM Name'="VM0003"; 'Assigned User'=$null} )

$out = $null
$out = $table.ForEach({[PSCustomObject]$_}) |
       Select ID, 'VM Name', 'Assigned User' |
       Out-GridView -Title "VM Creator" -OutputMode Single -OutVariable selectedID

if ($selectedID -eq $null -or $out.Count -eq 0) {
    Write-Host -BackgroundColor Black -ForegroundColor Red "Error: nothing is choosed"
}

if ($selectedID.'Assigned User' -eq $null) {
    Write-Host -BackgroundColor Black -ForegroundColor Green "Debug: Input UserName for Desktop" $selectedID.'VM Name'
    $newUser = Read-Host "Input UserName"
}

Now when you select an entry without a assigned User you should be able to add a name into the hashtable.

When I tried this:

$table.Add("123", "VM0004", "Pete")

I got the following error (in German):

Für "Add" und die folgende Argumenteanzahl kann keine Überladung gefunden
werden: "3".
In Zeile:8 Zeichen:1
+ $table.Add("123", "VM0004", "Pete")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
1
And your question is?Ansgar Wiechers
I want to add a User Accout Into tue hashtable. When you Select an entry I want to modify a Value. I am Not able to add or modify values Into the hastableChristian Cornelsen
Unsurprising, since your code doesn't even try. What have you attempted to make it work, and how did your attempts fail? What was the actual result? Did you get errors?Ansgar Wiechers
Basicly it's very simple. I want to modify the content of the Hashtable. But I am not able to modify or add content to the hastable on the top. Can anyone show me a sample code how to perform this "basic" task? :)Christian Cornelsen
What. Have. You. Tried?Ansgar Wiechers

1 Answers

0
votes

Your $table is an array of hashtables, so you need to append another hashtable:

$table += @{'ID'="123"; 'VM Name'="VM0004"; 'Assigned User'="Pete"}

The Add() method can't be used here, because you can't append to a fixed size array (+= works because it doesn't actually append to the array but creates a new array with increased size, copies the elements from the existing array, puts the new value(s) in the free slots, then assigns the new array to the variable).

To be able to modify values of selected rows I'd suggest to use an array of custom objects right away (you're creating them for the gridview anyway).

$table = [PSCustomObject]@{'ID'=$id++; 'VM Name'="VM0001"; 'Assigned User'=$null},
         [PSCustomObject]@{'ID'=$id++; 'VM Name'="VM0002"; 'Assigned User'=$null},
         [PSCustomObject]@{'ID'=$id++; 'VM Name'="VM0003"; 'Assigned User'=$null}

With that you can update values of a selected row like this:

$selected = $table | Out-GridView -PassThru

$selected | ForEach-Object {
    $_.'Assigned User' = 'Chris'
}