简体   繁体   中英

Powershell script executed on each file in a folder?

I currently have a powershell script, which print out some information regarding the files which passed in as argument..

The command for executing the script, it done as such:

.\myscript.ps1 -accessitem C:\folder

I want to apply the script on all files and folder on the drive C: , is it possible i for loop to list all files, and pass the path as argument for the script?

The script:

[CmdletBinding()]
Param (
    [Parameter(Mandatory=$True,Position=0)]
    [String]$AccessItem
)
$ErrorActionPreference = "SilentlyContinue"
If ($Error) {
    $Error.Clear()
}
$RepPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$RepPath = $RepPath.Trim()
$str = $AccessItem -replace ':',''
$str = $AccessItem -replace '/','.'

$FinalReport = "$RepPath\"+$str+".csv"
$ReportFile1 = "$RepPath\NTFSPermission_Report.txt"

If (!(Test-Path $AccessItem)) {
    Write-Host
    Write-Host "`t Item $AccessItem Not Found." -ForegroundColor "Yellow"
    Write-Host
}
Else {
    If (Test-Path $FinalReport) {
        Remove-Item $FinalReport
    }
    If (Test-Path $ReportFile1) {
        Remove-Item $ReportFile1
    }
    Write-Host
    Write-Host "`t Working. Please wait ... " -ForegroundColor "Yellow"
    Write-Host
    ## -- Create The Report File
    $ObjFSO = New-Object -ComObject Scripting.FileSystemObject
    $ObjFile = $ObjFSO.CreateTextFile($ReportFile1, $True)
    $ObjFile.Write("NTFS Permission Set On -- $AccessItem `r`n")
    $ObjFile.Close()
    $ObjFile = $ObjFSO.CreateTextFile($FinalReport, $True)
    $ObjFile.Close()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ObjFSO) | Out-Null
    Remove-Variable ObjFile
    Remove-Variable ObjFSO
    If((Get-Item $AccessItem).PSIsContainer -EQ $True) {
        $Result = "ItemType -- Folder"
    }
    Else {
        $Result = "ItemType -- File"
    }
    $DT = Get-Date -Format F
    Add-Content $ReportFile1 -Value ("Report Created As On $DT")
    Add-Content $ReportFile1 "=================================================================="
    $Owner = (Get-Item -LiteralPath $AccessItem).GetAccessControl() | Select Owner
    $Owner = $($Owner.Owner)
    $Result = "$Result `t Owner -- $Owner"
    Add-Content $ReportFile1 "$Result `n"
    (Get-Item -LiteralPath $AccessItem).GetAccessControl() | Select * -Expand Access | Select IdentityReference, FileSystemRights, AccessControlType, IsInherited, InheritanceFlags, PropagationFlags | Export-CSV -Path "$RepPath\NTFSPermission_Report2.csv" -NoTypeInformation
    Add-Content $FinalReport -Value (Get-Content $ReportFile1)
    Add-Content $FinalReport -Value (Get-Content "$RepPath\NTFSPermission_Report2.csv")
    Remove-Item $ReportFile1
    Remove-Item "$RepPath\NTFSPermission_Report2.csv"
    Invoke-Item $FinalReport
}
If ($Error) {
    $Error.Clear()
}

I would prefer a outside command doing this, as workings of the script should not be altered, it it used for single file testing..

There are two ways to do this:

  1. Add -Recurse Flag to the script
  2. Run the script on each directory

I'm going with option two since the script looks complicated enough that I don't want to touch it.

$path_to_script = "C:\path\to\myscript.ps1"
$start_directory  = "C:\folder"

# Call Script on Parent Directory
& "$path_to_script" -AccessItem "$start_directory"

# Call Script on any Child Directories within the "$start_directory"
foreach($child in (ls "$start_directory" -Recurse  -Directory))
{
    $path = $child.FullName
    & "$path_to_script" -AccessItem "$path"
}

Basically, I'm calling the script on the parent directory and any sub-directories within the parent directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM