1
votes

I have imported a list of computer names from csv into a variable.

Now I'd like to test whether a path exists on each computer name from the list

desired output:

Computer     Status
--------     ------
computer1    Online
computer2    Offline

Below will register the answer as false. That's one answer against 3 machines however. When i test the $userscomputer variable, it does contain the list of computernames and some of them are online.

Looks like I'll need to run against each machine in the variable but I've had no success so far. Thanks for reading.

$name = "vader"
$usersComputer = import-csv .....etc.
$path = "\\$userscomputer\c$\users\$name"

$componline = test-path $path

$componline

The csv file has 2 columns "Host Name " "Logged User" computer names under host name and ad names under Logged user computer1 bicard computer2 bicard computer3 vader

Ideally id like to show which computer is online my thought is below

$usersComputer | ForEach-Object {
$cn = $_."Host Name"
$path = "\\$cn\c$\users\$name"
$state = Test-Path $path
write-host $cn, $state

}

It outputs below, computer 3 however should report as true ans the $cn variable is not shown.

failed failed failed

I have rejigged your solution this about and it works wonderfully now. The importing of the CSV file was already done so the variable had the computer names. There after below worked.

$usersComputer | ForEach-Object {
    $cn = $_
        $comp = Test-Path \\$cn\c$\users\$username
    [PSCustomObject]@{ 
        Computer = $cn
        Status = if ($componline) { 'Online' } else { 'Offline' }
    }

}

1

1 Answers

0
votes

$usersComputer will contain a collection of objects, and each object will have properties whose names match the columns in the CSV. You haven't provided a sample of what the CSV looks like. So if it has a single column called ComputerName, you might do something like this:

$usersComputer | ForEach-Object {
    $cn = $_.ComputerName
    $path = "\\$cn\c$\users\$name"
    $componline = Test-Path $path
    [PSCustomObject]@{
        Computer = $cn
        Status = if ($componline) { 'Online' } else { 'Offline' }
    }
}