<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>WinBlogs.... &#187; powershell</title>
	<atom:link href="http://www.winblogs.net/index.php/tag/powershell/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.winblogs.net</link>
	<description>Just another Windows geek blog...</description>
	<lastBuildDate>Thu, 27 Oct 2011 21:56:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>SCVMM? errr&#8230;System Center Virtual Machine Manager and HyperV whining :)</title>
		<link>http://www.winblogs.net/index.php/2011/06/10/scvmm-errr-system-center-virtual-machine-manager-and-hyperv-whining/</link>
		<comments>http://www.winblogs.net/index.php/2011/06/10/scvmm-errr-system-center-virtual-machine-manager-and-hyperv-whining/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 00:06:40 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[hyper v]]></category>
		<category><![CDATA[HyperV]]></category>
		<category><![CDATA[hypervisor]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[virtual]]></category>
		<category><![CDATA[virtualization]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 2008 r2]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=471</guid>
		<description><![CDATA[Couple of days ago I installed SCVMM  in order to test it out, since we are developing our IAAS solution. Everything seemed great until I found out that SVCMM doesnt have any API which can be used to create/turn on/off  new machines.  Yes, I know everything can be scripted thru Powershell, but it sucks that [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://www.winblogs.net/wp-content/uploads/2011/06/frustrated.jpg"><img class="alignleft size-medium wp-image-472" title="frustrated" src="http://www.winblogs.net/wp-content/uploads/2011/06/frustrated-300x300.jpg" alt="" width="232" height="232" /></a></p>
<p>Couple of days ago I installed SCVMM  in order to test it out, since we are developing our IAAS solution. Everything seemed great until I found out that SVCMM doesnt have any API which can be used to create/turn on/off  new machines.  Yes, I know everything can be scripted thru Powershell, but it sucks that we need to create separate applicatons that will give us that feature. Imagine a solution where PRTG, Nagios or any other tools monitors your server&#8230; sees that the CPU load went up ( or any other condition) and starts up a new virtual machine  using API?</p>
<p>Its funny how the guys at MS fails to see the basic requirements for a modern cloud solution; do I have to mention that after years of HyperV being RTM-ed, we still dont have an option to limit a machines uplink speed!? Come on guys&#8230;SERIOUSLY!?!?</p>
<p>&nbsp;</p>
<p>I know that you already have this  little piece of software created, release it&#8230;pretty please!? I know that Microsoft sees HyperV in some giant corporation where they  dont have a need to limit the uplink of a virtual machine, but IMHO,  hosters are having a bigger &#8220;market&#8221; share  than big corporations, think  about us, pretty please with sugar on top <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  .</p>
<p>Oh, what about promiscous mode for network cards? You  have no idea how much would I like to use SNORT on my HyperV virtual switch, but&#8230;I cant <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>What about the limit on only 4 cores per virtual machine? This is a VERY limiting factor. I&#8217;m in a situation where i have a 2x six core, 48 GB ram server which I want to turn into ONE virtual machine ( live migrations, scaling, etc&#8230;), but I cant&#8230;I can only turn it into a 4 core, 44 GB machine, effectively, wasting other 8 cores!?</p>
<p>I would really love to get in touch with someone from Microsoft Virtualization team and send them a list of must-have changes <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  Someone knows some insiders? <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Let me make one thing clear, I love Microsoft, but lets face it, there is lots of work that needs to be done. Admit it or not, the guys at the &#8220;Linux Camp&#8221; have much more options and features regarding virtualization. I really, really love HyperV, but some limitations are just plain unlogical/stupid.</p>
<p>Sorry for a bitter post, but I had to throw out my frustrations <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<div class="shr-publisher-471"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2011/06/10/scvmm-errr-system-center-virtual-machine-manager-and-hyperv-whining/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SpamBots+bad application+SQL server = PROBLEMS!</title>
		<link>http://www.winblogs.net/index.php/2010/10/25/spambotsbad-applicationsql-server-problems/</link>
		<comments>http://www.winblogs.net/index.php/2010/10/25/spambotsbad-applicationsql-server-problems/#comments</comments>
		<pubDate>Mon, 25 Oct 2010 18:45:19 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 2008]]></category>
		<category><![CDATA[windows 2008 r2]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=413</guid>
		<description><![CDATA[So&#8230;today I had a very interesting issue. All of a sudden Nagios went red with the message: CRITICAL : &#8211; total: 232.82 Gb &#8211; used: 212.67 Gb (91%) &#8211; free 20.15 Gb (9%) on one of our SQL server. I was like..&#8221;NO! It cannot be. I clearly remember that this machine had over 150 Gb [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>So&#8230;today I had a very interesting issue. All of a sudden Nagios went red with the message: <strong>CRITICAL : &#8211; total: 232.82 Gb &#8211; used: 212.67 Gb (91%) &#8211; free 20.15 Gb (9%)</strong> on one of our SQL server. I was like..&#8221;NO! It cannot be. I clearly remember that this machine had over 150 Gb of free space yesterday!&#8221;</p>
<p>I log in to the machine, and automatically go to the data folder of SQL server thinking that some customer has a HUGE! log file on his database; so, right click &#8211; Select All -&gt; Properties&#8230; only 50 Gbs of data&#8230;hmm&#8230;not here!</p>
<p>At that moment I notice that the server isnt &#8220;jumpy&#8221; as usual, so I start Performance monitor and see that the Avg. Disk Queue is off the chart! <strong> That definatelly isnt right&#8230;</strong></p>
<p>Since that machine is using Windows 2003, theres no too good way to see what application is using so much disk I/O ( beside FileMon ) , I start the FileMon and se that the SQL service is writing HUGE amount of data to its ERRORLOG file.</p>
<p>By navigating to the folder, I see that the ERRORLOG file is over 120 Gb big so opening it in Notepad, Context or some other file was out of the question, since it would kill the machine completely.</p>
<p>So, lets turn to powershell once more. In order to be sure to not kill the machine, since its in production , I have moved the file to an empty machine to opened it there.</p>
<p>Powershell : cat errorlog +ENTER <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  after that powershell started to list the entire content of the file, and it took about 15 mins until it reached the end. AMAZING</p>
<p>By examining the log, I notice ( it was hard to NOT notice it ) litterally millions of these entries:</p>
<p><strong>Could not allocate space for object &#8216;dbo.tblComments&#8217;.'tblComments&#8217; in database &#8216;DATABASEname&#8217; because the &#8216;PRIMARY&#8217; filegroup is full. Create disk space by deleting unneeded files, dropping objects in the filegroup, adding additional files to the filegroup, or setting autogrowth on for existing files in the filegroup.</strong></p>
<p><strong>What happened here? </strong></p>
<p>The site that was using that database is poorly written and was allowing everyone to post comments on the articles without any security check, so it soon reached the size limit set on the database.  By further digging on the issue, I saw that the bots are posting about 4-5 comments each second which comes to about 18000 comments/hour&#8230;multiply that by 15-16 hours it took to fill the error log to that size, we roughly come to 280000 comments&#8230;that&#8217;s A LOT of comments! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>After disabling the site and sending the email to the customer, situation on the machine looks like this :</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2010/10/mssql.jpg"><img class="alignleft size-medium wp-image-414" title="mssql" src="http://www.winblogs.net/wp-content/uploads/2010/10/mssql-300x213.jpg" alt="" width="300" height="213" /></a></p>
<p><strong>The big drop on the green line is the moment where I disabled the problematic web site.</strong></p>
<div class="shr-publisher-413"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2010/10/25/spambotsbad-applicationsql-server-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automating backup of databases in SQL Express.</title>
		<link>http://www.winblogs.net/index.php/2009/11/22/automating-backup-of-databases-in-sql-20052008-express/</link>
		<comments>http://www.winblogs.net/index.php/2009/11/22/automating-backup-of-databases-in-sql-20052008-express/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 15:45:29 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[backup sql database]]></category>
		<category><![CDATA[backup sql express database]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[sql express]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 2008]]></category>
		<category><![CDATA[windows 2008 r2]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=224</guid>
		<description><![CDATA[Microsoft SQL Server 2005/2008 Express is a free edition of SQL Server that is ideal for learning, developing and powering desktop, web and small server applications, and for redistribution by ISVs. Top Features Supports stored procedures, triggers, functions, and views Store all kinds of business data with native support for relational data, XML, FILESTREAM and spatial [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/mssql-logo.jpg"><img class="alignleft size-full wp-image-253" style="margin: 5px;" title="mssql-logo" src="http://www.winblogs.net/wp-content/uploads/2009/11/mssql-logo.jpg" alt="mssql-logo" width="122" height="73" /></a>Microsoft SQL Server 2005/2008 Express is a free edition of SQL Server that is ideal for learning, developing and powering desktop, web and small server applications, and for redistribution by ISVs.</p>
<h3>Top Features</h3>
<div>
<ul>
<li>Supports stored procedures, triggers, functions, and views</li>
<li>Store all kinds of business data with native support for relational data, XML, FILESTREAM and spatial data</li>
<li>Improved performance, usability, visualization, in addition to integration with the Microsoft 2007 Office System in SQL Server Reporting Services</li>
<li>Simplify development efforts by leveraging existing T-SQL skills, ADO.NET Entity Framework and LINQ</li>
<li>Closely integrated with Visual Studio and Visual Web Developer</li>
</ul>
<p>Because there&#8217;s no &#8220;SQL Agent&#8221; in SQL Express Editions, in order to create scheduled backups on SQL server Express edition, we need to user couple of tricks  <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<div><span id="more-224"></span></div>
<div>First, we need to have a query that will backup all databases to one folder&#8230; Well, here it is <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<p><code><br />
DECLARE @name VARCHAR(50) -- database name<br />
DECLARE @path VARCHAR(256) -- path for backup files<br />
DECLARE @fileName VARCHAR(256) -- filename for backup<br />
DECLARE @fileDate VARCHAR(20) -- used for file name<br />
SET @path = 'E:\sqlbackups\' --path to the backup folder<br />
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)<br />
DECLARE db_cursor CURSOR FOR<br />
SELECT name<br />
FROM master.dbo.sysdatabases<br />
WHERE name NOT IN ('tempdb')<br />
OPEN db_cursor<br />
FETCH NEXT FROM db_cursor INTO @name<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'<br />
BACKUP DATABASE @name TO DISK = @fileName<br />
FETCH NEXT FROM db_cursor INTO @name<br />
END<br />
CLOSE db_cursor<br />
DEALLOCATE db_cursor</code></p>
<p>This will backup everything except the &#8220;Temdb&#8221; database. So, lets take a look at the current state of our backup folder.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup1.PNG"><img class="aligncenter size-medium wp-image-230" title="sqlbackup1" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup1-300x99.PNG" alt="sqlbackup1" width="300" height="99" /></a></p>
<p>As we can see, only the backups from 21.11.2009 exist. So, what happens when we run the query using SQL Management Studio.</p>
<p>Here&#8217;s the query pasted in &#8220;New query window&#8221;.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery.PNG"><img class="aligncenter size-medium wp-image-232" title="sqlquery" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery-300x288.PNG" alt="sqlquery" width="300" height="288" /></a>Sooo&#8230;if everything went smoothly, we should get a message similar to this one :</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery2.PNG"><img class="aligncenter size-medium wp-image-233" title="sqlquery2" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlquery2-300x141.PNG" alt="sqlquery2" width="300" height="141" /></a>Awesome! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Now, lets take a look at our backup folder, are there any changes? Sure hope there is, because this post will totally suck if the backup didn&#8217;t complete <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup2.PNG"><img class="aligncenter size-medium wp-image-234" title="sqlbackup2" src="http://www.winblogs.net/wp-content/uploads/2009/11/sqlbackup2-300x132.PNG" alt="sqlbackup2" width="300" height="132" /></a></p>
<p>As we can see, this post will not suck after all <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Notice the new files, created on the 22.11.2009 . IT WORKS! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  All we have to do now is to automate the backup, so we don&#8217;t have to run the query everyday by hand.</p>
<p>We will do that using Task Scheduler (or Scheduled tasks in Windows 2003). We have to add a task to execute everyday in 3 AM.</p>
<p>The command we have to run is:</p>
<p>&#8220;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE&#8221; -i &#8220;c:\pathtosqlquery\sqlbackup.sql&#8221;</p>
<p>Where &#8220;sqlbackup.sql&#8221; is the name of the file you have pasted the code mentioned before.</p>
<p>Here are a couple of pictures so you can see how to do it:</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/schedule.PNG"><img class="aligncenter size-medium wp-image-235" title="schedule" src="http://www.winblogs.net/wp-content/uploads/2009/11/schedule-300x177.PNG" alt="schedule" width="300" height="177" /></a></p>
<p>Now, click on &#8220;Create task&#8221;. That will click will start the task wizard. Enter the name of the task ( i.e &#8220;SQL Backups&#8221;.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/task1.PNG"><img class="aligncenter size-medium wp-image-237" title="task1" src="http://www.winblogs.net/wp-content/uploads/2009/11/task1-300x223.PNG" alt="task1" width="300" height="223" /></a>After entering the name and choosing the option &#8220;Run wheneter user is logged on or not&#8221;, click on &#8220;Triggers&#8221;.</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK2.PNG"><img class="aligncenter size-medium wp-image-238" title="TASK2" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK2-300x224.PNG" alt="TASK2" width="300" height="224" /></a></p>
<p>Click on &#8220;New&#8221;. It will bring up a new window</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK3.PNG"><img class="aligncenter size-medium wp-image-240" title="TASK3" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK3-300x256.PNG" alt="TASK3" width="300" height="256" /></a>Choose the &#8220;Daily&#8221; option, and set the time when you want the task to run. I have set it to 3 AM .</p>
<p><strong>ONE IMPORTANT THING!!! Dont forget to check the &#8220;Enable&#8221; checkbox <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  , otherwise, you will end up scratching your head and cursing Microsoft and uncle Bill <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  hehe.</strong></p>
<p>After that, click on &#8220;Actions&#8221;, where we will define the program that should be runned&#8230; Click on &#8220;New&#8221;&#8230;</p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK4.PNG"><img class="aligncenter size-medium wp-image-241" title="TASK4" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK4-300x234.PNG" alt="TASK4" width="300" height="234" /></a></p>
<p><a href="http://www.winblogs.net/wp-content/uploads/2009/11/TASK5.PNG"><img class="aligncenter size-medium wp-image-242" title="TASK5" src="http://www.winblogs.net/wp-content/uploads/2009/11/TASK5-276x300.PNG" alt="TASK5" width="276" height="300" /></a>Paste this into the &#8220;Program/Script&#8221; box.</p>
<p><code>“C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE”</code></p>
<p><strong>NOTE! &#8211; path to the &#8220;SQLCMD.exe&#8221; may be different on your system!!!</strong></p>
<p>All that is left to do is to &#8220;Add arguments&#8221;</p>
<p><code>-i “c:\pathtosqlqueryfile\sqlbackup.sql” </code></p>
<p>Click on &#8220;OK&#8221;, enter the system password and thats it. Enjoy your daily backup.</p>
<p><strong>HINT!!!! If you&#8217;re lazy like I am, you can use a powershell script to delete backup files older than X days. I have also covered that with a blog post. More info about that on this <a href="http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/">link</a>.</strong></p>
<p>Bye bye! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="shr-publisher-224"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2009/11/22/automating-backup-of-databases-in-sql-20052008-express/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powershell cheat sheet :)</title>
		<link>http://www.winblogs.net/index.php/2009/10/20/powershell-cheat-sheet/</link>
		<comments>http://www.winblogs.net/index.php/2009/10/20/powershell-cheat-sheet/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 00:55:56 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[cheat sheet]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[Poweshell]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 2008]]></category>
		<category><![CDATA[windows 2008 r2]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=147</guid>
		<description><![CDATA[Surfing all over the net, I found a very handy powershell cheat sheet that was put together by Ben Pearce. Anyways, here&#8217;s the download link (just click on the picture). Big hello to the guys from www.bug.hr]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Surfing all over the net, I found a very handy powershell cheat sheet that was put together by Ben Pearce. Anyways, here&#8217;s the download link (just click on the picture).</p>
<p style="text-align: center;"><a href="http://www.winblogs.net/Powershell_Cheat_Sheet.pdf"><img class="size-medium wp-image-150 aligncenter" title="cheatsheet" src="http://www.winblogs.net/wp-content/uploads/2009/10/cheatsheet-211x300.PNG" alt="cheatsheet" width="211" height="300" /></a></p>
<p>Big hello to the guys from <a href="http://www.bug.hr">www.bug.hr</a> <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="shr-publisher-147"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2009/10/20/powershell-cheat-sheet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Add new ip or ip range to firewall rule using Powershell</title>
		<link>http://www.winblogs.net/index.php/2009/10/14/add-new-ip-or-ip-range-to-firewall-rule-using-powershell/</link>
		<comments>http://www.winblogs.net/index.php/2009/10/14/add-new-ip-or-ip-range-to-firewall-rule-using-powershell/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 19:34:10 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Servers]]></category>
		<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[Poweshell]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[windows 2008]]></category>
		<category><![CDATA[windows 2008 r2]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=91</guid>
		<description><![CDATA[Tired of old point and click interface? Powershell is the new purple in computer world haha&#8230;Seriously! So, here&#8217;s a little script that will add the Ip address you want to the existing firewall rule. As you can see, it fairly simple, and there&#8217;s no need to explain it too much&#8230;. Function IP { param ($ipadress [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://www.winblogs.net/wp-content/uploads/2009/10/firewalljpg.jpg"><img class="alignleft size-full wp-image-210" title="firewalljpg" src="http://www.winblogs.net/wp-content/uploads/2009/10/firewalljpg.jpg" alt="firewalljpg" width="126" height="126" /></a></p>
<p>Tired of old point and click interface? <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Powershell is the new purple in computer world <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  haha&#8230;Seriously! <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So, here&#8217;s a little script that will add the Ip address you want to the existing firewall rule.</p>
<p>As you can see, it fairly simple, and there&#8217;s no need to explain it too much&#8230;.</p>
<p><code><br />
Function IP<br />
{<br />
param ($ipadress = $(Read-host "Enter IP address"))<br />
$rule = "YOUR PREVIOUSLY CREATED RULE NAME"<br />
Write-host "Adding address :" $ipadress -foregroundcolor "Red"<br />
Start-sleep -s 5<br />
netsh advfirewall firewall set rule name="$rule" new remoteip="$ipadress" action="allow"<br />
}<br />
IP<br />
</code></p>
<p>Only thing you have to do in the script is to enter your fireall rule name.</p>
<p>When you start the script, it  will ask you to enter desired ip address, and press enter. After that, wait 5 seconds and thats it <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Cheers!</p>
<div class="shr-publisher-91"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2009/10/14/add-new-ip-or-ip-range-to-firewall-rule-using-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete files older than&#8230;. in Powershell</title>
		<link>http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/</link>
		<comments>http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 23:57:53 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[delete files older than days]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[scheduled task]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=41</guid>
		<description><![CDATA[Recently I&#8217;ve found myself in a situation where I had to periodically delete files older than 7 days. I could have done it manually, but being busy, I often forgot to do it. The script I&#8217;m about to show you is a bit modified than the original, but, the principle is the same. Here I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><img class="alignleft size-full wp-image-51" title="delete_key_eraser_inhand" src="http://www.winblogs.net/wp-content/uploads/2009/10/delete_key_eraser_inhand.jpg" alt="delete_key_eraser_inhand" width="105" height="105" />Recently I&#8217;ve found myself in a situation where I had to periodically delete files older than 7 days. I could have done it manually, but being busy, I often forgot to do it. The script I&#8217;m about to show you is a bit modified than the original, but, the principle is the same. Here I&#8217;ve added a posibilty to manually enter the directory name. In your script, you can define it, so it is being run automatically, without your intervention.</p>
<p>Here&#8217;s the code :</p>
<p><code> Function GetOldFile<br />
{<br />
param ($Dir = $(Read-host "Unesi putanju do direktorija")),<br />
($Days = $(Read-Host "Brišem fileove starije od koliko dana?"))<br />
$TargetFolder = $Dir<br />
if (Test-Path $TargetFolder)</code></p>
<p><code> {<br />
Write-host "DIREKTORIJ JE :" $TargetFolder -foregroundcolor "Red"<br />
#Write-Host `a `a `a `a `a - odkomentirati ako zelimo da pc speaker vristi <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Write-Host "UPS, krivi  folder! Stisni 'Ctrl + C' za prekid - imas 5 sekundi za prekid" -foregroundcolor "Yellow"<br />
Start-sleep -s 5<br />
$Now = Get-Date<br />
$LastWrite = $Now.AddDays(-$days)<br />
$Files = get-childitem $TargetFolder -include *.log -recurse |Where {$_.LastWriteTime -le "$LastWrite"}<br />
foreach ($File in $Files)<br />
{write-host "Brišem file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}<br />
}<br />
Else<br />
{Write-Host "Direktorij $TargetFolder ne postoji! Provjeri path!"}<br />
}<br />
GetOldFile</code></p>
<p>The questions in the script are in Croatian, but anyone with a little bit of programming experience will be able to change it, because the script is pretty straight forward. But, if you&#8217;re lazy as most of us are, here is the translated script <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-41"></span></p>
<p><code> Function GetOldFile<br />
{<br />
param ($Dir = $(Read-host "Enter destination folder path")),<br />
($Days = $(Read-Host "Delete files older than how many days ?"))<br />
$TargetFolder = $Dir<br />
if (Test-Path $TargetFolder)<br />
{<br />
Write-host "Target folder path is :" $TargetFolder -foregroundcolor "Red"<br />
#Write-Host `a `a `a `a `a - uncomment if you would like to hear the pc speaker #warning <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Write-Host "WRONG FOLDER! Press 'Ctrl + C' to abort"-foregroundcolor "Yellow"<br />
Start-sleep -s 5<br />
$Now = Get-Date<br />
$LastWrite = $Now.AddDays(-$days)<br />
$Files = get-childitem $TargetFolder -include *.log -recurse |Where {$_.LastWriteTime -le "$LastWrite"}<br />
foreach ($File in $Files)<br />
{write-host "Deleting file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}<br />
}<br />
Else<br />
{Write-Host "Folder $TargetFolder doesnt exist! Recheck the folder path!"}<br />
}<br />
GetOldFile<br />
</code></p>
<p>Be careful with this script, because it will delete all the files in the subfolder also! Notice the &#8220;<strong>-recurse</strong>&#8221; in the script..</p>
<p>Now, to make your life even easier, you can add this script to scheduled tasks and set it to run whenever you need it.</p>
<p>First thing that we need to do is to set the folder path as a constant value. Instead of<br />
<code><br />
param ($Dir = $(Read-host "Enter destination folder path")),<br />
($Days = $(Read-Host "Delete files older than how many days ?"))<br />
</code><br />
we need to define<strong> $Dir</strong> and <strong>%Days</strong> in the script. That is done like this:<br />
<code><br />
$Days = "2"<br />
$TargetFolder = "C:\pathtofolder"<br />
</code></p>
<p>So the script looks like this:<br />
<code><br />
Function GetOldFile<br />
{<br />
$Days = "2"<br />
$TargetFolder = "</code><code>C:\pathtofolder</code><code>"<br />
if (Test-Path $TargetFolder)<br />
{<br />
$Now = Get-Date<br />
$LastWrite = $Now.AddDays(-$days)<br />
$Files = get-childitem $TargetFolder -include *.log -recurse |Where {$_.LastWriteTime -le "$LastWrite"}<br />
foreach ($File in $Files)<br />
{write-host "Deleting file $File" -foregroundcolor "Red"; Remove-Item $File | out-null}<br />
}<br />
Else<br />
{Write-Host "Folder $TargetFolder doesnt exist! Recheck the folder path!"}<br />
}<br />
GetOldFile<br />
</code></p>
<p>Notice that I have removed couple of lines from that script that are not neccesary to the script to function properly. If you like, you can also remove the line where the script writes out the names of the files that are being deleted.</p>
<p>Second thing you need to do is make sure that Powershell is set to execute Powershell scripts, instead of only allowing interactive commands to be run in the Powershell environment.</p>
<p>Type the following at the Powershell command prompt:<br />
<code><br />
set-executionpolicy RemoteSigned</code></p>
<p>After that, create a <strong>.bat</strong> file and name it whatever you like ( i.e dltfilesolderthanXYday.bat) and paste the following entry in:</p>
<p><code> powershell -command "&amp; 'SCRIPTNAME.ps1' "<br />
</code><br />
All that is left to do is to create a Scheduled Task in Windows. Add a task that will run the .bat file every number of days you need.</p>
<p>Enjoy!</p>
<div class="shr-publisher-41"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2009/10/01/delete-files-older-from-in-powershell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Powershell CAT and GREP-like stuff!</title>
		<link>http://www.winblogs.net/index.php/2009/09/30/a-few-powershell-useful-tricks/</link>
		<comments>http://www.winblogs.net/index.php/2009/09/30/a-few-powershell-useful-tricks/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 00:04:20 +0000</pubDate>
		<dc:creator>Alesandro Slepčević</dc:creator>
				<category><![CDATA[Windows and Powershell]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[cat]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[windows]]></category>
		<guid isPermaLink="false">http://www.winblogs.net/?p=12</guid>
		<description><![CDATA[Working as a Windows server administrator many times I found myself in a situation where I have to check 50+ Mb log file for only one e-mail address or some other term. Ofcourse, many of you are going to think: &#8220;Yeah, like that&#8217;s a problem, just press CRTL+F and type in the term &#8221; ,and [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><img class="alignleft size-full wp-image-29" style="border: 0pt none; margin: 1px 5px;" title="cat1" src="http://www.winblogs.net/wp-content/uploads/2009/09/cat1.jpg" alt="cat1" width="99" height="164" />Working as a Windows server administrator many times I found myself in a situation where I have to check 50+ Mb log file for only one e-mail address or some other term. Ofcourse, many of you are going to think: &#8220;Yeah, like that&#8217;s a problem, just press CRTL+F and type in the term <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> &#8221; ,and you&#8217;re right. That way rocks when you have 10, or maximum 20 entries where that term is mentioned ( i.e some e-mail address) in the entire log file .  But, what to do when you&#8217;re in a situation that a client wants to see the logs for his entire domain. All sent e-mail, all incoming e-mail, everything&#8230;then, it gets a little too much to do &#8220;crtl+f&#8221;  and copy/paste the result in some other file which you are going to send to the client.  Linux users have their way to do that, simply by : <strong>cat filename.txt | grep term &gt; outputfile.txt</strong> .  By starting to explore powershell, my first task was to find a similar command to do the same thing on a Windows server. Doing a little searching on the web I found the solution that is really simple and similar to linux command <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  hooray for me <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-12"></span></p>
<p>The Unix “<a href="http://en.wikipedia.org/wiki/Cat_%28Unix%29">cat</a>” command is used to concatenate and display files.   Given a file or filename list, it will print the contents of that file to standard output.  There are several options in the Unix command that are implemented with the following PowerShell arguments:</p>
<table border="0">
<tbody>
<tr>
<th>Unix</th>
<th>PowerShell</th>
<th>Description</th>
</tr>
<tr>
<td align="center">-b</td>
<td align="center">filespec</td>
<td align="center">A file or file matching pattern to concatenate.</td>
</tr>
<tr>
<td align="center">-n</td>
<td align="center">-number</td>
<td align="center">Prefix all output lines with line numbers</td>
</tr>
<tr>
<td align="center">-b</td>
<td align="center">-number_nonblank</td>
<td align="center">Prefix nonblank output lines with numbers.</td>
</tr>
<tr>
<td align="center">-E</td>
<td align="center">-show_ends</td>
<td align="center">display “$” at the end of each line.</td>
</tr>
<tr>
<td align="center">-T</td>
<td align="center">-show_tabs</td>
<td align="center">Display TAB characters as “^I”</td>
</tr>
<tr>
<td align="center">-s</td>
<td align="center">-squeeze_blanks</td>
<td align="center">Never more than one consecutive single blank line</td>
</tr>
</tbody>
</table>
<p>But, as we can see, no <strong> &#8220;grep&#8221;</strong> command like on linux <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  . Again, some more searching and I found a command called &#8220;findstr&#8221; .</p>
<p>Example:</p>
<p><strong>c:\cat logs.tx</strong>t  &#8211; will print all the content of that file.</p>
<p><code> #Software: Microsoft Internet Information Services 6.0<br />
#Version: 1.0<br />
#Date: 2009-09-28 15:30:08<br />
#Fields: date time s-sitename s-computername s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs-version cs(User-Agent) cs(Cookie) cs(Referer) cs-host sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken<br />
2009-09-28 15:30:08 W3SVC1958603616 WIN7 89.201.164.205 GET /index.php - 80 - 89.201.144.106 HTTP/1.1 Mozilla/5.0+(X11;+U;+Linux+x86_64;+en-US;+rv:1.9.0.14)+Gecko/2009090217+Ubuntu/9.04+(jaunty)+Firefox/3.0.14 - - www.winblogs.net.win7.mojsite.com 500 0 0 1900 406 31<br />
2009-09-28 15:30:08 W3SVC1958603616 WIN7 89.201.164.205 GET /wp-admin/css/install.css - 80 - 89.201.144.106 HTTP/1.1 Mozilla/5.0+(X11;+U;+Linux+x86_64;+en-US;+rv:1.9.0.14)+Gecko/2009090217+Ubuntu/9.04+(jaunty)+Firefox/3.0.14 - http://www.winblogs.net.win7.mojsite.com/ www.winblogs.net.win7.mojsite.com 200 0 0 2802 437 15<br />
2009-09-28 15:30:08 W3SVC1958603616 WIN7 89.201.164.205 GET /wp-admin/images/white-grad.png - 80 - 89.201.144.106 HTTP/1.1 Mozilla/5.0+(X11;+U;+Linux+x86_64;+en-US;+rv:1.9.0.14)+Gecko/2009090217+Ubuntu/9.04+(jaunty)+Firefox/3.0.14 - http://www.winblogs.net.win7.mojsite.com/wp-admin/css/install.css www.winblogs.net.win7.mojsite.com 200 0 0 460 482 15</code></p>
<p>Now, lets say that we want to see the lines only where the word <strong>&#8220;username&#8221;</strong> is mentioned. As you can see, there is much other stuff in the output that we dont need, so we will simple do this:</p>
<p><strong>c:\cat logs.txt | findstr username</strong></p>
<p>and it will print out the lines where the term <strong>&#8220;username&#8221;</strong> is mentioned.<br />
<code><br />
#Fields: date time s-sitename s-computername s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs-version cs(User-Agent) cs(Cookie) cs(Referer) cs-host sc-status sc-substatus sc-win32-status sc-bytes cs-bytes time-taken<br />
#Fields: date time s-sitename s-computername s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs-version cs(User-Agent) cs(Cookie) cs(Referer) cs-host<br />
</code></p>
<p>AWESOME, we got 60 percent of the work done! All we need to do now is to copy that output to a file. Yes, we could do copy/paste, but that is boring <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  . All we have to do is add  &#8221; &gt; filename.txt&#8221; to the end of the cat command.</p>
<p>Example:</p>
<p><strong>c:\ cat logs.txt  | findstr username &gt; outputfile.txt</strong></p>
<p>And bingo! All the lines where the term <strong>&#8220;username&#8221; </strong>is mentioned are now written to the file called <strong>&#8220;outputfile.txt&#8221;</strong> &#8211; ofcourse, you can name it whatever you like <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>It made my life easier <img src='http://www.winblogs.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Cheers!</p>
<div class="shr-publisher-12"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.winblogs.net/index.php/2009/09/30/a-few-powershell-useful-tricks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

