ÌÒ×ÓÊÓƵ

Categories
Documentation PowerShell Syndication

PowerShell I Heart You

PowerShell = Giggity Giggity Goo

Ok so in the last 48 hours I think I’ve fallen madly in love with PowerShell. My time is limited so I’ll make this short. Last night I wrote a script to copy files from one file share to another and log the process. Luckily PowerShell community guru Laerte Junior (| ) and Ron Dameron (| ) were online and willing to help me out (as always). I aksed him some general questions but was determined to write script on my own and just have him review it and point me in right direction. So after much Boogling I got some code example and spat this PowerShell turd out:Ìý

[sourcecode language=”powershell”]
try {
$a = (Get-Date -uformat "%Y%m%d").ToString()
ÌýGet-ChildItem \servernameextracts_foldertestdata*.001 | ForEach-Object -Process{Move-Item -PassThru -Force $_.FullName -Destination \targetservernametest | Format-Table -AutoSize >> ("\servernameextracts_foldertestlogstest_extracts_copy" + $a + ".log")Ìý }
}
Ìýcatch {
ÌýÌý"Error occurred on $_" >> \servernameextracts_foldertestlogstest_copyerrors.txt
}
Ìý
try {
ÌýGet-ChildItem \servernameextracts_foldertestdata*.IDX | ForEach-Object -Process{Move-Item -PassThru -Force $_.FullName -Destination \targetservernametest | Format-Table -AutoSize >> ("\servernameextracts_foldertestlogstest_extracts_copy" + $a + ".log")Ìý }
}
Ìýcatch {
ÌýÌý"Error occurred on $_" >> \servernameextracts_foldertestlogstest_copyerrors.txt
}

[/sourcecode]

Basically it moves the files from one server to another and writes basic information of each file moved to a log file. The log file it writes to is dynamically named based on the date the script is ran. If the script bombs then it writes the errors to another log file the administrator can refer to for troubleshooting.Ìý

Next up this morning I saw an article on ‘s newsletter on how to . 30 minutes? Hell, with PowerShell I think I could knock that out even quicker! And so I have, try 5. First create a text file with a list of all your server names. I’m sure you could get that using PowerShell too but Boogle it. Next try this code out:Ìý

[sourcecode language=”powershell”]

Get-Content ‘c:serverlist.txt’ | ForEach-Object {systeminfo.exe /s $_ } | Out-File c:testserver_reports.txt

[/sourcecode]

ONE line of code and you have a full report of everything on your server. Giggity giggity goo! ALRIGHT!

11 replies on “PowerShell I Heart You”

Now if you wanted to be really sexy you could do a /FO TABLE at the end of that an UPSERT it into a database to keep an up to date queryable inventory of your environment. 😀

Haha damn you sir, your ideas have just tacked on another few hours of hacking because now I’ll do that plus build the SSRS reports to view everything.

[…] This post was mentioned on Twitter by Marlon Ribunal, Jorge Segarra, Chuck Lathrope, mike g, Mark Vaillancourt and others. Mark Vaillancourt said: RT @SQLChicken: [NEW BLOG POST]: PowerShell I Heart You #powershell […]

Sounds great… then you could write a book on Powershell cause you’ll be the guru or awesomeness!!! 🙂

GO CHICKEN GO!!

Lol are you volunteering for another co-authoring gig? You me and Ken could become the new book-writing Muskateers!

If you have your servers in their own OU adding this bit will create your input string.

$strCategory = “computer”
$objDomain = New-Object System.DirectoryServices.DirectoryEntry(“LDAP://OU=yourou,dc=domain,dc=com”)
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDomain,”(objectCategory=$strCategory)”,@(‘name’))
$objSearcher.FindAll() | %{$_.properties.name} | out-File C:testserverlist.txt

Or you could rewrite it to use invoke-command and run it against all your servers in parallel, rather than serially, and I’ll bet it would be down to less than minute. Just mentioned this to Don Jones and he said “Any time you’re using foreach, you’re trying too hard.”

Huh, very cool, hadn’t thought of that approach. I’m probably going to re-write this script with all of your inputs and repost (maybe update this one or post whole new entry for it). Thanks for your replies guys!

I am gettin an error. Get-Content : Cannot find path ‘C:serverlist.txt’ because it does not exist.
At line:1 char:12
+ Get-Content <<<< 'c:serverlist.txt' | ForEach-Object {systeminfo.exe /s $_ } | Out-File
+ CategoryInfo : ObjectNotFound: (C:serverlist.txt:String) [Get-Content], Ite
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

Did you create a text file with all your server names in it called serverlist.txt and place it on the root of your c: drive? If not, my code won’t work but you can create the text file anywhere you want (and name it anything you want for that matter) and simply modify the script to point to the correct location.

I worked on a similar solution to collect Windows and SQL Server inventory
by gathering scripts from your posts and other SQL Family and released
it as a free tool at . The tool uses powershell to
grab data from all servers and stores it in SQL tables and can be viewed
using SSRS reports. Can you please check it and give me any suggestions
to improve it?

Thnx

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam.