Solved

Powershell Replace

Posted on 2013-02-01
7
183 Views
Last Modified: 2013-04-02
I'm trying to pull back some information on logfiles from a server.  The Parent is being pulled back with brackets, [ & ], that I want to remove.  What is the best way of doing this.  I thought this might work but it doesn't.  

ls \SQL\LOCALHOST\DEFAULT\Databases\ | ForEach-Object {$_.LogFiles} | SELECT @{name="Parent";expression={$_.Parent.Replace("[","").Replace("]","")}}

Open in new window


This just returns a bunch of blank values.
0
Comment
Question by:tim_cs
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38844730
I think the problem was with closing brakets for Foreach loop. Try this now.

ls \SQL\LOCALHOST\DEFAULT\Databases\ | ForEach-Object {$_.LogFiles | SELECT @{name="Parent";expression={$_.Parent.Replace("[","").Replace("]","")}}
0
 
LVL 15

Author Comment

by:tim_cs
ID: 38844750
Nope, same result, just a bunch of blank lines.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38844758
FileSystem objects do not have a property of LogFiles, and PS defaults to not display any message, but continue with empty values. Further you should show the filename itself.
get-childitem \SQL\LOCALHOST\DEFAULT\Databases\*.log |
   select @{name="Parent"; expression={$_.DirectoryName.Replace("[","").Replace("]","")}}, Name

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 15

Author Comment

by:tim_cs
ID: 38844813
FileSystem objects very well might not contain a LogFiles property but database servers do which is what I'm using.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38844940
Ooops. But I don't get what you want to get here, the parent of a SQL Transaction Log is the DB?!
0
 
LVL 15

Author Comment

by:tim_cs
ID: 38845185
Yes, if I run this.
ls \SQL\LOCALHOST\DEFAULT\Databases\ | ForEach-Object {$_.LogFiles} | SELECT PARENT

Open in new window

Then I get back
[DBName1]
[DBName2]
[Testdb]

I need to be able to pull that back without the [ ].  I'm pulling back other values as well but to simpify the question I removed those columns.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 38878223
Parent is a Microsoft.SqlServer.Management.Smo.Database Parent object, and cannot be  treated (directly) like a string. But if you use
dir SQLServer:\SQL\LOCALHOST\DEFAULT\Databases\ | 
 SELECT @{name="Parent";expression={$_.LogFiles.Parent -replace "\[|\]" }}

Open in new window

you'll get the stripped DB name. Note that I used the (regex) replace operator instead of the Replace method.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 40
Why Delete * statement wont work with sql server ? 6 42
sql how to count case when 4 17
T-SQL: New to using transactions 9 31
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

810 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