array with single quotes

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Christian Sandfeld Christian Sandfeld 7 months, 1 week ago.

  • Author
    Posts
  • #38517
    Profile photo of Nick Britton
    Nick Britton
    Participant

    I am attempting to use an array of servers that i have in a sql query. But for that i need each server to be listed with single quotes.

    Here is what i have:

    $serverarray = @(server1, server2, server3)

    for the query to work i need to build a query command file. I am doing this with:

    $sqlcmd = "some long sting here including ('server1', 'server2', 'server3')"
    $sqlcmd |out-file -encoding ascii -filepath $filepathvar

    How can i get the servers to list with single ticks?

  • #38520
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Put the single quotes within double quotes or use a here-string.

    $serverarray = @("'server1'", "'server2'", "'server3'")

    – or –

    $serverarray = @"
    'server1'
    'server2'
    'server3'
    "@ -split "`r`n"
    
    $sqlcmd = "some long sting here including ($($serverarray -join ','))"
    $sqlcmd | out-file -encoding ascii -filepath $filepathvar
  • #38534
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    You can also do like this:

    $sqlcmd = "some long sting here including ('$($serverarray -join "','")')"
  • #38542
    Profile photo of Nick Britton
    Nick Britton
    Participant

    Thanks Christian , that is exactly what i was looking to do. That will allow me to use the same array of servers for multiple purposes.

  • #38574
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    Happy to help

You must be logged in to reply to this topic.