albin63
Goto Top

Powershell User in Netz abfragen

Hoi Zusammen

Komme nicht weiter, mir fehlt noch die Anzeige des aktiven Username.

Das Makro zeigt schön, welche Maschinenname unter welcher IP läuft. Schön ware auch, wenn
der aktuelle Username pro Maschine angezeigt würde.

Kann mir bitte jemand helfen?

LG
Albin63

$path = ".\results.xls"
$objExcel = new-object -comobject excel.application

if (Test-Path $path)
{
$objWorkbook = $objExcel.WorkBooks.Open($path)
$objWorksheet = $objWorkbook.Worksheets.Item(1)
}

else {
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)
}

$objExcel.Visible = $True

Add Header####

$objWorksheet.Cells.Item(1, 1) = "MachineIP"
$objWorksheet.Cells.Item(1, 2) = "Result"
$objWorksheet.Cells.Item(1, 3) = "HostName"
$objWorksheet.Cells.Item(1, 4) = "UserName:"

$machines = gc c:\temp\machinelist.txt
$count = $machines.count

$row=2

$machines | foreach-object{
$ping=$null
$hname =$null
$hname2 =$null
$machine = $_
$user = $_
$ping = Test-Connection $machine -Count 1 -ea silentlycontinue

if($ping){

$objWorksheet.Cells.Item($row,1) = $machine
$objWorksheet.Cells.Item($row,2) = "UP"


$hname = [System.Net.Dns]::GetHostByAddress($machine).HostName
$hname = [System.Net.Dns]::GetHostByAddress($user).HostName


#$hname2 = [System.Security.Principal.WindowsIdentity]::GetCurrent($username).Name

$objWorksheet.Cells.Item($row,3) = $hname
#$objWorksheet.Cells.Item($row,4) = $hname2


$row++}
else {

$objWorksheet.Cells.Item($row,1) = $machine
$objWorksheet.Cells.Item($row,2) = "DOWN"
#$objWorksheet.Cells.Item($row,4) = $username

$row++}
}
#

Content-Key: 383304

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

Ausgedruckt am: 28.03.2024 um 23:03 Uhr

Mitglied: Penny.Cilin
Penny.Cilin 14.08.2018 um 15:25:05 Uhr
Goto Top
Hallo,

damit Dein Code übersichtlicher wird, diesen in Codetags einbinden. Dazu findest Du links neben dem Editor das Symbol </>.
Das macht es leichter, Code zu lesen.

Gruss Penny
Mitglied: colinardo
Lösung colinardo 14.08.2018 aktualisiert um 15:31:46 Uhr
Goto Top
(gwmi Win32_Computersystem -Computer $machine).Username
Einfach immer erst mal unsere Suche benutzen!
Powershell Full Username abfragen

G. Uwe
Mitglied: albin63
albin63 14.08.2018 um 16:12:00 Uhr
Goto Top
Hoi Uwe

Wo braucht es die Zeile, bin leider kein Profi im Powershell. Am liebsten bitte das ganzes Makro auf diese Seite eintragen.

Vielen Dank
Albin


$path = ".\results.xls"
$objExcel = new-object -comobject excel.application

if (Test-Path $path)
{
$objWorkbook = $objExcel.WorkBooks.Open($path)
$objWorksheet = $objWorkbook.Worksheets.Item(1)
}

else {
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)
}

$objExcel.Visible = $True

Add Header####

$objWorksheet.Cells.Item(1, 1) = "MachineIP"
$objWorksheet.Cells.Item(1, 2) = "Result"
$objWorksheet.Cells.Item(1, 3) = "HostName"
$objWorksheet.Cells.Item(1, 4) = "UserName:"

$machines = gc c:\temp\machinelist.txt
$count = $machines.count

$row=2

$machines | foreach-object{
$ping=$null
$hname =$null
$hname2 =$null
$machine = $_
$user = $_
$ping = Test-Connection $machine -Count 1 -ea silentlycontinue

if($ping){

$objWorksheet.Cells.Item($row,1) = $machine
$objWorksheet.Cells.Item($row,2) = "UP"


$hname = [System.Net.Dns]::GetHostByAddress($machine).HostName
$hname = [System.Net.Dns]::GetHostByAddress($user).HostName


#$hname2 = (gwmi Win32_Computersystem -Computer $machine).Username


$objWorksheet.Cells.Item($row,3) = $hname
#$objWorksheet.Cells.Item($row,4) = $hname2


$row++}
else {

$objWorksheet.Cells.Item($row,1) = $machine
$objWorksheet.Cells.Item($row,2) = "DOWN"
#$objWorksheet.Cells.Item($row,4) = $username

$row++}
}
#
Mitglied: colinardo
Lösung colinardo 14.08.2018 aktualisiert um 16:17:38 Uhr
Goto Top
Wo braucht es die Zeile,
Koppschüttel, mal wieder nur kopiert das ganze was?

Natürlich dort wo du die Daten den Zellen zuweist, denn das Ergebnis ist ja direkt der Username
$objWorksheet.Cells.Item($row,4).Value2 = (gwmi Win32_Computersystem -Computer $machine).Username
Am liebsten bitte das ganzes Makro auf diese Seite eintragen.
Du schaffst es ja noch nicht mal es selbst hier vernünftig formatiert einzustellen, dann nö :-P, deine Hausaufgabe.

Tschüss ... und viel Erfolg.

G.
Mitglied: albin63
albin63 14.08.2018 um 16:26:48 Uhr
Goto Top
Hoi Zusammen

Danke für die rasche Lösung, ich habe es geschafft. face-smile

Die Kommandozeile Colinardo hat gepasst. Hurra

Sorry, es ist noch kein Meister vom Himmel gefallen.

Schönen Abend.
Albin