脚本块中的变量未使用PowerShell传递给我的其余代码

Zac*_*ach 2 powershell

在我的代码中,$ CoName并不总是完美的,需要稍微调整一下.$ CoFixes解决了这个问题.但是,当我按下图所示运行它时,$ CoName永远不会进入$ cell.我需要在$ CoFixes中重复使用代码很多次,所以我正在努力学习如何使这项工作.

$CoFixes = {
if ($CoName -eq "L.F. 10' Panel w/o lath"){$CoName = "L.F. of 10' Panel w/o lath"}
if ($CoName -eq "L.F. 9' Panel w/o lath"){$CoName = "L.F. of 9' Panel w/o lath"}
if ($CoName -eq "L.F. 8'2`" Panel w/o lath"){$CoName = "L.F. of 8'2`" Panel w/o lath"}
if ($CoName -eq "L.F. 4' Panel w/o lath"){$CoName = "L.F. of 4' Panel w/o lath"}
if ($CoName -eq "L.F. 4' Panel w/ 8`" top w/o lath"){$CoName = "L.F. of 4' Panel w/ 8`" top w/o lath"}
if ($CoName -eq 'Special Window Openings over 27"'){$CoName = 'Special Window Openings over 37"'}
if ($CoName -eq 'Door Opening up to 41.5" wide'){$CoName = 'Door Opening up to 41 1/2" wide'}
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
&$CoFixes
$cell = $QuoteSheet.range('B1:B60').Find($CoName).offset(0, 3).address(0,0)
$value = $ChangeOrder1Worksheet.range('A20').text
&$vba
$objExcel.run("ChangeOrder", $cell, $value)
write-host $CoName " " $cell " " $value " " $QuoteSheet.range($cell).text
Run Code Online (Sandbox Code Playgroud)

bri*_*ist 5

你有一个范围问题.父作用域中的变量可以从子作用域访问,但是一旦写入它们,它们就会被复制到本地作用域中,这就是您要修改的内容.

将您正在创建的匿名函数视为函数,并返回值:

$CoFixes = {
if ($CoName -eq "L.F. 10' Panel w/o lath"){"L.F. of 10' Panel w/o lath"}
elseif ($CoName -eq "L.F. 9' Panel w/o lath"){"L.F. of 9' Panel w/o lath"}
elseif ($CoName -eq "L.F. 8'2`" Panel w/o lath"){"L.F. of 8'2`" Panel w/o lath"}
elseif ($CoName -eq "L.F. 4' Panel w/o lath"){"L.F. of 4' Panel w/o lath"}
elseif ($CoName -eq "L.F. 4' Panel w/ 8`" top w/o lath"){"L.F. of 4' Panel w/ 8`" top w/o lath"}
elseif ($CoName -eq 'Special Window Openings over 27"'){'Special Window Openings over 37"'}
elseif ($CoName -eq 'Door Opening up to 41.5" wide'){'Door Opening up to 41 1/2" wide'}
else { $CoName }
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = &$CoFixes
Run Code Online (Sandbox Code Playgroud)

为了使其更具惯用性,请尝试使用开关:

$CoFixes = {
    switch($CoName)
    {
        "L.F. 10' Panel w/o lath" {"L.F. of 10' Panel w/o lath"}
        "L.F. 9' Panel w/o lath" {"L.F. of 9' Panel w/o lath"}
        "L.F. 8'2`" Panel w/o lath" {"L.F. of 8'2`" Panel w/o lath"}
        "L.F. 4' Panel w/o lath" {"L.F. of 4' Panel w/o lath"}
        "L.F. 4' Panel w/ 8`" top w/o lath" {"L.F. of 4' Panel w/ 8`" top w/o lath"}
        'Special Window Openings over 27"' {'Special Window Openings over 37"'}
        'Door Opening up to 41.5" wide' {'Door Opening up to 41 1/2" wide'}
        default { $Name }
    }
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = &$CoFixes
Run Code Online (Sandbox Code Playgroud)

然后可能把它放在一个真正的功能:

function Repair-CoName {
    param(
        [String]
        $Name
    )

    switch($Name)
    {
        "L.F. 10' Panel w/o lath" {"L.F. of 10' Panel w/o lath"}
        "L.F. 9' Panel w/o lath" {"L.F. of 9' Panel w/o lath"}
        "L.F. 8'2`" Panel w/o lath" {"L.F. of 8'2`" Panel w/o lath"}
        "L.F. 4' Panel w/o lath" {"L.F. of 4' Panel w/o lath"}
        "L.F. 4' Panel w/ 8`" top w/o lath" {"L.F. of 4' Panel w/ 8`" top w/o lath"}
        'Special Window Openings over 27"' {'Special Window Openings over 37"'}
        'Door Opening up to 41.5" wide' {'Door Opening up to 41 1/2" wide'}
        default { $CoName }
    }
}

$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = Repair-CoName -Name $CoName
Run Code Online (Sandbox Code Playgroud)

等等