You can try using the -quicktimeout Parameter of the Resolve-DNSName cmdlet. It does not let you specify a timeout value, but it’s quicker than the default.
OK so from what I can see above the solution is to run Invoke-Command within a job, downside is that it will time out the whole lot when the job is stopped.
So in my example I had an actual problem where I wanted to retrieve information about services running on several hundred servers. I didn’t want the Invoke-Command to halt after a specific time but, if there was a server that wasn’t responding, it would timeout for that particular computer so that the batch wouldn’t be waiting for it.
In my case I had one duff server that was making the query take 30 mins to run, with the code below the whole lot runs in about 2.5 mins, less then a tenth of the time.
Basically I’ve just put the start-job within the ICM.
Hopefully someone may find this useful
#Array of Servers
$ComputerName = @('Server01','Server02','Server03')
# service to check
$svcName="sql*"
$out=Invoke-Command -computername $ComputerName -ArgumentList $svcName{
param($svcName)
$jbID=Start-Job -ArgumentList $svcName{
param($svcName)
$objArray=@();
get-service -DisplayName "$svcName"| foreach-object{
# get service startmode and time
$filter="Name='"+$_.ServiceName+"'"; # can't put filter variable into function filter!
$s=Get-WmiObject -Class Win32_Service -Filter $filter
$ProcessInfo=Get-WmiObject -Class Win32_Process -Filter "ProcessID='$($s.processid)'" -ea 0
if($_.Status -eq 'Running'){
$lastStarted=$s.ConvertToDateTime($ProcessInfo.CreationDate)
}else{
$lastStarted='N/A'
}
$ht = @{
DisplayName=$_.DisplayName;
ServiceName=$_.ServiceName;
Status=$_.Status;
ServiceType=$_.ServiceType;
StartMode=$s.startmode;
lastStarted=$lastStarted;
LogOnAs=$s.StartName;
};
$Obj=New-Object -TypeName PSObject -Property $ht
$objArray+=$Obj
}
$objArray;
}
for ($i2=1; $i2 -le 30; $i2++){
if((Get-Job $jbID.id).State -notmatch 'Running'){
BREAK;
}
sleep -Seconds 1
}
$out=Get-Job $jbID.id | Receive-Job
Stop-Job $jbID.id
remove-Job $jbID.id
Write-Output $out
}
$out|select pscomputername,DisplayName,lastStarted,ServiceName,LogOnAs,StartMode,ServiceType,Status |ft -a