可以根据写入时间获取最新的日志文件,但无法从远程服务器读取日志文件的内容

问题描述 投票:0回答:1

在尝试检索多个远程服务器的最后 10 个日志条目时,我有:

$prod_relational_datastore = @('computer1', 'computer2')
foreach ($hostname in $prod_relational_datastore)
{
    $path = "D:\arcgisdatastore\logs\$($hostname).domain\server"
    Write-Host 'Searching '$path' for latest log file' -ForegroundColor Yellow
    $log = Invoke-Command -ComputerName $hostname -ScriptBlock {(Get-Childitem -Path "D:\arcgisdatastore\logs$($hostname)" -Recurse -Filter server*.log| Select-Object -Last 1).Name}
    $file = $path+'\'+$log
    Write-Host $file -ForegroundColor Red
    Invoke-Command -ComputerName $hostname -ScriptBlock {Get-Content $file -tail 10}
}
    

返回:

Searching  D:\arcgisdatastore\logs\computer1.domain\server for latest log file
D:\arcgisdatastore\logs\computer1.domain\server\server-20241102.081002-7712-0.0.log
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : computer1
 
Searching  D:\arcgisdatastore\logs\computer2.domain\server for latest log file
D:\arcgisdatastore\logs\computer2.domain\server\server-20241102.081019-8820-0.0.log
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : computer2

但是

Invoke-Command -ComputerName computer1 -ScriptBlock {Get-Content 'D:\arcgisdatastore\logs\computer1.domain\server\server-20241102.081002-7712-0.0.log' -tail 10}

退货

<Msg time="2024-11-07T00:01:27,80" type="INFO" code="110608" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Starting backup of webhooks database 'webhooks'...</Msg>
<Msg time="2024-11-07T00:01:27,537" type="INFO" code="110609" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Webhooks database 'webhooks' was backed up successfully.</Msg>
<Msg time="2024-11-07T00:01:27,537" type="INFO" code="110608" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Starting backup of webhooks database 'webhooks'...</Msg>
<Msg time="2024-11-07T00:01:27,988" type="INFO" code="110609" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Webhooks database 'webhooks' was backed up successfully.</Msg>
<Msg time="2024-11-07T00:01:27,988" type="INFO" code="110608" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Starting backup of webhooks database 'webhooks'...</Msg>
<Msg time="2024-11-07T00:01:28,471" type="INFO" code="110609" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Webhooks database 'webhooks' was backed up successfully.</Msg>
<Msg time="2024-11-07T00:01:28,471" type="INFO" code="110608" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Starting backup of webhooks database 'webhooks'...</Msg>
<Msg time="2024-11-07T00:01:28,931" type="INFO" code="110609" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Webhooks database 'webhooks' was backed up successfully.</Msg>
<Msg time="2024-11-07T00:01:28,931" type="INFO" code="110608" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Starting backup of webhooks database 'webhooks'...</Msg>
<Msg time="2024-11-07T00:01:29,387" type="INFO" code="110609" source="Data Store" process="3404" thread="26" methodName="" machine="computer1.domain" user="" elapsed="0.0" requestID="">Webhooks database 'webhooks' was backed up successfully.</Msg>

正如所料。

最后一行 (12) 我做错了什么?

powershell variables remote-server
1个回答
0
投票

由于某种原因,看不见的字符弄乱或传递变量时出现问题。

试试这个:

$prod_relational_datastore = 'computer1' , 'computer2'

foreach ($hostname in $prod_relational_datastore) {
    # Let's prepare the paths variables in advance for easier debug.   
    $DomainServerPath = "D:\arcgisdatastore\logs\$($hostname).domain\server"
    $LogNamesPath     = "D:\arcgisdatastore\logs$($hostname)"

    Write-Host 'Searching '$LogNamesPath' for latest log file' -ForegroundColor Yellow

    # Passing variables through -ArgumentList is the safer option.   
    $LatestLogName = Invoke-Command -ArgumentList $LogNamesPath -ScriptBlock { 
        $LogNamesPath = $args[0]
        Get-ChildItem -Path $LogNamesPath -Recurse -Filter server*.log | 
            # Because you already are using Select-Object there is no need 
            # to encapsulate, just expand the property you need.   
            Select-Object -Last 1 -ExpandProperty Name
    } -computername $Hostname 

    # Use Join-Path to minimize typing mistakes.   
    $LogFilePath = Join-Path -Path $DomainServerPath -ChildPath $LatestLogName

    Write-Host $LogFilePath -ForegroundColor Red

        
    Invoke-Command -ArgumentList $LogFilePath -ScriptBlock { 
        $LogFilePath = $args[0]

        # Evaluate adding error control in case the file is not actually there.  
        Get-Content -Path $LogFilePath -Tail 10  
    } -computername $Hostname 
        
        
}
    
© www.soinside.com 2019 - 2024. All rights reserved.