all.
About to lose it - This was working hours ago.
So when I run this I get
Cannot index into a null array.
At line:3 char:15
+ $backlog[0]=$(Get-DFSRBacklog -sourcecomputername:ts-fileshares2 -destinationcom ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Issue being, when I search here and elsewhere, I find people making issues assigning a null array TO a variable. Not assigning to the array.
Below is most of the code (I am running this against multiple fileshares, too lazy to change server names in six lines. Typically backlog[] contains 6 sets of replies.
clear-host
$backlog[0]=$(Get-DFSRBacklog -sourcecomputername:a -destinationcomputername:b -Verbose) 4>&1
#$collection is one array result. This will be easier to split out into by-server later.
[int]$total = 0
foreach ($collection in $backlog)
{
$collection | ForEach-Object -Process {
if ($_.getType().FullName.equals("System.Management.Automation.VerboseRecord"))
{
#$_ | Get-Member
#Write-Host $_.Message
if ($_.Message -like "*has a backlog*")
{
#testing write-host $_.Message.split(" ")[12]
$total += [convert]::ToInt32($_.Message.split(" ")[12],10)
}
}
}
}
#testing Write-Host $total
[Environment]::SetEnvironmentVariable("TOTALFILESHAREBACKLOG",$total, "Machine")
#testing Write-Host $env:TOTALFILESHAREBACKLOG
As I said, this was working hours ago. Not sure if I have some weird variable stuck, but this is on out prod server (no way to get DFSR backlog from a non-prod server) so I don't want to reboot the entire server.
This is a signed powershell script that is running on a schedule (zabbix pulls the env variable later for reporting) but that shouldn't affect it. The only thing that I changed between this working and not working was moving where this file was saved.
The Powershell Get-DFSRBacklog does always return a value or print an error, so that's not it.
Even doing $backlog[0] = 1 throws an error.
P.P.S.
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
$null. So where is$backlogbeing defined?$x[0] = 1hasn't ever been a valid way to define a variable from scratch. It would have to be$backlog=$(Get-DFSRBacklog -sourcecomputername:a -destinationcomputername:b -Verbose) 4>&1with no[]for the first definition. Once it is an array, then you can[0]it.backlogs or just 1 backlog that contains an array of items?