How to write a Powershell function that can evaluate “using expressions” both locally without WinRM and remotely through WinRM?

The new IT-issued developer laptops at my org are configured in such a way that fully enabling WinRM is a nightmare – I still can’t figure it out. I try to work within lowest-common-denominator wherever possible instead of demanding everybody reconfigure machines to meet DevOps needs, so I still target PS 5.1.

Since “I don’t want to use WinRM” seems to be a common config pain point on many developer machines and I want to be able to run my devops powershell code locally, I wanted to write wrapper for Invoke-Command where it uses either WinRM or not depending on whether being run against localhost – when you run it with a ComputerName of “localhost”, it doesn’t use remoting, but when you give it the name of one of our servers, it goes through WinRM remoting.

However, the naive approach means using ArgumentList. Which is very tedious since you have to triple-declare every var, with matching order. Too many opportunities to screw up.

So I wanted to see if I could use “using expressions” (that is, $using:MyVarName). But using expressions can’t be used locally, only through WinRM and through Jobs. So as a hack I thought “okay, I’ll make it use a Job”.

So I developed this function:

function Invoke-CommandWithUsingVars {
    <#
    .SYNOPSIS
        invokes the command in such ways that $using:varname is always available *without* using remoting if
        pointing at localhost.
    #>
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline)]
        [string] $ComputerName = $null,

        [Parameter(Mandatory)]
        [ScriptBlock] $ScriptBlock
    )

    # deep magic: get all variables from calling scope and write them into this one.
    Get-Variable -Scope 1 -Exclude ScriptBlock, ComputerName |
        Where-Object {
            $localVar = Get-Variable $_.Name -ErrorAction SilentlyContinue 
            
            # return
            -not $localVar `
            -or -not $_.Options -bAnd [Management.Automation.ScopedItemOptions]::ReadOnly `
            -or -not $_.Options -bAnd [Management.Automation.ScopedItemOptions]::Constant
        } |
        Foreach-Object {Set-Variable -Name $_.Name -Value $_.Value}

    if ($ComputerName -and -not (Test-IsLocalhost $ComputerName)) {
        Invoke-Command -ComputerName $ComputerName $ScriptBlock
    } else {
        Start-Job -ScriptBlock $ScriptBlock | Receive-Job -Wait
    }
}

And it works! But only from scripts and from the console.

>> $foo = "bar"
>> Invoke-CommandWithUsingVars -ComputerName localhost {$using:foo}
bar

If I try to use my module in another module? It fails to find the variables in the caller’s scope.

I create a module ScopeTestModule

#ScopeTestModule.psm1
function Test-Foo {
    [cmdletbinding()]
    param()
    $baz = "quux"
    Invoke-CommandWithUsingVars -ComputerName localhost {$using:baz}
}
#ScopeTestModule.psd1
@{
RootModule = 'ScopeTestModule.psm1'
ModuleVersion = '1.0'
GUID = '9c33f05f-ef04-4eb0-a8ba-8084fb63945e'
Author = 'Unkonwn'
CompanyName = 'Unknown'
Copyright = '(c) Unknown.'
}

and then try to test it and get this:

>> Import-Module C:tempscopeTestModuleScopeTestModule.psd1
>> Test-Foo
Start-Job : The value of the using variable '$using:baz' cannot be retrieved because it has not been set in the local
session.
At C:<MYMODULEPATHGOESHERE>.psm1:150 char:3
+         Start-Job -ScriptBlock $ScriptBlock | Receive-Job -Wait
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Job], RuntimeException
    + FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.StartJobCommand

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật