我们需要获取一组用户的工作站ID列表。我们修改了启动脚本以运行批处理文件,该文件使用以下方法将我们需要的信息转储到文件共享:
用户名 .txt的
编辑:这是一个文本文件的示例(有2000个用户,所以我们将及时有2000个文件)
Workstation1 Workstation2 CitrixServer1 Workstation1 Workstation1
我们会有重复项,因为它们会多次登录。
此文件包含计算机列表(用户已登录的工作站和citrix服务器)。现在我试图以有意义的方式提取信息(有超过1300个文件,最终计数将在2000左右)。到目前为止,这就是我所拥有的:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
# this is the path for the share
$SearchFolder = "\\server\Users\*.txt"
# Search each txt file in the path and store all of the lines and filenames that don't match the pattern
$File = Select-String -Path $SearchFolder -Pattern %CLIENTNAME%,"ECHO is on", "server*","vs*","ctx*" -NotMatch | select line, filename
$File
这给了我以下内容:
Workstation1 User1 Workstation2 User2 Workstation3 User3 Workstation4 User3 Workstation4 User3
我想要的是获取用户登录的唯一工作站列表。
User1 Workstation1 User2 Workstation2 User3 Workstation3 Workstation4
我该怎么做?
答案 0 :(得分:0)
你应该能够使用group-object获得你需要的东西,假设你的colums的名字是“user”和“machine”它看起来像
$files | group-object -property user
答案 1 :(得分:0)
您在$File
中存储的输出应该已经是一个对象数组,每个对象都有一个名为.line
的属性(即计算机名称)和一个名为{的属性{1}}(这是用户名)。
通过按用户对它们进行分组,您可以获得工作站列表,然后从那里可以使该列表具有唯一性:
.filename
这为您提供了一种新类型的对象,其中$groups = $File | Group-Object filename
属性是指被分组的元素,在本例中是文件名(用户名)。 .Name
属性是原始对象。
因此,从那里,您可以遍历组并获取每个组的工作站:
.Group
现在$all = $groups | ForEach-Object {
$user = [System.IO.Path]::GetFileNameWithoutExtension($_.Name)
$computers = $_.Group.line | Sort-Object | Select-Object -Unique
[PSCustomObject]@{
User = $user
Computers = $computers
}
}
将包含一个对象数组,其中每个对象代表一个用户,其中包含一组计算机。你可以随意格式化。
我想要的是一列有用户,第二列有工作站。如果 如果用户有多个工作站,则用户名将为空。
这有点奇怪,但如果您想要一个看起来像这样的CSV,我可能会做这样的事情:
$all