medinibster
medinibster

Reputation: 57

how to concatenate a network path and combobox variable in powershell

please help. I have a the following function.

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'
            }

so basically I would like to concatenate the path with the combobox selected item. for example. if the selected item is web_da-DK.config , the path should be 'D:\Inetpub\MyHL3Ordering\Configuration\MyHL\web_da-DK.config' but it is not working.

error is:

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

Please let me know what am I doing wrong.

Upvotes: 0

Views: 265

Answers (1)

Robert Westerlund
Robert Westerlund

Reputation: 4838

The problem is that you are trying to use a variable from a scope in which is does not exist. You can read more about scopes if you run the following command:

Get-Help about_scopes

Since you are using PowerShell v3 you can use the Using scope modifier. From the help on about_scopes:

The Using scope modifier

Using is a special scope modifier that identifies a local variable in a remote command. By default, variables in remote commands are assumed to be defined in the remote session.

The Using scope modifier is introduced in Windows PowerShell 3.0.

For more information, see about_Remote_Variables.

It further suggest reading the about_Remote_Variables, which states:

USING LOCAL VARIABLES

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>       

In order to take an example of this, we could make a sample first which tries to use the local variable immediately, like the following:

$serverArray = "localhost"
$filename = "somefile.txt"
Invoke-Command -ComputerName $ServerArray -ScriptBlock {
    $concatenated = [System.IO.Path]::Combine("C:\rootpath", $filename)
    Write-Host $concatenated
}

This will yield the following output:

C:\rootpath

If we change the script to use the Using scope modifier to indicate that we want to use a local variable from the remote scope, we get code like the following:

$serverArray = "localhost"
$filename = "somefile.txt"
Invoke-Command -ComputerName $ServerArray -ScriptBlock {
    $concatenated = [System.IO.Path]::Combine("C:\rootpath", $Using:filename)
    Write-Host $concatenated
}

This will yield the output which we want, that is:

C:\rootpath\somefile.txt

So what you need to do is to either pass it as an argument to the Invoke-Command function, using the -ArgumentList parameter, or (since you are using PowerShell v3) indicate that your variable is a local variable and use the Using scope modifier like the examples above.

Upvotes: 1

Related Questions