我正在尝试将离线计算机记录在文本文件中,以便我可以在以后再次运行它们.似乎没有记录或陷入捕获.
function Get-ComputerNameChange {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[string[]]$computername,
[string]$logfile = 'C:\PowerShell\offline.txt'
)
PROCESS {
Foreach($computer in $computername) {
$continue = $true
try { Test-Connection -computername $computer -Quiet -Count 1 -ErrorAction stop
} catch [System.Net.NetworkInformation.PingException]
{
$continue = $false
$computer | Out-File $logfile
}
}
if($continue){
Get-EventLog -LogName System -ComputerName $computer | Where-Object {$_.EventID -eq 6011} |
select machinename, Time, EventID, Message }}}
Run Code Online (Sandbox Code Playgroud) 基本上我想查看文本文件中的计算机是否在线.如果他们不在线,则写主机"$ computer is down".如果$计算机在线,那么检查是否存在此服务,如果它存在则写入主机"$ computer installed,如果没有则写入主机"$ computer not installed".测试连接似乎有效但如果计算机在线他们都返回写主机"$ computer installed",即使我有一台我知道没有运行此服务的测试机器.
function Get-RunService {
$service = get-service -name ABCService
Get-Content "C:\powershell\computers.txt" |
foreach {if (-not (Test-Connection -comp $_ -quiet))
{
Write-host "$_ is down" -ForegroundColor Red
}
if ($service )
{
write-host "$_ Installed"
}
else {
Write-host "$_ Not Installed"
}
}
}
get-RunService
Run Code Online (Sandbox Code Playgroud)