检测远程服务器上的文件

时间:2015-02-12 08:34:02

标签: powershell

猜测远程注册表是不可用的(强化的构建因此服务没有运行) - 我无法在注册表中查询特定值。但是,我正在分析的服务器上存在一个文件,它提供了我需要的数据。到目前为止,我已经写了以下内容 - 如果可以对其进行审核,我会很感激,因为它只是挂起 - 我猜我会从父目录的if exists语句中受益..

建议和帮助非常感谢(仅在短时间内使用PowerShell,因此努力工作以解决这个问题。

Set-ExecutionPolicy RemoteSigned -ErrorAction SilentlyContinue

$servers = Get-Content -Path C:\Windows\System32\list3.txt

$out = ForEach ($server in $servers)
{ 
    invoke-command -computername $server {Get-ChildItem -Path "C:\ProgramData\Microsoft\Microsoft Antimalware\Definition Updates\" -Exclude Backup -Filter mpavdlta.vdm -Recurse | Select-Object -Last 1 | Select LastWriteTime | ft -AutoSize}
} 
$out|Out-File C:\Temp\Versions.log

2 个答案:

答案 0 :(得分:0)

$servers = Get-Content -Path "X:\ServerList.txt"
$logfile = "X:\Versions.log"
$Include = "Include.file"
$out = ForEach ($server in $servers)
{ 
 Write-Output(Get-ChildItem -Path "\\$Server\X$\Remote Folder\Structure\" -Exclude Backup -Filter $Include -Recurse | Select-Object -Last 1 | Select LastWriteTime | ft -AutoSize) | Out-File $logFile
} 
$out

您使用的是具有远程计算机权限的帐户吗?如果是这样,这应该提供一条走下去的道路。这将从列表中提取服务器名称并通过\ UNC \ admin $ share查询。 Serverlist.txt只是以下格式的机器列表。

machinename.domain.com

我查看了您的原始请求。你能不能遍历服务器列表并启动远程注册服务,完成工作然后停止它。

类似的东西。

$servers = Get-Content -Path "X:\ServerList.txt"
$Service = "Remote Registry"
$out = ForEach ($server in $servers) 
{ 
 Get-Service -Name $Service  -ComputerName $Server | Set-Service -Status Running
 Do remote reg stuff
 Get-Service -Name $Service  -ComputerName $Server | Set-Service -Status Stopped
} 
$out

https://technet.microsoft.com/en-us/library/hh849849.aspx

答案 1 :(得分:0)

您的脚本有效,但由于您的查询可能会挂起(听起来像是捕获了太多项目)。 “.vdm”文件是否位于该确切目录中?如果有,则可以删除-Recurse。这是你的修改版本。我刚刚添加了连接和目的地检查。

Set-ExecutionPolicy RemoteSigned -ErrorAction SilentlyContinue
$servers = Get-Content -Path C:\Windows\System32\list3.txt
$out = ForEach ($server in $servers)
{
    If(Test-Connection $server -Quiet){ 
        Invoke-Command -Computername $server {
            param([string]$parentPath)

            If(Test-Path $parentPath)
                {
                #Write-Host "$parentPath Exists on $env:COMPUTERNAME."
                Get-ChildItem -Path $parentPath -Exclude Backup -Filter mpavdlta.vdm -Recurse | Select-Object -Last 1 | Select LastWriteTime | ft -AutoSize
                }
            Else{ 
                   #Write-Host "$parentPath does not exist on $env:COMPUTERNAME" 
                }     

            } -ArgumentList $parentPath
        }
    Else { Write-host "Unable to connect to $server." }

} 
$out | Out-File C:\Temp\Versions.log
相关问题