我已经看过,但似乎找不到我要找的东西。 我快到了,我想要的只是一个在多个服务器上运行的脚本,该脚本将关闭并检查一个文件的内容并显示结果。
这是我目前所拥有的:
$credentials = Get-Credential
$hostnames = 'server1','server2'
$searchtext = "Group*"
foreach ($hostname in $hostnames)
{
$file = "\\$hostname\E$\test.htm"
if (Test-Path $file)
{
if (Get-Content $file | Select-String $searchtext)
{
Write-host "$hostname $file"
}
else
{
Write-Host "$hostname Imaging not completed"
}
}
else
{
Write-Host "$hostname cannot read file: $file"
}
}
此脚本正在发送到服务器,但我无法在屏幕上告诉我该文件中的内容。我明白了:
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
server1 \\server1\E$\test.htm
server2 \\server2\E$\test.htm
谁能建议我如何将文件路径换成它读取的实际内容?
提前致谢!
可以将搜索字符串添加到您的脚本中,以便您可以检索其结果。这将使您能够在运行脚本时查看与所选搜索字符串匹配的文件内容。
$credentials = Get-Credential
$hostnames = 'server1','server2'
$searchtext = "Group*"
foreach ($hostname in $hostnames)
{
$file = "\\$hostname\E$\test.htm"
if (Test-Path $file)
{
$result = Get-Content $file | Select-String $searchtext
if ($result)
{
Write-Host "$hostname $file - Search string found: $searchtext"
$result | Write-Output
}
else
{
Write-Host "$hostname Imaging not completed"
}
}
else
{
Write-Host "$hostname cannot read file: $file"
}
}
您也可以通过 invoke-command 并行执行此操作。但是 select-string 的输出比 invoke-command 更有趣,除非你选择一个属性。
icm ar001,ar002,ar003 { select-string logfilepath c:\googleupdate.ini | select line }
Line PSComputerName RunspaceId
---- -------------- ----------
LogFilePath=GoogleUpdate.log ar001 e411d83a-362f-42cb-9c84-b560bce44949
LogFilePath=GoogleUpdate.log ar002 g411d83a-362f-42cb-9c84-b560bce44949
LogFilePath=GoogleUpdate.log ar003 h411d83a-362f-42cb-9c84-b560bce44949