0

First of all thank you for reading. I am new to Powershell so please forgive my ignorance. Also, this is my first time posting here.

Before I had this and it was working, however, it was crazy slow. Here is the initial script:

$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -Property * -SearchBase "OU=Computers,OU=Domain".Name 

ForEach ($Computer in $RemoteComputers)
{
$result = Invoke-Command -ErrorAction SilentlyContinue -ComputerName $Computer -ScriptBlock {Test-NetConnection -Port 135 server.domain.local}  
[pscustomobject]@{
                    Target = $Computer
                    RemoteAddress = $result.RemoteAddress
                    SourceAddress = (Resolve-Dnsname $Computer -ErrorAction SilentlyContinue).IPAddress
                    Port   = $result.RemotePort
                    Status = $result.tcpTestSucceeded
                }        
}

then ran it by doing this

.\ports.ps1 | Tee-Object -Variable result | Export-Csv ports-all.csv -NoTypeInformation

So below I have removed -Property * and cleaned it up. However no content in the CSV file. Can anyone tell me why when I run this there is no content in the csv file?

$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -SearchBase "OU=Computers,OU=Domain").Name 

$results = try {
    Invoke-Command -ErrorAction Stop -ComputerName $RemoteComputers -ScriptBlock {
        $Test = Test-NetConnection -Port 135 "server.domain.local"
        [pscustomobject]@{
            Target = $Env:COMPUTERNAME
            RemoteAddress = $Test.RemoteAddress
            SourceAddress = (Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress
            Port   = $Test.RemotePort
            Status = $Test.tcpTestSucceeded
        }    
    }
}
catch {
    Write-Output ([pscustomobject]@{
        Target = $_.TargetObject
        RemoteAddress = $null
        SourceAddress = $null
        Port   = $null
        Status = "Failed to connect"
    })
}

$results | select target, remoteaddress, sourceaddress, port, status | export-csv file.csv

So this last script does not output anything to CSV. Why?

Kody
  • 16
  • 3

1 Answers1

0

Looks like Resolve-DnsName will return a collection of IPv6 & v4 addresses. I suppose that somewhat depends on what's in your DNS zone. I don't know if it's reliable but you can wrap the command in an array sub-expression then reference the index [1]

SourceAddress = @((Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress)[1]

My tests were not real world, but it looks like you probably want to put the try catch on the inside of Invoke-Command's ScriptBlock.

Maybe something like:

$RemoteComputers = (Get-ADComputer -Filter {OperatingSystem -Like 'Windows*'} -SearchBase "OU=Computers,OU=Domain").Name 

$results = 
    Invoke-Command -ErrorAction Stop -ComputerName $RemoteComputers -ScriptBlock {
        try {
        $Test = Test-NetConnection -Port 135 "server.local.com"
        [pscustomobject]@{
            Target = $Env:COMPUTERNAME
            RemoteAddress = $Test.RemoteAddress
            SourceAddress = @((Resolve-Dnsname $Env:COMPUTERNAME -ErrorAction SilentlyContinue).IPAddress)[1]
            Port   = $Test.RemotePort
            Status = $Test.tcpTestSucceeded
        } }
        catch {
        [pscustomobject]@{
            Target = $_.TargetObject
            RemoteAddress = $null
            SourceAddress = $null
            Port   = $null
            Status = "Failed to connect"
        } }
}

$results | select target, remoteaddress, sourceaddress, port, status | export-csv file.csv

I'd also point out that Test-NetConnection doesn't seem to work well with Try/Catch and with SilentlyContinue as the error action I'm not sure how well the catch will work anyhow.

Again I can only do lackluster tests, don't know what your exact expectation is, but try some of this and let me know how it goes.

Steven
  • 6,817
  • 1
  • 14
  • 14
  • My main issues is the last script does not output to CSV. The IP Address thing was just a secondary comment. – Kody Mar 10 '20 at 02:37
  • I tried your script however and still no outputting to the CSV, – Kody Mar 10 '20 at 02:44
  • Is there a way to make my initial script better then instead of a Try/Catch? – Kody Mar 10 '20 at 02:57
  • I think I just will stick with my original script and just drop the -Property * and call it good. I don't think there is any other way to do it faster. – Kody Mar 10 '20 at 03:04
  • When I tested it I did get output in the CSV file. Removing -Properties will shorten the query. And since you are only after the name. You don't need it. That said my guess is that the number of machines you're has more to do with the speed. Is the CSV getting created at all? – Steven Mar 10 '20 at 03:33
  • Thank you for your help, Steven. When I copy your script as-is, no file is created. I am opening PowerShell as an admin then entering ./script.ps1 and it runs and shows it connecting to clients. Just no csv file is created. Can you tell me how you are running the script exactly? Are you running Powershell as admin and just entering ./script.ps1 or something similar or are you adding anything on it? I even tried to add export-csv -append file.csv and still nothing. I have saved script.ps1 to my desktop and am running PowerShell from that directory too. – Kody Mar 11 '20 at 01:39
  • I finally got it to run. I removed all the -ErrorAction from anywhere in the script and it worked. Also, my initial script would write to the csv file as it ran each test-netconnection, this one would create the file once it was finished. I was reading about workflows and Foreach -Parallel, maybe this is another option but those are over my head. – Kody Mar 11 '20 at 01:46
  • Glad to hear you got it working though I'm still puzzled. I was running it directly in the VSCode editor, equivalent to pasting it in the console. I wouldn't bother with Workflow for this, it's being deprecated. It was mostly used for the parallel capabilities but not much else. That said, PowerShell 7 has added ForEach -Parallel. However, Invoke-Command has it's own parallel and throttling capabilities so you should stick with them. You can check out the help file and the -ThrottleLimit parameter. – Steven Mar 11 '20 at 02:38