如何在powershell中连接网络路径和combobox变量

如何在powershell中连接网络路径和combobox变量,combobox,powershell-3.0,Combobox,Powershell 3.0,请帮忙。我有以下函数的一个例子 PROCESS { $ServersArray = @('localhost') foreach ($serverArray in $ServersArray) { try { if ($WebConfig.SelectedIndex -gt -1) { Write-Host -ForegroundColor Cyan "Applying Maintenance on $S

请帮忙。我有以下函数的一个例子

PROCESS {
    $ServersArray = @('localhost')
    foreach ($serverArray in $ServersArray) {
        try {
            if ($WebConfig.SelectedIndex -gt -1) {  
                Write-Host -ForegroundColor Cyan "Applying Maintenance on $ServerArray"
                $everything_ok = $true
                Invoke-Command  $serverArray -ScriptBlock { 
                    $filePath = "D:\\Inetpub\\MyHL3Ordering\\Configuration\\MyHL" + "\\" + $WebConfig.SelectedItem

                    (Get-Content $filePath) | ForEach-Object {
                        $_ -replace 'allowDO="true"','allowDO="false"'
                    } | Set-Content $filePath -Encoding UTF8; 
                }  -ErrorAction 'Stop'
            }
因此,基本上我希望将路径与combobox所选项目连接起来。例如如果所选项目为web_da-DK.config,则路径应为 “D:\Inetpub\MyHL3Ordering\Configuration\MyHL\web\u da-DK.config”,但它不工作

错误是:

Cannot find part of the path 'D:\Inetpub\MyHL3Ordering\Configuration\MyHL\' it doesnt seem to concatenate the value of combobox selectedItem  to the path. 

请让我知道我做错了什么

问题在于,您试图使用的变量的作用域不存在is。如果运行以下命令,可以阅读有关作用域的更多信息:

Get-Help about_scopes
由于您使用的是PowerShell v3,因此可以使用using scope修饰符。从
关于作用域的帮助中

使用范围修饰符

Using是一个特殊的范围修饰符,用于标识本地 远程命令中的变量。默认情况下,远程 假定命令是在远程会话中定义的

Windows PowerShell 3.0中引入了使用范围修改器

有关更多信息,请参阅关于远程变量

它还建议阅读关于远程变量的
,其中说明:

使用局部变量

You can also use local variables in remote commands, but you must
indicate that the variable is defined in the local session.

Beginning in Windows PowerShell 3.0, you can use the Using scope
modifier to identify a local variable in a remote command.

The syntax of Using is as follows:

   The syntax is:
       $Using:<VariableName>       
这将产生以下输出:

C:\rootpath
如果我们将脚本更改为使用Using scope修饰符来指示我们要使用远程作用域中的局部变量,我们将得到如下代码:

$serverArray = "localhost"
$filename = "somefile.txt"
Invoke-Command -ComputerName $ServerArray -ScriptBlock {
    $concatenated = [System.IO.Path]::Combine("C:\rootpath", $filename)
    Write-Host $concatenated
}
$serverArray = "localhost"
$filename = "somefile.txt"
Invoke-Command -ComputerName $ServerArray -ScriptBlock {
    $concatenated = [System.IO.Path]::Combine("C:\rootpath", $Using:filename)
    Write-Host $concatenated
}
这将产生我们想要的输出,即:

C:\rootpath\somefile.txt

因此,您需要做的是使用
-ArgumentList
参数将其作为参数传递给
Invoke命令
函数,或者(因为您使用的是PowerShell v3)指出您的变量是一个局部变量,并像上面的示例一样使用Using scope修饰符。

您是否可以包括创建和添加组合框的脚本部分,以及导致执行上述代码的脚本部分?刚才注意到您正试图从不同的范围访问变量在远程计算机上执行
调用命令时。我建议通过运行命令
gethelp about\u scopes
@robert.westerlund来阅读scopes,我正在powershell studio中尝试上述代码。如果我运行以下命令:Invoke命令$serverArray-scriptblock{$filePath=“D:\Inetpub\MyHL3Ordering\Configuration\MyHL\Web_da-DK.config”(获取内容$filePath)| foreach对象{$|-replace'allowDO=“true”,'allowDO=“false”}|设置内容$filePath-编码UTF8;}-Credential$cred上述代码工作正常,但如果我尝试继续,它将不起作用。但是,如果我尝试在scriptblock之外进行连接,在调试时我可以看到整个连接路径。如果它在本地作用域中工作正常,那么您的问题很可能是作用域问题。我根据这个假设写了一个答案。