Showing posts with label test-connection. Show all posts
Showing posts with label test-connection. Show all posts

Friday, April 28, 2017

Everyday Powershell - Part 41 - Get Screen Resolution from remote PCs

Everyday powershell. It's not updated everyday, it's tools you could use daily.

You know sometimes you just need to know what resolution all your computers are running.
get-adcomputer -Filter {operatingsystem -like "Windows 7*"} -Properties operatingsystem | ForEach-Object {
    $temp = "" | select computername, ScreenHeight, ScreenWidth, pcon
    $temp.pcon = Test-Connection $_.name -Count 1 -Quiet
    $temp.computername = $_.name
    if($temp.pcon){
        $resolution = Get-WmiObject -ComputerName $_.name -Class Win32_DesktopMonitor
        $temp.ScreenHeight = $resolution.ScreenHeight
        $temp.ScreenWidth  = $resolution.ScreenWidth
    }
    $temp
    $temp | export-csv C:\temp\monitoraudit.csv -Append
}

Check out the filter on the first line, that can be anything. We just needed Windows 7 machines.

It's a pretty easy foreach-object;

  • sets up a temp object in my favorite manner,
  • pings the machine to make sure it's up,
  • pulls the info we need from WMI
  • then bangs it out to the console and a CSV.
Hitting up each machine with a WMI query gets us fast results but it's not exactly complete. You could rejig this so that it ran as part of a login script or schedule it to run daily.

Why do we need this info? Well that's for next time when we'll do something useful with this data set we've created.

Monday, February 15, 2016

Everyday Powershell - Part 37 - Powershell Subnet Scanner

So we had a need to run a subnet scan this morning on a host where installing a subnet scanner would be a hassle. So powershell to the rescue!

Turns out there's a really easy but slow way to do this...
1..254 | ForEach-Object {test-connection ("192.168.6." + $_)}
Then there's a tricky way that's much quicker.
function start-subnetscan{
    param(
        $addressspace = "192.168.6.",
        $startip = 1,
        $endip = 254,
        $concurrentthreads = 30
    )
    $report = @()
    $startip..$endip | ForEach-Object {
        Write-Progress -Activity "Pinging Computers" -Status ("Pinging " + $addressspace + $_.tostring().padleft(3,"0") + " Starting Jobs. Started " + (get-job).count) -PercentComplete (($_ / $endip) * 100)
        start-job -scriptblock {
            $temp = "" | select IP, Online
            $temp.ip = ($args[0] + $args[1].tostring().padleft(3,"0"))
            $temp.online = test-connection $temp.ip -Quiet -Count 1
            $temp
        } -ArgumentList $addressspace, $_  
        while((get-job).count -ge $concurrentthreads){
            Write-Progress -Activity "Pinging Computers" -Status ("Waiting for resources. " + (get-job).count + " jobs running") -PercentComplete (($_ / $endip) * 100)
            $report += Get-Job | where state -eq 'completed' | Receive-Job
            Get-job | where state -eq 'completed' | Remove-Job
        }                           
    }
    $report += get-job | wait-job | Receive-Job
    $report | select IP, Online | sort IP | ft
    Get-job | wait-job  | Remove-Job
}

So to give you an idea of the power of multi threading we setup both commands so we could run them through measure-command and then we raced them!
Measure-Command -Expression {start-subnetscan} 
Days              : 0
Hours             : 0
Minutes           : 2
Seconds           : 55
Milliseconds      : 951
Ticks             : 1759513835
TotalDays         : 0.00203647434606481
TotalHours        : 0.0488753843055556
TotalMinutes      : 2.93252305833333
TotalSeconds      : 175.9513835
TotalMilliseconds : 175951.3835

Measure-Command -Expression {1..254 | ForEach-Object {test-connection ("192.168.6." + $_) -count 1}} 
Days              : 0
Hours             : 0
Minutes           : 11
Seconds           : 26
Milliseconds      : 940
Ticks             : 6869401183
TotalDays         : 0.00795069581365741
TotalHours        : 0.190816699527778
TotalMinutes      : 11.4490019716667
TotalSeconds      : 686.9401183
TotalMilliseconds : 686940.1183
 
Have a look at that! Running it multi-threaded made it 3 times faster.

Tuesday, July 29, 2014

Everyday Powershell - Part 26 - Check Server last boot time

Here's a quickie that was useful this week.
$servers = Get-ADComputer -filter {OperatingSystem -like "*Server*"}
$report = @()
foreach ($server in $servers){
    $temp = "" | Select Name, Lastboot
    $temp.name = $server.dnshostname
    if(test-connection $temp.name -Count 1 -quiet){
        $temp.lastboot = [datetime]::ParseExact(((Get-wmiobject -ComputerName $temp.name Win32_operatingSystem).lastbootuptime).Substring(0,12), "yyyyMMddHHmm", $null)
    }
    else{
        $temp.lastboot = $null
    }
    $report += $temp
}
$report | sort name

Original inspiration was from here;

You see the great thing with powershell is once you know how to do something on one server you can do it on many servers relatively easily. To support some of our older fleet we had to change that query from CIM to WMI and meant some trickery with the Datetime but that’s all pretty simple stuff.

This could be combined with the server shutdown comments script from part 11 to get the reason for the last reboot too!

Wednesday, May 28, 2014

Everyday Powershell - Part 23 - Check for old servers

Another simple AD cleanup script. But it's the simple things in life that count right?

You can run this script and it'll find any servers in your domain. It pings them and if they reply it sets .there to true otherwise it'll be null.
$servers = Get-ADComputer -filter {OperatingSystem -like "*Server*"}
$report = @()
foreach ($server in $servers)
{
    $temp = "" | select Name, There
    $temp.name = $server.DNSHostName
    $temp.there = if(Test-Connection $server.DNSHostName -Count 1){$true}
    $report += $temp
}
$report

Quick and dirty powershell to do something useful in a hurry. That's what we're all about here!