這是一個PowerShell函數,它應該能夠完成您正在尋找的任務。正如@Frank提到的,你應該提供一些更多的細節和具體的例子,所以我們可以確定解決方案建議回答你的問題。我對你的文本文件的格式做了一些非常大的假設。
採取下面的功能,並將其保存爲一個名爲.ps1文件和包括它像這樣:
. c:\temp\Get-Permission.ps1
然後,調用Get-許可功能如實施例中所示:
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 " "
}
}
}
}
很難理解你想要做什麼,用一個例子修改這個問題並且更加清楚! – Frank
你到目前爲止嘗試過什麼?預期的結果是什麼?什麼是實際結果? –