2017-09-11 76 views
-3

我有一些文件夾權限2個TXT文件:文件TXT PowerShell的廣告輸出比較的對象

文件 「舊」

 
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中o bject?

+1

很難理解你想要做什麼,用一個例子修改這個問題並且更加清楚! – Frank

+0

你到目前爲止嘗試過什麼?預期的結果是什麼?什麼是實際結果? –

回答

0

這是一個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 " " 
     } 
    } 
    } 
} 
+0

比較-對象-ReferenceObject $的oldfile -DifferenceObject $ NEWFILE |出文件differenze.txt {$ 分裂 –

+0

過程 { 的foreach(以(獲取內容-Path differenze.txt)$ LN)= $ LN -split 「+」 新物體-TypeName 「PSCustomObject」 -Property' @ { 「計算機名」= $分裂[0] 「文件」= $分裂[1] 「主要」= $分裂[2] 「權限「=($ split | Select-Object -Skip 3)-join」「 } } } } –

+0

腳本沒問題,但是我可以跳過文件的前三行嗎? tnx –