2016-12-15 58 views
1

我有以下代码:输出网络共享位置到控制台用写主机

$exclude = 'Remote Admin', 'Remote IPC', 'Default share' 
$shared = Get-WmiObject Win32_Share | 
     Where-Object { $exclude -notcontains $_.Description } | 
     Select-Object -Expand Path 

$FilesToSearch = Get-ChildItem $shared -Filter '*_HELP_instructions*' -Recurse -ErrorAction SilentlyContinue 

If($FilesToSearch -ne $null) 

{ 
Write-Host "System Infected!" 
exit 2015 
} 

else 

{ 
Write-Host "System Clear!" 
exit 0 
} 

本质上是通过一个本地服务器的任何文件上的所有共享扫描与_HELP_instructions的文件名。

如果找到与此名称匹配的文件,则会输出“System Infected!”到控制台并用特定的代码退出脚本。

我想要做的是实际详细的共享位置感染和输出到控制台。输出看起来像“网络共享E:\数据\客户端被感染!”。

请问这可以实现吗?

非常感谢

+0

'$ FilesToSearch'如果不为空应该包含文件。您可以输出该变量,该变量应该是受感染文件路径的数组。 – Nkosi

+0

嗨Nkosi - 所以代码行将是: { 写主机“网络共享”$ FilesToSearch“被感染!” 退出2015 } 会这样吗? –

回答

1

$ FilesToSearch是一个包含您找到的文件的数组。 所以要达到你想要的:

If($FilesToSearch -ne $null) 
{ 
    foreach ($file in $FilesToSearch){ 
     Write-Host "System Infected! Directory:" $file.DirectoryName 
    } 
    exit 2015 
} 
+0

非常感谢,为此工作。这两个解决方案给了我后我的结果,虽然这个解决方案只是详细的股份,而不是个别的文件,所以是一个稍微更好的解决方案,我的需求 –

+0

不客气 – Boendal