clever38
Goto Top

PowerShell Ausgabe der Variabel Zuweisung stimmt nicht. Warum ?

Hallo zusammen,

bin kein großer Script Schreiber, aber hier habe ich eine Problem wo ich nicht weiter komme.
Ich möchte mir alle Aktiven Angemeldeten User aus dem TS Anzeigen lassen.
Das Funktioniert soweit auch als Command Befehl nur wenn ich das Ergebnis in eine Variabel Schreibe., kommt nur noch „Müll“ raus:

hier der Code: (Get-TSSession = ist Zusatz Module zu PowerShell STerminalServices )

$all=Get-Content Z:\Datensicherung\TS-User-Suche\Server.txt | ForEach-Object { Get-TSSession -State Active -ComputerName $_ | Where-Object { $_.UserName -like $suche } | ft -AutoSize }

So soll es aussehen:

Server SessionID State IPAddress ClientName WindowStationName UserName
--------- ----- --------- ---------- ----------------- --------
Server15 6 Active 192.168.1.112 PC1055 RDP-Tcp#832 Dom \User1
Server15 7 Active 192.168.178.28 ARBEITSPLATZ-1 RDP-Tcp#833 Dom \User2
Server15 3 Active 192.168.0.34 HUKPC04 RDP-Tcp#838 Dom \User3
Server15 10 Active 192.168.1.101 C011 RDP-Tcp#839 Dom \user4
Server15 2 Active 192.168.2.100 GUSTAVO RDP-Tcp#843 Dom \User5
Server15 1 Active 192.168.178.20 LUMPI-PC RDP-Tcp#845 Dom \User6
Server15 9 Active 127.0.0.1 ABOIS RDP-Tcp#848 Dom\User7

Als Variabel sieht es so aus:
Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData

Danke für die Hilfe.

Content-Key: 228281

Url: https://administrator.de/contentid/228281

Printed on: April 25, 2024 at 15:04 o'clock

Member: colinardo
colinardo Jan 31, 2014 updated at 13:27:37 (UTC)
Goto Top
Hallo clever38,
du wandelst die Objekte mit ft -Autosize innerhalb der Schleife bereits in einen Formatierten String um anstatt die Objekte => Objekte bleiben zu lassen.
$all = Get-Content "Z:\Datensicherung\TS-User-Suche\Server.txt" | ForEach-Object {Get-TSSession -State Active -ComputerName $_ | Where-Object { $_.UserName -like $suche }}  
Grüße Uwe
Member: clever38
clever38 Jan 31, 2014 at 13:27:35 (UTC)
Goto Top
Hallo Uwe,
Danke für die Hilfe, aber leider bekomme ich noch immer den gleichen „Müll“ wie vorher auch.

Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData

Gruß
Clever38
Member: colinardo
colinardo Jan 31, 2014 updated at 13:42:07 (UTC)
Goto Top
kannst du mal bitte einen größeren Ausschnitt aus deinem Code posten..
Zur Info: ich hatte inzwischen das ft -Autosize am Ende meiner Codezeile noch entfernt.

Wie gibst du die Variable denn überhaupt aus, dafür reicht ein:
$all
Member: clever38
clever38 Jan 31, 2014 at 13:41:46 (UTC)
Goto Top
Hallo Uwe,
hier der ganze Code face-smile

#region ScriptForm Designer

#region Constructor

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#endregion

#region Post-Constructor Custom Code

#endregion

#region Form Creation
#Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
#When working with the ScriptForm designer this region and any changes within may be overwritten.
#~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(1072, 681)
$Form1.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$Form1.Text = "Form1"
#~~< Ausgabe >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Ausgabe = New-Object System.Windows.Forms.Label
$Ausgabe.Location = New-Object System.Drawing.Point(15, 69)
$Ausgabe.Size = New-Object System.Drawing.Size(1045, 564)
$Ausgabe.TabIndex = 4
$Ausgabe.Text = "Ausgabe"
#~~< Eingabe >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Eingabe = New-Object System.Windows.Forms.TextBox
$Eingabe.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 9.75, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Eingabe.Location = New-Object System.Drawing.Point(12, 35)
$Eingabe.Size = New-Object System.Drawing.Size(264, 22)
$Eingabe.TabIndex = 3
$Eingabe.Text = ""
#~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif", 12.0, [System.Drawing.FontStyle]::Bold, [System.Drawing.GraphicsUnit]::Point, ([System.Byte](0)))
$Label1.Location = New-Object System.Drawing.Point(12, 9)
$Label1.Size = New-Object System.Drawing.Size(243, 23)
$Label1.TabIndex = 2
$Label1.Text = "Welchen User Suchen Sie :"
#~~< Cancel >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Cancel = New-Object System.Windows.Forms.Button
$Cancel.Location = New-Object System.Drawing.Point(106, 646)
$Cancel.Size = New-Object System.Drawing.Size(75, 23)
$Cancel.TabIndex = 1
$Cancel.Text = "Cancel"
$Cancel.UseVisualStyleBackColor = $true
$Cancel.add_MouseClick({CancelMouseClick($Cancel)})
#~~< OK >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$OK = New-Object System.Windows.Forms.Button
$OK.Location = New-Object System.Drawing.Point(12, 646)
$OK.Size = New-Object System.Drawing.Size(75, 23)
$OK.TabIndex = 0
$OK.Text = "OK"
$OK.UseVisualStyleBackColor = $true
$OK.add_MouseClick({OKMouseClick($OK)})
$Form1.Controls.Add($Ausgabe)
$Form1.Controls.Add($Eingabe)
$Form1.Controls.Add($Label1)
$Form1.Controls.Add($Cancel)
$Form1.Controls.Add($OK)

#endregion

#region Custom Code

#endregion

#region Event Loop

function Main{
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.Application]::Run($Form1)
}

#endregion

#endregion

#region Event Handlers

function CancelMouseClick( $object ){
$Form1.Close()
}

function OKMouseClick( $object ){
$top=$Eingabe.Text
#Read-Host "Eingabe" $top
$suche = "*" + $top + "*"
$all = Get-Content "Z:\Datensicherung\TS-User-Suche\Server.txt" | ForEach-Object {Get-TSSession -State Active -ComputerName $_ | Where-Object { $_.UserName -like $suche }} | ft -AutoSize
$Ausgabe.Text=$all

}

Main #This call must remain below all other event functions

#endregion
Member: colinardo
colinardo Jan 31, 2014 updated at 13:50:15 (UTC)
Goto Top
Ahhh, alles klar, das hättest du erwähnen müssen, dass du die Ausgabe in ein Steuerelement übergeben willst.
Dazu musst du das ganze in einen String wandeln:
$all = Get-Content "Z:\Datensicherung\TS-User-Suche\Server.txt" | ForEach-Object {Get-TSSession -State Active -ComputerName $_ | Where-Object { $_.UserName -like $suche }} | ft -AutoSize | out-string  
$Ausgabe.Text=$all
Member: clever38
clever38 Jan 31, 2014 at 14:01:12 (UTC)
Goto Top
Hallo Uwe,
Super hat Funktioniert, Danke noch mal für die Hilfe

GELÖST !

G.
Clever 38
Member: colinardo
colinardo Jan 31, 2014 updated at 14:46:37 (UTC)
Goto Top
p.s. wenn du sowieso schon eine Form benutzt würde ich für die Darstellung der Daten ein Datagridview anstatt eines Labels hernehmen. Sieht meines Erachtens übersichtlicher aus

Beispiel wie man Objekte in ein DataGridView lädt (Zeile 10 -23):
function GenerateForm {
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null  
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null  

$form1 = New-Object System.Windows.Forms.Form
$dg1 = New-Object System.Windows.Forms.DataGridView
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState


$handler_form1_Load= 
{
   # Liste der Dienste holen
   $data = get-service

   # ArrayList erstellen
   $arrList = New-Object System.Collections.ArrayList

   # der Array-Liste das Objekt hinzufügen
   $arrList.AddRange($data)

   # Die ArrayList der DataSource-Eigenschaft des DataGridView zuweisen
   $dg1.DataSource = $arrList
}


$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 315
$System_Drawing_Size.Width = 412
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"  
$form1.Text = "Primal Form"  
$form1.add_Load($handler_form1_Load)

$dg1.Anchor = 15
$dg1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$dg1.Location = $System_Drawing_Point
$dg1.Name = "dg1"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 291
$System_Drawing_Size.Width = 388
$dg1.Size = $System_Drawing_Size
$dg1.TabIndex = 0

$form1.Controls.Add($dg1)
$InitialFormWindowState = $form1.WindowState
$form1.add_Load($OnLoadForm_StateCorrection)
$form1.ShowDialog()| Out-Null
}
#Call the Function
GenerateForm
Grüße Uwe
Member: clever38
clever38 Feb 03, 2014 at 14:53:01 (UTC)
Goto Top
Hallo Uwe,

Danke für den Tipp, habe auch schon daran gedacht es über eine DataGridView zu machen, sieht halt besser aus.

G.
Clever38