Powershell 2.0 problem

by OliAdams at 2013-03-27 09:51:11

The script below works perfectly in powershell 3.0 but in powershell 2.0 the $ID variable gets passed to Get-BackupJobs as one item with a space between instead of 2 separate values? How does this differ between powershell 2 and 3? Thanks in advance for any help.


$Command = ‘C:\Program Files\Symantec\Backup Exec\bemcmd’
$JobType = ‘-o12’
$Arg = ‘-i’

function Get-BackupIDs{

$JobIds = & $Command $JobType $Arg

$Jobs = @()

ForEach ($ID in $JobIds){

IF ($ID){

IF($ID -match ‘JOB ID’){

$Code = @{ ‘JOBID’=$ID.Substring($ID.Indexof(’{’),($ID.Length - ($ID.Indexof(’{’))))}

}

New-Object -TypeName psobject -Property $Code
}
}
}

$Data = Get-BackupIDs | Select -Unique -ExpandProperty JOBID

function Get-BackupJobs{

param(

[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipeLineByPropertyName=$True)]
[string[]]$JOBID
)

$JobType = ‘-o21’


ForEach($ID in $JOBIDS){

$ArgID = “$Arg$ID”

$Backups = & $Command $JobType $ArgID

ForEach($Entry in $Backups){


If ($Entry -match ‘JOB ID:’){

$Code = @{ ‘BACKUPID’=$Entry.Substring($Entry.Indexof(’{’),($Entry.Length - ($Entry.Indexof(’{’))))}

New-Object -TypeName psobject -Property $Code

}

}
}

}

function Get-BackupData{

param(

[Parameter(Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipeLineByPropertyName=$True)]
[string[]]$BACKUPID
)

$Arg = ‘-hi:’
$Jobtype = ‘-o21’

ForEach($Entry in $BackupID){

$ArgEntry = “$Arg$Entry”

$BackupInfo = & $Command $JobType $ArgEntry

ForEach($Field in $BackupInfo){

IF ($Field -match ‘JOB ID’){

$JOBID = ($Field.Trim(‘JOB ID:’).Trim())

}

IF($Field){

Switch -wildcard ($Field)

{
“JOB ID:" {$jobid=($Field.Trim(‘JOB ID:’)).Trim()}
"JOB NAME:
” {$JobName=($Field.Trim(‘JOB NAME’).Trim(’:’).Trim())}
“JOB STATUS:" {$JobStatus=($Field.Trim(‘JOB STATUS:’)).Trim()}
"SERVER NAME:
” {$ServerName=($Field.Trim(‘SERVER NAME:’)).Trim()}
“DEVICE NAME:" {$DeviceID=($Field.Trim(‘DEVICE NAME:’)).Trim()}
"START TIME:
” {$StartTime=($Field.Trim(‘START TIME:’)).Trim()}
“ELAPSED TIME:" {$ElapsedTime=($Field.Trim(‘ELAPSED TIME:’)).Trim()}
"BYTES PROCESSED:
” {$BytesProcessed=($Field.Trim(‘BYTES PROCESSED:’)).Trim()}
“ESTIMATED BYTES:" {$EstimatedBytes=($Field.Trim(‘ESTIMATED BYTES:’)).Trim()}
"LOGFILE:
” {$LogFile=($Field.Trim(‘LOGFILE:’)).Trim()}
“DIRECTORIES PROCESSED:" {$DirectoriesProcessed=($Field.Trim(‘DIRECTORIES PROCESSED:’)).Trim()}
"FILES PROCESSED:
” {$FilesProcessed=($Field.Trim(‘FILES PROCESSED:’)).Trim()}
“FILES SKIPPED:" {$FilesSkipped=($Field.Trim(‘FILES SKIPPED:’)).Trim()}
"FILES CORRUPT:
” {$FilesCorrupt=($Field.Trim(‘FILES CORRUPT:’)).Trim()}
“FILES IN-USE: *” {$FilesInUse=($Field.Trim(‘FILES IN-USE:’)).Trim()}
“RETURN VALUE: *” {Continue}
Default {“Error No Switch Match Found for $Field”}
}


}

$JobInfo = @{

‘JOBID’=$JOBID;
‘JobName’=$JobName;
‘JobStatus’=$JobStatus;
‘ServerName’=$ServerName;
‘DeviceName’=$DeviceID;
‘StartTime’=$StartTime;
‘ElapsedTime’=$ElapsedTime;
‘BytesProcessed’=$BytesProcessed;
‘EstimatedBytes’=$EstimatedBytes;
‘LogFile’=$LogFile;
‘DirectoriesProcessed’=$DirectoriesProcessed;
‘FilesProcessed’=$FilesProcessed;
‘FilesSkipped’=$FilesSkipped;
‘FilesCorrupt’=$FilesCorrupt;
‘FilesInUse’=$FilesInUse}

}

$Arg = “-hi:”

New-Object -TypeName psobject -Property $JobInfo

}

}

$UniqueIDS = Get-BackupIDs |
Select-Object -Unique -ExpandProperty JOBID

$UniqueIDS = Get-BackupJobs -JOBID $UniqueIDS |
Select-Object -Unique -ExpandProperty BACKUPID

Get-BackupData -BACKUPID $UniqueIDS |
Where-Object JobStatus -match ‘Success’ |
Select-Object JOBName,StartTime,ElapsedTime,JobStatus |
ConvertTo-Html -as Table -PreContent “<h1>Backup Information for $env:Computername from $(Get-Date)</h1>” |
Out-File C:\Index.html

C:\Index.html
by DonJ at 2013-03-27 10:24:15
It’s just a difference in how 2.0 is capturing the output of an external command. It normally does capture it as an array of strings, same as 3.0 - not sure why in your case it isn’t doing so.
by OliAdams at 2013-03-27 11:34:52
I have restarted the shell now and that part seems to be ok. The problem now appears to be when I try to concatenate two variables together. In powershell 3.0 “$Var1$Var2” is acceptable. Is this not acceptable in version 2? Or do I need to keep looking?
by DonJ at 2013-03-27 11:39:33
That’s legal syntax in 2.0. In double quotes, as you’ve done here.
by OliAdams at 2013-03-27 15:31:14
Thanks sorted it now I didnt have the double quotes at the start added that and changed a where clause and it is fine now. Is there an easy way to check if a cmdlet is supported in version 2?
by DexterPOSH at 2013-03-27 16:14:11
[quote=“OliAdams”]Is there an easy way to check if a cmdlet is supported in version 2?[/quote]

Whatever works in PowerShell v2 works in v3 …so best bet would be to go in the v2 --> v3 direction.