none
Need help with Runtime error RRS feed

  • Question

  • I am using this function to parse text. I am getting this error:

    Method invocation failed because [System.Object[]] does not contain a method named 'op_Subtraction'.At
    C:\users\okadhim\Documents\WindowsPowerShell\Scripts\SystemAccess.ps1:277 char:9
    +         $PathLineNumber = (($Description | Select-String -Pattern "Pa ...
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Subtraction:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    Function Read-NetworkDriveAccessRequest{
    
        [CmdletBinding()]
    
        Param(
    
            [Parameter(Mandatory=$True,ValueFromPipeline = $True)]
            [ValidateNotNullOrEmpty()]
            [ValidatePattern("[IS][NR]\d{6}")]
            [String]$ID
        )
    
        Begin {
    
            If (Test-path .\output.txt) {
                
                Write-Verbose -Message "Removed $(Get-Location)\output.txt"
                Remove-Item .\output.txt
            }
        }
    
        Process{
            
            Find-ServiceRequest $ID | Show-Description > output.txt
            $Description = Get-Content output.txt
            Write-Verbose -Message "Service Request Description is `n $Description"
            $UsernameLineNumber = (($Description | Select-String -Pattern "User name:").LineNumber)-1
            $usernameLine = $Description[$UsernameLineNumber]
            Write-Verbose -Message "UsernameLine is $UsernameLine"
            $username =$UsernameLine.Substring($UsernameLine.IndexOf(':')+1).trim()
            $FirstName = $username.Split(" ")[0]
            $LastName = $username.Split(" ")[1]
            Write-Verbose -Message "User name is $Username"
            $UserID = Get-QADUser -FirstName $FirstName -LastName $LastName | Select-Object -ExpandProperty SamAccountName
            Write-Verbose -Message "UserID is $UserID"
            $PathLineNumber = (($Description | Select-String -Pattern "Path:").LineNumber)-1
            $PathLine = $Description[$PathLineNumber]
            $Path = $pathLine.Substring($PathLine.IndexOf(':')+1).trim()
            Write-Verbose -Message "Path is $Path"
            $AccessLevelLineNumber = (($Description | Select-String -Pattern "Access Level:").LineNumber)-1
            $AccessLevelLine = $Description[$AccessLevelLineNumber]
            $AccessLevel = $AccessLevelLine.Substring($AccessLevelLine.IndexOf(':')+1).trim()
            Write-Verbose -Message "AccessLevel is $AccessLevel"
    
            If($AccessLevel.Contains("Read") -and $AccessLevel.Contains("Write")) {
                
                $AccessLevel = "Modify"
                
            } else {
                
                $AccessLevel = "Read"
                    
            }
            Write-Verbose -Message "AccessLevel is $AccessLevel"
            $props = [Ordered]@{
                Username = $userID
                Path = $Path
                AccessLevel = $AccessLevel
            }
    
            $obj = New-Object -TypeName psobject -Property $props
            Write-Output $obj
    
        }
    }

    Wednesday, September 20, 2017 3:02 AM

Answers

  • The error says tyhere is more than one line.

    $x = $Description | Select-String -Pattern "Path:"
    $x.Count


    \_(ツ)_/

    • Marked as answer by Simsim1981 Wednesday, September 20, 2017 3:35 AM
    Wednesday, September 20, 2017 3:34 AM

All replies

  • The error is pretty clear.  An array type does not support subtraction.  YOU are trying to subtract 1 from and array.

    The following returns an array.

    ($Description | Select-String -Pattern "Access Level:")


    \_(ツ)_/


    • Edited by jrv Wednesday, September 20, 2017 3:17 AM
    Wednesday, September 20, 2017 3:15 AM
  • But this 

    $UsernameLineNumber = (($Description | Select-String -Pattern "User name:").LineNumber)-1

    works :-(, the Ideas is to get the line number that contains a specific Pattern, then we need to subtract 1 as the File will starts with [0].

    only 

    $PathLineNumber = (($Description | Select-String -Pattern "Path:").LineNumber)-1

    is throwing error. :-(

    Wednesday, September 20, 2017 3:29 AM
  • The error says tyhere is more than one line.

    $x = $Description | Select-String -Pattern "Path:"
    $x.Count


    \_(ツ)_/

    • Marked as answer by Simsim1981 Wednesday, September 20, 2017 3:35 AM
    Wednesday, September 20, 2017 3:34 AM