Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

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 (Blog | Twitter) and Ron Dameron (Blog | Twitter) 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: 

try {
$a = (Get-Date -uformat "%Y%m%d").ToString()
 Get-ChildItem \\servername\extracts_folder\test\data\*.001 | ForEach-Object -Process{Move-Item -PassThru -Force $_.FullName -Destination \\targetservername\test | Format-Table -AutoSize >> ("\\servername\extracts_folder\test\logs\test_extracts_copy" + $a + ".log")  }
}
 catch {
  "Error occurred on $_" >> \\servername\extracts_folder\test\logs\test_copyerrors.txt
}
 
try {
 Get-ChildItem \\servername\extracts_folder\test\data\*.IDX | ForEach-Object -Process{Move-Item -PassThru -Force $_.FullName -Destination \\targetservername\test | Format-Table -AutoSize >> ("\\servername\extracts_folder\test\logs\test_extracts_copy" + $a + ".log")  }
}
 catch {
  "Error occurred on $_" >> \\servername\extracts_folder\test\logs\test_copyerrors.txt
}

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 SQLServerCentral.com ‘s newsletter on how to Make a 100+ Server Inventory in 30 minutes. 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: 


Get-Content 'c:\serverlist.txt' | ForEach-Object {systeminfo.exe /s $_ } | Out-File c:\test\server_reports.txt

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


Comments

No comments.

Leave a Comment

Please register or log in to leave a comment.