Solved

Condtionally Appending a string

Posted on 2013-11-14
4
163 Views
Last Modified: 2013-11-18
I have a SQL select statement in a script file like

Select [Var1], [Var2], [Var3] from [dbase1].[dbo]. [Table1]

I would  like to write a powershell script that I will append 't.' in front of every variable (but not database name [dbase1].[dbo]. [Table1])

For example

Select t.[Var1], t.[Var2], t.[Var3] from [dbase1].[dbo]. [Table1]

How can I make it stop before the word 'from'?
0
Comment
Question by:tommym121
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Subsun
Comment Utility
Try..

$String = "Select [Var1], [Var2], [Var3] from [dbase1].[dbo]. [Table1]"
$NewString = "$(($string -split "from")[0] -replace "\[","t.[")","$(($string -split "from")[1])" -join "From"

$NewString

Open in new window

0
 

Author Comment

by:tommym121
Comment Utility
what happen if there is form within the variable name

Select [Var1], [Var2 from abc], [Var3] from [dbase1].[dbo]. [Table1]

How would I pass so that I can still get this
Select [Var1], [Var2 from abc], [Var3] from [dbase1].[dbo]. [Table1]
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
Comment Utility
Try..
$NewString = "$(($string -split "\] from \[")[0] -replace "\[","t.[")","$(($string -split "\] from \[")[1])" -join "] from ["

Open in new window

0
 

Author Closing Comment

by:tommym121
Comment Utility
Thanks.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now