如何获取远程服务器上“C:\user”文件夹中所有用户的名称,并将该名称写入 Excel 文件

如何获取远程服务器上“C:\user”文件夹中所有用户的名称,并将该名称写入 Excel 文件

我试图通过 powershell 获取多个服务器的所有用户的名称,然后将其传输到 excel 文件,但我只在 txt 文件中获得了用户的名称,无法将其复制到 excel。

Foreach($Computer in get-content D:\Discserver.txt) { $win7= Test-path C:\users If(win7 -eq 'true') {$win7users=(get-itemproperty -path C:\users* -exclude Admin,Administrator,public,temp,updateuser,'All users' ,user).name} Write-host 用户列表: - foregroundcolor green $win7users Out-file -filepath c:\userinfo.txt -inputobject (new-object -typename String -argumentlist "user list:") -Append Out-file -filepath c:\userinfo.txt -inputobject $win7users -Append ##我必须用来在 excel 中写入它的方法如下 #打开工作簿 $XL = new-object -ComObject Excel.Application $WB = $XL.workbook.open("c:\userinfo.xlsx") #激活 sheet1,通过管道传输到 out-null 以避免将“true”输出到屏幕上$WB.Sheets.Item("Sheet1").Activate() | out-null #找到第一个空白行 #,并激活该行中的第一个单元格 $FirstBlankRow = $($XL.Activesheet.usedRange.Rows)[-1].Row + 1 $XL.ActiveSheet.Range("A$FirstBlankRow"). Activate () [Array]$users={$win7users} [Array]$Record=[PSCustomObject]@{ 'Users' = $users[0] } $Record += $users | Select -Skip 1 | foreach{[PSCustomObject]@{ '$users' = $_ } } $Record | ConvertTo-Csv +Delimeter "`t" -NoTypeInformation $XL.ActiveSheet.Paste() $WB.save() | out-null $Wb.close() | out-null $XL.Quit() |出空值

相关内容