PowerShell: How to pass variable values between scripts

时间:2016-07-11 22:00:30

标签: powershell

I have two PowerShell scripts. One script (omreg.ps1) contains a function that checks a couple things in the windows registry and returns some values; and the other script (test.ps1) references omreg.ps1, calls the function, and tries to retrieve the values from the variables in omreg.ps1. The variables in omreg.ps1 return the correct values within the function itself, however, the values are not getting passed properly to test.ps1. I have many other functions that do similar things and have no problem passing the variable values from these functions to another PowerShell script. I'm not sure why I'm getting the problem with this particular function. Any help would be appreciated.

omgreg.ps1:

Function CheckOMRegistry
{
    [int]$OMCodeBaseValueFail = 0
    [int]$OMRegPathFail = 0
    [string]$BuildNumber = GC 'E:\buildnumber.txt'
    New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
    [string]$OMGuid = '{E9E7xxxx-6Dxx-49xx-91xx-B919DD45xxxx}'
    [string]$OMRegPath = ('HKCR:\CLSID\' + $OMGuid + '\InprocServer32')

    $OMRegPathExists = Test-Path $OMRegPath
    If ($OMRegPathExists)
    {
        Write-Host "OMRegPath Exists"
        Write-Host "Check CodeBase value ..."
        $OMCodeBaseValue = Get-ItemProperty -Path $OMRegPath -Name 'CodeBase' | Select -Exp 'CodeBase'
        Write-Host "OMCodeBaseValue: $OMCodeBaseValue"
        If ($OMCodeBaseValue.Contains($BuildNumber))
        {
            Write-Host "OM CodeBase Value is Correct" -fore yellow
        }
        Else
        {
            Write-Host "OM CodeBase Value in INCORRECT!" -fore red
            $OMCodeBaseValueFail += 1
        }
    }
    Else
    {
        Write-Host "OMRegPath DOES NOT EXIST!" -fore red
        $OMRegPathFail += 1
    }

    $OMCodeBaseValueFail
    $OMRegPathFail
    $OMRegPath
    $OMCodeBaseValue

    Write-Host
    Write-Host "OMCodeBaseValueFail : $OMCodeBaseValueFail"
    Write-Host "OMRegPathFail : $OMRegPathFail"
    Write-Host "OMRegPath : $OMRegPath"
    Write-Host "OMCodeBaseValue : $OMCodeBaseValue"
}

And here are the returned values (what I expect):

OMCodeBaseValueFail : 0
OMRegPathFail : 0
OMRegPath : HKCR:\CLSID\{E9E7xxxx-6Dxx-49xx-91xx-B919DD45xxxx}\InprocServer32
OMCodeBaseValue : file:///E:/<buildnumber>/Release/someproduct.dll

Here is the second script.

test.ps1:

. $pwd\omreg.ps1
$OMCodeBaseValueFail, $OMRegPathFail, $OMRegPath, $OMCodeBaseValue = CheckOMRegistry

Write-Host
Write-Host "OMCodeBaseValueFail : $OMCodeBaseValueFail"
Write-Host "OMRegPathFail : $OMRegPathFail"
Write-Host "OMRegPath : $OMRegPath"
Write-Host "OMCodeBaseValue : $OMCodeBaseValue"

And here is how the values are getting returned in test.ps1 (not what I expect, should be the same as above):

OMCodeBaseValueFail : HKCR
OMRegPathFail : 0
OMRegPath : 0
OMCodeBaseValue : HKCR:\CLSID\{E9E7xxxx-6Dxx-49xx-91xx-B919DD45xxxx}\InprocServer32 file:///E:/<buildnumber>/Release/someproduct.dll

1 个答案:

答案 0 :(得分:0)

Your first function is writing to the Console host, not writing to an output stream. I don't exactly what format you want the responses returned in, but to capture just the strings, change

Write-Host "OMCodeBaseValueFail : $OMCodeBaseValueFail"
Write-Host "OMRegPathFail : $OMRegPathFail"
Write-Host "OMRegPath : $OMRegPath"
Write-Host "OMCodeBaseValue : $OMCodeBaseValue"

To

Write-Output $OMCodeBaseValueFail
Write-Output $OMRegPathFail
Write-Output $OMRegPath
Write-Output $OMCodeBaseValue

Essentially, you need to have the first function output to the pipeline rather than to the console host. I would suggest creating a custom object and having it return properties with values, like so

$OutputObject = New-Object -TypeName PSObject $OutputObject | Add-Member -Type NoteProperty -Name "OMCodeBaseValueFail" -Value $OMCodeBaseValueFail

Keep adding lines for "$OutputObject | Add-Member..." and at the end

Write-Output $OutputObject

EDIT: Disregard my inane babbling. I see what the problem was, as answered by the comments, and I also see that your function did (implicitly) return values to the pipeline. I'd still recommend having it spit out a custom object with the correct properties, though, to make it easier to reuse elsewhere.