Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to call Custom functions in poshspec. #27

Open
SamPosh opened this issue Aug 18, 2016 · 0 comments
Open

Unable to call Custom functions in poshspec. #27

SamPosh opened this issue Aug 18, 2016 · 0 comments
Assignees

Comments

@SamPosh
Copy link

SamPosh commented Aug 18, 2016

I am using Package resource for my infrastructure validation. As my environment has only powershell v4, Get-package function was not there.

I have written a module named Poshspechelper in my machine and written a custom function
Get-InstalledPackage. The function is given below.

function Get-InstalledPackage
{
    [CmdletBinding()]
  param($appName, $appVersion)

  if ((Get-WmiObject win32_operatingsystem).OSArchitecture -notmatch '64')  
  { 
    $keys= (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*')
    $possible_path= 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
  }  
  else  
  { 
    $keys = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*')
    $possible_path= 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
    $possible_path= 'HKCU:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
    if (Test-Path $possible_path)
    {
      $keys+= (Get-ItemProperty $possible_path)
    }
  }

  if ($appVersion -eq $null) { 
    @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName}).Length -gt 0
  }
  else{
    $IsAppInstalled =    @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName}).Length -gt 0
    $VersionAvailable =   @($keys | Where-Object {$_.DisplayName -like $appName -or $_.PSChildName -like $appName  } | Where-Object {$_.DisplayVersion -eq $appVersion} ).Length -gt 0
    if ($VersionAvailable)
    {
        $Version = $appVersion
    }    
    $object = [Pscustomobject] @{
        IsInstalled= $IsAppInstalled
        Version= $Version   
    }
    Write-Output $object

  }
}

When i run this with pester like below , it worked as expected. .

Import-module pester
Import-Module PoshspecHelper

Describe "Orca msi validation" {
    It "Package validation" {
        Get-InstalledPackage -appName 'Orca' -appVersion '3.1.3790.0000' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'version' | should be '3.1.3790.0000'
    }
}

But when i call this in package.ps1 file instead of Get-package (like given below) , i am getting error given below the code.


#$expression = {Get-Package -Name '$Target' -ErrorAction SilentlyContinue}
    $expression = { Get-InstalledPackage -appName '$Target' -appVersion $Property -ErrorAction SilentlyContinue}

[-] Package2 property 'version' for 'Orca' should be '3.1.3790.0000' 67.04s
Expected: {3.1.3790.0000}
But was: {}
1: Get-InstalledPackage -appName 'Orca' -appVersion version -ErrorAction SilentlyContinue | Select-Object -ExpandProperty 'versio
n' | should be '3.1.3790.0000'
at , : line 1
at , C:\Program Files\WindowsPowerShell\Modules\Poshspec\2.1.12\Private\Invoke-PoshspecExpression.ps1: line 12

Please check and let me know what went wrong . when i debugged it ,it always failed in the below line at Invoke-PoshspecExpression.ps1 file

Invoke-Expression $InputObject.Expression

@cdhunt cdhunt self-assigned this Aug 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants