我有两个带有一些文件夹权限的txt文件:
文件“旧”
pc1 test everyone full control pc2 test everyone full control
提交新文件
pc1 test everyone full control pc3 test everyone full control pc2 test everyone full control
如何使用Compare-Object
命令查找两个文件中的差异并将其写入PowerShell对象?
答案 0 :(得分:0)
这是一个PowerShell功能,可以完成你想要的。正如@Frank所提到的,您应该提供更多细节和具体示例,以便我们确定解决方案建议回答您的问题。我对文本文件的格式做了一些很大的假设。
使用下面的功能,并将其另存为.ps1文件,并将其包括在内:
. c:\temp\Get-Permission.ps1
然后,调用Get-Permission函数,如示例所示:
Compare-Object -ReferenceObject (Get-Permission -File C:\temp\old.txt) -DifferenceObject (Get-Permission -File C:\temp\new.txt)
应该这样做。这是功能:
function Get-Permission()
{
<#
.SYNOPSIS
This function reads a set of permissions defined in a text file.
.DESCRIPTION
The text file is space-delimited but for the last field. The format is as follows:
Machine Name - Specifies the name of the machine the file is on.
File Name - Specifies the name of the folder the permission applies to.
Principal - Specifies the principal the permission is applied against.
Permission - Specifies the permission on the file.
Example:
pc1 test everyone full control
pc3 test everyone full control
pc2 test everyone full control
.PARAMETER File
Specifies the file to read the permissions from.
.INPUTS
[System.Io.FileInfo]
.OUTPUTS
[PSCustomObject[]]
A PSCUstomObject with the following properties:
ComputerName
File
Principal
Permission
.EXAMPLE
# Get the permissions structure from the old file:
Get-Permission -File c:\temp\old.txt
Permission ComputerName File Principal
---------- ------------ ---- ---------
full control pc1 test everyone
full control pc3 test everyone
.EXAMPLE
# Get the permissions structure from the old and new files and compare:
Compare-Object -ReferenceObject (Get-Permission -File C:\temp\old.txt) -DifferenceObject (Get-Permission -File C:\temp\new.txt)
InputObject SideIndicator
----------- -------------
@{Permission=full control; ComputerName=pc2; File=test; Principal=everyone} =>
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[string] $File
)
process
{
foreach ( $ln in (Get-Content -Path $File) )
{
$split = $ln -split " +"
New-Object -TypeName "PSCustomObject" -Property `
@{
"ComputerName" = $split[0]
"File" = $split[1]
"Principal" = $split[2]
"Permission" = ($split | Select-Object -Skip 3) -join " "
}
}
}
}