Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell Replace

Posted on 2013-02-01
7
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 71

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
Ready for your healthcare security check-up?

In the past few years, healthcare organizations have become a prime target for advanced attacks. Does your organization have what it needs to defend itself? Schedule your healthcare security check-up today and download our free Healthcare Security Resource Kit today!

 
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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

596 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