klarmann

Windows Powershell Auswahldialog

Hi,

ich steh leider vor einem kleien Problem mit der Powershell von Windows. Ich erzeuge ein Menü in der Serverbezeichnungen stehen. Z.B. "Mailserver", "Fileserver", "Fileserver Standort XY" usw... Anhand der Auswahl würde ich gerne ein entsprechendes Netzlaufwerk auf dem ausgewählten Server einbinden. Setze ich Anstelle der Bezeichungen nun den Servernamen ins Menü, klappt dies.

Die Frage ist nun, wie kann ich mit Bezeichnung zum Servernamen assoziieren? Das muss doch möglich sein oder irre ich?

Hier mail eins Auszug:
[void] $objListBox.Items.Add("Fileserver")  
[void] $objListBox.Items.Add("Fileserver2")   
[void] $objListBox.Items.Add("Fileserver3")  

$objForm.Controls.Add($objListBox)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog()

$Drive = "M:" $UNC = "\$x\share"  

$net = New-Object -com WScript.Network; $net.mapnetworkdrive($Drive,$UNC)
$x ist die Auswahl aus dem Dialog. Allerdings müsste ich das in den entprechenden Servernamen umwandeln.

Hat da jemand eine Idee?
Auf Facebook teilen
Auf X (Twitter) teilen
Auf Reddit teilen
Auf Linkedin teilen

Content-ID: 180921

Url: https://administrator.de/forum/windows-powershell-auswahldialog-180921.html

Ausgedruckt am: 22.05.2025 um 12:05 Uhr

Karo
Karo 24.02.2012 um 16:12:42 Uhr
Goto Top
Hoi,

hmmm, alternativ (mit Multi-Auswahl face-wink )?

# Create the form

$objForm = New-Object System.Windows.Forms.Form
$objForm.Width = 300
$objForm.Height = 250 

# Create three ListViewItems

$item1 = New-Object System.Windows.Forms.ListViewItem('SERVER1')  
[void] $item1.SubItems.Add('M:')  
[void] $item1.SubItems.Add('SHARE1')  

$item2 = New-Object System.Windows.Forms.ListViewItem('SERVER2')  
[void] $item2.SubItems.Add('N:')  
[void] $item2.SubItems.Add('SHARE2')  

$item3 = New-Object System.Windows.Forms.ListViewItem('SERVER3')  
[void] $item3.SubItems.Add('O:')  
[void] $item3.SubItems.Add('SHARE3')  


# Create a ListView, set the view to 'Details' and add columns  

$listView = New-Object System.Windows.Forms.ListView
$listView.View = 'Details'  
$listView.Width = 300
$listView.Height = 200

[void] $listView.Columns.Add('NAME', 100)  
[void] $listView.Columns.Add('DRIVE',50)  
[void] $listView.Columns.Add('SHARE',200)  


# Add items to the ListView

$listView.Items.AddRange(($item1, $item2, $item3))

# This is the custom comparer class string
# copied from the MSDN article

$comparerClassString = @"  

  using System;
  using System.Windows.Forms;
  using System.Drawing;
  using System.Collections;

  public class ListViewItemComparer : IComparer
  {
    private int col;
    public ListViewItemComparer()
    {
      col = 0;
    }
    public ListViewItemComparer(int column)
    {
      col = column;
    }
    public int Compare(object x, object y)
    {
      return String.Compare(
        ((ListViewItem)x).SubItems[col].Text, 
        ((ListViewItem)y).SubItems[col].Text);
    }
  }

"@  

# Add the comparer class

Add-Type -TypeDefinition $comparerClassString `
  -ReferencedAssemblies (`
    'System.Windows.Forms', 'System.Drawing')  

# Add the event to the ListView ColumnClick event

$columnClick = 
{
  $listView.ListViewItemSorter = `
    New-Object ListViewItemComparer($_.Column)
}

[void] $listView.Add_ColumnClick($columnClick)

# ADD CANCEL BUTTON
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,200)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"  
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)


# ADD OK BUTTON
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,200)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "MAP"  

$counter=0
[void] $OKButton.Add_Click(
   {
       foreach ($objItem in $listView.SelectedItems)
          {
            $Drive = $objItem.subitems[1].text
            $SERVER = $objItem.subitems.text
            $SHARE = $objItem.subitems[2].text
            $UNC = "\\$Server\$Share"  
            Write-host $DRIVE $UNC
            $net = New-Object -com WScript.Network; $net.mapnetworkdrive($Drive,$UNC)
          }  
        $objForm.Close()
        
   })

$objForm.Controls.Add($OKButton)


# Add the ListView to the form and show the form

[void] $objForm.Controls.Add($listView)
[void] $objForm.ShowDialog()


bye

Karo