我想列出所有其启动类型设置为自动如何使用PowerShell来提取.dll文件或数据.exe文件
我使用PowerShell的5
$path = 'hklm:\SYSTEM\ControlSet001\Services'
$services = get-childitem $path | get-itemproperty -name 'Start'
foreach ($s in $services){
if($s.'Start' -like '2'){
$dn = get-itemproperty $s.'pspath' -name 'DisplayName'
echo $dn
}
}
但问题的服务大部分条目都是这样使用的:
@%systemroot%\system32\SearchIndexer.exe,-103
@%SystemRoot%\System32\wscsvc.dll,-200
那么如何从中提取字符串呢?
要进一步说明,对于@%systemroot%\system32\SearchIndexer.exe,-103
显示名称是"Windows Search"
。问题是,PowerShell能够从SearchIndexer.exe
中提取字符串"Windows Search"
吗?以及如何做到这一点?
UPDATE:
基本上偷走了代码How to extract string resource from DLL
$source = @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public class ExtractData
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int LoadString(IntPtr hInstance, int ID, StringBuilder lpBuffer, int nBufferMax);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FreeLibrary(IntPtr hModule);
public string ExtractStringFromDLL(string file, int number) {
IntPtr lib = LoadLibrary(file);
StringBuilder result = new StringBuilder(2048);
LoadString(lib, number, result, result.Capacity);
FreeLibrary(lib);
return result.ToString();
}
}
"@
Add-Type -TypeDefinition $source
$ed = New-Object ExtractData
$path = 'hklm:\SYSTEM\ControlSet001\Services'
$services = get-childitem $path | get-itemproperty -name 'Start' -ErrorAction SilentlyContinue
foreach ($s in $services){
if($s.'Start' -like '2'){
$dn = get-itemproperty $s.'pspath' -name 'DisplayName'
try{
$dn = $dn.DisplayName.Split(',')
$dn = $ed.ExtractStringFromDLL([Environment]::ExpandEnvironmentVariables($dn[0]).substring(1), $dn[1].substring(1))
}
catch{}
finally{
echo $dn
}
}
}
丑陋,但它的工作,终于......
Thx!这是更好的方式来做到这一点。但是,我仍然可以得到如何从.dll,.exe文件中提取字符串的答案吗?只是好奇 – oNion
@oNion:我在想那个。我不认为你可以依赖于总是附加资源ID或路径名不包含逗号!因此可能需要应用一些启发式方法来获得一个可靠的解决方案。但简单的情况是(字符串在'$ s'中)'[Environment] :: ExpandEnvironmentVariables($ s.Substring(1,$ s.LastIndexOf(',') - 1))''。 – Richard
它似乎没有工作,它使'@%SystemRoot%\ System32 \ wscsvc.dll,-200'成为'@%SystemRoot%\ System32 \ wscsvc.dll',不完全提取文件中的数据。但是,无论如何感谢:) – oNion