Вопрос

Here is the code:

$outputPath = "c:\Output"
$scopePath = "$($outputpath)\scopes.csv"
$clientsPath = "$($outputpath)\clients.csv"
$scopeStream = New-Object System.IO.StreamWriter($scopePath)
$clientStream = New-Object System.IO.StreamWriter($clientsPath)
$scopeStream.WriteLine("Scope,ScopeName")
$clientStream.WriteLine("IP,MAC,Lease,Reservation,Hostname")
$scopeStream.Flush()
$clientStream.Flush()
...
$scopeStream.WriteLine("$($scope.Name),$($scope.Address)")
...
$clientStream.WriteLine("$($client.IP),$($client.MAC),$($client.Lease),$($client.Reservation)$($client.Hostname)")
...
$scopeStream.close()
$scopeStream.dispose()
$clientStream.close()
$clientStream.dispose()

The files are created if they don't exist, but nothing is ever written to them. Perfectly blank files, and I can't figure out why, for the life of me.

Это было полезно?

Решение

I've seen this before - usually it's because the StreamWriter is throwing an Exception. You'd think that this would cause an exception in your script, but I've seen cases where it's throwing non-terminating exceptions, so your script doesn't ever receive it.

After running the block of code, try getting the last exception details:

$Error[0].Exception | Format-List * -Force

If there's an exception, it should help you figure out exactly what is causing the problem. My guess would be that it's a permissions issue.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top