远程复制日志

问题描述 投票:0回答:0
    function Copy-Logs {
    [CmdletBinding()]
    param (     
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, HelpMessage="Specify the name(s) of the computer to run the SCCM actions on, separated by commas ','")]
    [string[]]$TargetComputerName,
        
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, HelpMessage="Specify the path(s) of logs.")]
    [SupportsWildcards()]
    [string[]]$TargetLogsPaths,
    
    [Parameter(Mandatory=$false, HelpMessage="Specify the folder name for destination.")]
    [ValidateNotNullOrEmpty()]
    [string]$DestName = "logs",
    
    [Parameter(Mandatory=$false, HelpMessage="Specify the Error Action on Copy Item")]
    [ValidateNotNullOrEmpty()]
    [ValidateSet("Continue", "SilentlyContinue", "Stop")]
    [string[]]$ActionOnError = "Stop",

    [Parameter(Mandatory=$false, HelpMessage="Specifies whether to include verbose output.")]
    [Switch]$WriteVerbose,
    
    [Parameter(Mandatory=$false, HelpMessage="Specify if the function should create a new folder each time it's run.")]
    [switch]$NewFolder,
    
    [Parameter(Mandatory=$false, HelpMessage="Specify the domain controller to use.")]
    [string]$TargetDomainController = ".?.?"
    )
        
    # Convert TargetComputerName to an array if it's a string
    if (-not $TargetComputerName -is [array]) {
        $TargetComputerName = $TargetComputerName -split ","
    }
    
    # Convert TargetLogsPaths to an array if it's a string
    if (-not $TargetLogsPaths -is [array]) {
        $TargetLogsPaths = $TargetLogsPaths -split '\s*,\s*' -split '\s+'
    }
    
    # Gets current user into variable for DestFolder
    $CurrentUser = [System.Environment]::UserName
    
    # Loop through each computer name and copy the specified logs
    foreach($ComputerName in $TargetComputerName) {
        $DateFormat = (Get-Date).ToString('dd-MM-yyyy hh-mm-ss')
        $HostDate = $ComputerName + ' - ' + $Dateformat
        $DestFolder = Join-Path "C:\Temp\$CurrentUser" (Join-Path -path $HostDate -child $DestName)
        if ($NewFolder -or (-not(Test-Path $DestFolder))) {
            New-Item -ItemType "directory" -Path $DestFolder
        }
        
        # Create PSSession
        $PC = $ComputerName + $TargetDomainController
        $Session = New-PSSession -ComputerName $PC –UseSSL
                
        foreach ($log in $TargetLogsPaths) { 
            $DestFile = Join-Path $DestFolder (Split-Path $log -Leaf)
            $copyParams = @{
                Path = $log
                Destination = $DestFile
                FromSession = $Session
                Recurse = $True
                ErrorAction = $ActionOnError
                Verbose = $WriteVerbose
            }
            try {
                # Copy-Item @copyParams | Write-Progress
                # Copy the file and pipe the output to Write-Progress
                Copy-Item @copyParams -PassThru | ForEach-Object {
                    Write-Progress -Activity "Copying File" -Status $_.Name -PercentComplete (($_.BytesTransferred / $_.TotalBytes) * 100)
                }
            } catch {
                Write-Warning "Failed to copy '$log': $_"
            }
        }
        
        # Close PSSession
        Remove-PSSession $Session
    }
}

此功能只有在我使用时才有效

TargetLogsPaths = C:\WINDOWS\logs\Software\

我想复制特定的日志,我正在使用像这样的通配符

TargetLogsPaths = C:\WINDOWS\logs\Software\ado*

用于复制所有 adobe 日志的通配符。

然后我得到

**WARNING: Failed to copy 'C:\WINDOWS\logs\Software\ado*': Illegal characters in path.**

你能帮我解决这个问题吗,给我指导或不同的方法。

powershell parameters wildcard copy-item
© www.soinside.com 2019 - 2024. All rights reserved.