Solved

Powershell Replace

Posted on 2013-02-01
7
185 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 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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