I have a powershell module called NetBackupPS.psm1 , It has only 0-255 range status code and it's respective messages for the net-backup jobs. I need all status codes and their respective messages which are in net-backup jobs. Then only I can get all the error messages using my powershell script and report it to my client.
NetBackupPS module that I am using:
https://github.com/lazywinadmin/NetBackupPS/blob/master/NetBackupPS.psm1
My Script:
$server = xxxxxxxxxxx
$Username = YYYYYYYYYY
$password=zzzzzzzzzzzz
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$secpasswd
$netbackupfailed=Invoke-Command -ComputerName $server -Credential $mycreds -ScriptBlock {
Import-Module "C:\Program Files\WindowsPowerShell\Modules\NetBackupPS.psm1"
$full= Get-NetBackupJob -Full|?{$_.status -ne ""}
$failed=$full|select ID,jobid,jobtype,status,@{n='fail';e={Get-NetBackupStatusCode -StatusCode ($_.status)}},schedule,client,@{n='Started';e={((Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(($_.started - 18000)))).ToString('yyy-MM-dd HH:mm:ss')}},@{n='Ended';e={((Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds(($_.ended -18000)))).ToString('yyy-MM-dd HH:mm:ss')}},schedule_type
$failed
}
$netbackupfailed
The above code returns an error on the
@{n='fail';e={Get-NetBackupStatusCode -StatusCode ($_.status)}}
line. Because in NetBackupPS module contains only 255 error codes and its respective messages. If status code greater than 255 then it gives error like
greater than maximum range
I need a solution to get error messages for all error codes.
I'm no expert on NetBackup, but a few things to point out. First to get the best response to your question try to format your code for readability. I took the liberty of doing so while changing a few small items below:
$server = xxxxxxxxxxx
$Username = YYYYYYYYYY
$password=zzzzzzzzzzzz
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$secpasswd
$ScriptBlock =
{
Import-Module "C:\Program Files\WindowsPowerShell\Modules\NetBackupPS.psm1"
$full= Get-NetBackupJob -Full |
Where-Object{ $_.status -ne "" }
$failed = $full |
Select-Object ID, jobid, jobtype, status,
@{n = 'fail'; e = { Get-NetBackupStatusCode -StatusCode $_.status } },
schedule, client,
@{n = 'Started'; e = { ( (Get-Date 01.01.1970) + ( New-TimeSpan -Seconds ($_.started - 18000) )).ToString('yyy-MM-dd HH:mm:ss') } },
@{n = 'Ended'; e = { ( (Get-Date 01.01.1970) + ( New-TimeSpan -Seconds ($_.ended -18000) )).ToString('yyy-MM-dd HH:mm:ss') } },
schedule_type
$failed
}
$netbackupfailed = Invoke-Command -ComputerName $server -Credential $mycreds -ScriptBlock $ScriptBlock
$netbackupfailed
Note: I only did this to help me read it.
Now on line 1230 - 1236 of NetBackupPS.psm1 you'll find:
param (
[parameter(Mandatory = $true,
HelpMessage = "Enter status code number (ex: 15) ",
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[ValidateRange(0, 255)]
[int]$StatusCode
You can try modifying the allowed range!
Now on lines 1241 - 1497 you'll find a long switch statement, that seems to define the text per status code. You may want to add the status codes you are expecting here, and make sure they fall within the range you re-defined earlier.
DISCLAIMER: I strongly advise you to reach out to the module's author. I wouldn't take modifying someone else code too lightly you never know how things may work together in the code. Everything I've said is based on a limited review of the .psm1 file code. Moreover, if you have legit status codes from NetBackup that the designer hasn't accommodated he/she may want to update their work etc...
UPDATE
I also found this, looks like a newer module of compiled C# cmdlets built using NEtBackup Rest API. Whether or not you use for this project it is probably worth checking out.