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
Solved

Powershell Replace

Posted on 2013-02-01
7
184 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial 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 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

840 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