对于查找日志和匹配项的每个循环,请将其保存在单独的位置



我计划每月第一天在每个设备上按月收集日志。示例

  1. 192.168.1.100->1月-->log1,log2,log3,二月-->log1,log2,log3,Mar->log1、log2、log3

2.192.168.1.101->1月-->log1,log2,log3,二月-->log1,log2,log3,Mar->log1、log2、log3

3.192.168.1.120->1月-->log1,log2,log3,二月-->log1,log2,log3,Mar->log1、log2、log3

因此每个月的脚本都会得到上个月的格式MMM,并检查设备路径,收集上个月日志,并将其复制到不同的位置,包括设备名称和日志文件。

这个脚本我正在尝试PowerShell。请大家帮帮我。我需要它来完成我的工作。

再次感谢大家帮助我。

这是我的样本代码

$Month = Get-Date -UFormat %b
$files = Get-ChildItem –Path "C:templogs"
foreach ($f in $files) {
if ($Month -eq "May") {
Write-Host ("Found")
Copy-Item "C:templogs" -Filter *.log -Destination C:tempNetwork_Logs -Recurse
}
else {
Write-Host ("Not-Found")
}
}

上面的代码复制了所有的日志文件在一个月,但我只想五月的所有设备。

[1]: https://i.stack.imgur.com/Gjaec.png
[2]: https://i.stack.imgur.com/Ckvz8.png
[3]: https://i.stack.imgur.com/pgBNq.png

如果我正确理解你的问题,这就是你想做的:

$logPath    = 'C:templogs'               # the sourcepath where the logs can be found
$logArchive = 'C:tempNetwork_Logs'       # the destination path
$previousMonth = (Get-Date).AddMonths(-1)  # get last month
# construct the destination path for the log files
$destination = Join-Path -Path $logArchive -ChildPath ('{0:MMM}' -f $previousMonth)
# create the archive month subdirectory
$null = New-Item -Path $destination -ItemType Directory -Force
Get-ChildItem -Path $logPath -File | 
Where-Object { $_.LastWriteTime.Month -eq $previousMonth.Month } |
# copy the files to the destination. 
# You can change this to Move-Item if that is what you want to do
Copy-Item -Destination $destination

如果你想在几台机器上完成这项工作,请确保存档路径位于可以从不同机器访问的网络位置,并使用Invoke-Command:

$machines   = '192.168.1.100', '192.168.1.101', '192.168.1.120'  # the array of machine names or IP addresses
# create a scriptblock each machine will run 
$scriptBlock = {
$logPath    = 'C:templogs'                 # the LOCAL sourcepath where the logs can be found on each computer
$logArchive = '\servershareNetwork_Logs'  # the centralized network destination path (in UNC format)
$previousMonth = (Get-Date).AddMonths(-1)    # get last month
# one way of getting the machines IPV4 address
$myIP = @([System.Net.DNS]::GetHostEntry($env:COMPUTERNAME).AddressList | 
Where-Object {$_.AddressFamily -eq 'InterNetwork'})[0].IPAddressToString
# construct the destination path for the log files
# will become something like '\servershareNetwork_Logs192.168.1.100Apr'
$destination = Join-Path -Path $logArchive -ChildPath ('{0}{1:MMM}' -f $myIP, $previousMonth)
# create the archive path subdirectory
$null = New-Item -Path $destination -ItemType Directory -Force
Get-ChildItem -Path $logPath -File | 
Where-Object { $_.LastWriteTime.Month -eq $previousMonth.Month } |
# copy the files to the destination. 
# You can change this to Move-Item if that is what you want to do
Copy-Item -Destination $destination
}
# have all machines copy their logfiles to the archive
# if needed, add parameter '-Credential $cred' to this, where the $cred variable is
# obtained via:
#     $cred = Get-Credential -Message 'Please provide admin credentials'
Invoke-Command -ComputerName $machines -ScriptBlock $scriptBlock # -Credential $cred