Skip to content

Variables inside the assertion script block #18

Description

@DexterPOSH

Since the Assertion in the PoshSpec is placed inside a scriptblock, how does one reference variables in it e.g below works fine when the assertion is hardcoded -:

Describe test {
    Service Bits Status {Should be 'Running'}
}

But when a variable is placed in the assertion, it can't see them e.g.


Describe test {
    $DesiredStatus = 'Running'
    Service Bits Status {Should be $DesiredStatus}
}

Above gives me the error -:

Describing test
 [-] Service property 'Status' for 'Bits' Should be $DesiredStatus 123ms
   Expected: {}
   But was:  {Running}
   1: Get-Service -Name 'Bits' | Select-Object -ExpandProperty 'Status' | Should be $Desir
edStatus
   at <ScriptBlock>, <No file>: line 1
   at <ScriptBlock>, C:\Users\Deepak_Dhami\Documents\GitHub\poshspec\Private\Invoke-Poshsp
ecExpression.ps1: line 12

The idea is to use a CSV file as a template for all the service and their compliant status.
How do I do something like this with PoshSpec ?

Describe 'IT Compliance for Services' {
    $ObjectFromCSV = @([PSCustomObject]@{
        Name='Bits';
        Status='Stopped'
    })

    $ObjectFromCSV.Foreach({
        Service $PSItem.Name Status {Should be $PSItem.Status}

    })
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions