Steve
Steve

Reputation: 17

powershell - variable inside a string variable shall update even after it was declared

As you can see, the variable $SQLRCNumberNew is inside another variable $SQLUpdateRC which has been declared before $SQLRCNumberNew did.

When I am running the script $SQLRCNumberNew is empty, which makes sense because it is declared inside $SQLUpdateRC at the beginning.

What I want to ask if someone has an idea how I can update $SQLRCNumberNew even it is declared at the beginning.

I know I can just put the string Variable right below, but this is not what I am looking for.

Thanks for any help!

$RCFirstDigit = 0
$RCSecondDigit = 0

$PhoenixDBServer = "Servername"
$PhoenixDBName = "Databasename"
$ProductionDBServer = "Servername"
$ProductionDBName = "Databasename"

$SQLSelectRC = $("SELECT [Char_Value_01] FROM [dbo].[TABLENAME] WHERE [Keyword] = 'Version Number'")
$SQLUpdateRC = $("UPDATE TABLENAME set Char_Value_01 = '$SQLRCNumberNew' where Keyword = 'Version Number'")

Switch ($env:COMPUTERNAME) {
    $PhoenixDBServer {
        $Server = $PhoenixDBServer + "." + $env:USERDNSDOMAIN
        $Database = $PhoenixDBName
    }
    $ProductionDBServer {
        $Server = $ProductionDBServer + "." + $env:USERDNSDOMAIN
        $Database = $ProductionDBName
    }
    default {
        Write-Verbose "This script does not run on this computer"
        exit
    }
}

$SQLRCNumber = Invoke-Sqlcmd -ServerInstance $Server -Database $Database -Query $SQLSelectRC

[int]$RCFirstDigit = $SQLRCNumber.Char_Value_01.split(".")[1]
[int]$RCSecondDigit = $SQLRCNumber.Char_Value_01.split(".")[2]

$RCSecondDigit++

# put string back together with updated RC number
[string]$SQLRCNumberNew = $SQLRCNumber.Char_Value_01.Split(".")[0] + "." + $RCFirstDigit + "." + $RCSecondDigit

# Update RC Version on Database
Invoke-Sqlcmd -ServerInstance $Server -Database $Database -Query $SQLUpdateRC

Upvotes: 0

Views: 55

Answers (1)

Mötz
Mötz

Reputation: 1732

Just to be clear - this is not how PowerShell scripting works. When you assign data to your variables, at least when working with strings.

You could have a function inside your script file, that you could call and have it return the concatenated string.

Function StringBuilder {
Param ($Value)
"UPDATE TABLENAME set Char_Value_01 = '$Value' where Keyword = 'Version Number'"
}  


...
$SQLRCNumber = Invoke-Sqlcmd -ServerInstance $Server -Database $Database -Query $SQLSelectRC

$SQLUpdateRC = StringBuild $SQLRCNumber
Invoke-Sqlcmd -ServerInstance $Server -Database $Database -Query $SQLUpdateRC
...

Something like that should work. But I would say you should have a string format defined in the top and simply format when you need it.

$SQLUpdateRCBase = "UPDATE TABLENAME set Char_Value_01 = '{0}' where Keyword = 'Version Number'"

...
$SQLRCNumber = Invoke-Sqlcmd -ServerInstance $Server -Database $Database -Query $SQLSelectRC

$SQLUpdateRC = $SQLUpdateRCBase -f $SQLRCNumber
Invoke-Sqlcmd -ServerInstance $Server -Database $Database -Query $SQLUpdateRC

Upvotes: 1

Related Questions