I’m very slowly revising my main web site. The upshot for now is that there is no direct link to my old script library. Many of my Mr. Roboto tools can be found on this page. Until I can finish the upgrade project, you can use this link, http://www.jdhitsolutions.com/scripts.htm to take you directly to the old page. Enjoy.
Tag Archives: Mr. Roboto
R.I.P. Mr. Roboto
I have some sad news to share today. The March 2010 Mr. Roboto column is to be the last. It seems that REDMOND is revamping somewhat and my column is being retired. But it has been a long run, and to be honest, the column has run its course. I’ve been doing the column for several years, and the original Mr. Roboto, Don Jones, did it for awhile before then. So it’s probably time.
All of the past Mr. Roboto articles will remain available online. I expect to contribute to REDMOND in the future in other ways so I hope you’ll stay tuned. And (as far as I know now), my Prof. PowerShell columns will continue to be published on MCPMag.com.
I appreciate all the warm, and thoughtful comments over the years about the column. I hope it has helped in some small way at one time or another. I’ll continue the Mr. Roboto philosophy in other projects and this blog.
It will be nice to get out of this iron monkey suit.
Domo Arigato
Putting the Squeeze on Files with PowerShell
My December Mr. Roboto column is now online This month’s tool is a PowerShell WinForm script that uses WMI to compress files. I used PrimalForms 2009 to build the graphical interface. The interface is essentially a wizard that lets you build a WMI query to find files and compress them.
Results can be logged to a CSV file or you can merely list the files that match the search criteria. Here’s a code excerpt.
|
1 |
$Compress={ |
|
1 |
<span style="color: #008000">#initialize some variables</span> |
|
1 |
$filters=@() |
|
1 |
$i=0 |
|
1 |
$richtextbox1.Clear() |
|
1 |
$drive=$txtPath.Text.Substring(0,2) |
|
1 |
$<span style="color: #0000ff">filter</span>=<span style="color: #006080">"Drive='$drive' AND Compressed='False' AND Encrypted='FALSE'"</span> |
|
1 |
$blnLog=$false |
|
1 |
|
1 |
$form1.Cursor=<span style="color: #006080">"WaitCursor"</span> |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($chkLogResults.checked) { |
|
1 |
<span style="color: #008000">#create a file for logging</span> |
|
1 |
$timestamp=(get-date).toString(<span style="color: #006080">'yyyMMddhhmmss'</span>) |
|
1 |
$logfile=<span style="color: #006080">"Squeeze_$timestamp.log"</span> |
|
1 |
$blnLog=$True |
|
1 |
<span style="color: #008000">#create the header</span> |
|
1 |
<span style="color: #006080">"Server,Name,Extension,FileSize,LastModified,CreationDate,Drive,Path,Result"</span> | out-file $logfile |
|
1 |
} |
|
1 |
  |
|
1 |
<span style="color: #008000">#check other filter options</span> |
|
1 |
<span style="color: #0000ff">if</span> ($chkSize.Checked) { |
|
1 |
<span style="color: #008000">#add file size to filter</span> |
|
1 |
[int]$FileSizeValue=$txtFileSize.Text |
|
1 |
$size=$FileSizeValue*1KB |
|
1 |
$<span style="color: #0000ff">filter</span>+=<span style="color: #006080">" AND filesize >= $size"</span> |
|
1 |
} |
|
1 |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($chkDate.Checked) { |
|
1 |
<span style="color: #008000">#convert datetime to a WMI datetime</span> |
|
1 |
$datetime=[System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($datetimepicker1.Value) |
|
1 |
$<span style="color: #0000ff">filter</span>+=<span style="color: #006080">" AND LastModified >= '$datetime'"</span> |
|
1 |
} <span style="color: #008000">#end if $chkDate.Checked</span> |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($chkFileExtensions.Checked) { |
|
1 |
$arrExtensions=$txtExtensions.Text.split(<span style="color: #006080">","</span>) |
|
1 |
$extensionFilter=<span style="color: #006080">" "</span> |
|
1 |
<span style="color: #0000ff">for</span> ($c=0;$c <span style="color: #cc6633">-lt</span> $arrExtensions.Count;$c++) { |
|
1 |
$extensionFilter+=<span style="color: #006080">"extension='$($arrExtensions[$c])'"</span> |
|
1 |
<span style="color: #0000ff">if</span> ($c <span style="color: #cc6633">-ne</span> $arrExtensions.count-1) { |
|
1 |
$extensionFilter+=<span style="color: #006080">" OR "</span> |
|
1 |
} |
|
1 |
} |
|
1 |
|
1 |
$<span style="color: #0000ff">filter</span>+=<span style="color: #006080">" AND ($extensionFilter)"</span> |
|
1 |
} <span style="color: #008000">#end if $chkFileExtensions.Checked</span> |
|
1 |
|
1 |
<span style="color: #008000">#create a copy of the filter up to this point</span> |
|
1 |
$basefilter=$<span style="color: #0000ff">filter</span> |
|
1 |
|
1 |
<span style="color: #008000">#get base path</span> |
|
1 |
<span style="color: #0000ff">if</span> ($txtPath.Text.Length <span style="color: #cc6633">-eq</span> 3) { |
|
1 |
<span style="color: #008000">#check if drive root</span> |
|
1 |
$filepath=<span style="color: #006080">"\\" |
|
1 |
$filter+="</span> AND path=<span style="color: #006080">'$filepath'</span><span style="color: #006080">" |
|
1 |
} |
|
1 |
else { |
|
1 |
#parse out file path |
|
1 |
$filepath=$txtPath.Text.Substring(2) |
|
1 |
#replace \ with \\ |
|
1 |
$filepath=$filepath.Replace("</span>\<span style="color: #006080">","</span>\\<span style="color: #006080">") |
|
1 |
$filter+="</span> AND path=<span style="color: #006080">'$filepath\\'" |
|
1 |
} |
|
1 |
|
1 |
#add the base path to the set of filters |
|
1 |
$filters+=$filter |
|
1 |
|
1 |
if ($chkRecurse.Checked) { |
|
1 |
  |
|
1 |
#process subfolders |
|
1 |
$p=$txtPath.Text |
|
1 |
dir $p -rec | |
|
1 |
where {$_.psIsContainer} | foreach { |
|
1 |
#replace \ with \\ and append the trailing \\ |
|
1 |
$filepath="{0}\\" -f $_.fullname.substring(2).replace("\","\\") |
|
1 |
#append each filepath to a copy of the basefilter |
|
1 |
$tempFilter=$basefilter |
|
1 |
$tempfilter+=" AND path='</span>$filepath'<span style="color: #006080">" |
|
1 |
|
1 |
#add each subfolder path to the collection of WMI query filters |
|
1 |
$filters+=$tempfilter |
|
1 |
} #end foreach |
|
1 |
|
1 |
} #end if $chkRecurse.checked |
|
1 |
|
1 |
  |
|
1 |
foreach ($item in $filters) { |
|
1 |
#run a WMI query for each filter |
|
1 |
$cmd=("</span>get-wmiobject -class CIM_DataFile -<span style="color: #0000ff">filter</span> <span style="color: #006080">""</span>{0}<span style="color: #006080">""</span><span style="color: #006080">" -f "</span>$item<span style="color: #006080">") |
|
1 |
|
1 |
# write-host $item -foregroundcolor CYAN |
|
1 |
|
1 |
$statusbar1.text="</span>Running Query $item<span style="color: #006080">" |
|
1 |
$files=Invoke-Expression $cmd |
|
1 |
|
1 |
if ($files) { |
|
1 |
foreach ($file in $files) { |
|
1 |
$i+=1 |
|
1 |
$line="</span>{0}`n<span style="color: #006080">" -f $file.name |
|
1 |
$richtextbox1.text+=$line |
|
1 |
$form1.Refresh() |
|
1 |
#compress the file if list only NOT checked |
|
1 |
if (! $chkListOnly.Checked) { |
|
1 |
$rc=$file.Compress() |
|
1 |
$result=$rc.returnValue |
|
1 |
} |
|
1 |
else { |
|
1 |
$result="</span>NA<span style="color: #006080">" |
|
1 |
} |
|
1 |
#log the results if checked |
|
1 |
if ($chkLogResults.checked) { |
|
1 |
# "</span>Server,Name,Extension,FileSize,LastModified,CreationDate,Drive,Path,Result<span style="color: #006080">" |
|
1 |
$data="</span>{0},{1},{2},{3},{4},{5},{6},{7},{8}<span style="color: #006080">" -f $file.CSName,$file.Name,$file.Extension,$file.FileSize,$file.ConvertToDateTime($file.LastModified),$file.ConvertToDateTime($file.CreationDate),$file.Drive,$file.path,$result |
|
1 |
$data | out-file $logfile -append |
|
1 |
} |
|
1 |
} #end foreach $file in $files |
|
1 |
|
1 |
} #end if $files |
|
1 |
|
1 |
if ($i -gt 0) { |
|
1 |
$statusbar1.text="</span>Found $i file(s) to compress<span style="color: #006080">" |
|
1 |
} |
|
1 |
else { |
|
1 |
$statusbar1.text="</span>Failed to find any files to compress.<span style="color: #006080">" |
|
1 |
} |
|
1 |
|
1 |
} #end foreach |
|
1 |
$form1.Cursor="</span>Default" |
|
1 |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($blnLog) {notepad $logfile} |
|
1 |
} <span style="color: #008000">#end Compress</span> |
The script has (I think) a nice example of providing popup help. You can download a zip file with the script and PrimalForms 2009 source file from the Mr. Roboto article.
Thanks to Wes Stahler for being such a willing lab rat.
5 Minute PowerShell
My October Mr. Roboto column is now available online. The article contains my suggestions for how someone completely new to PowerShell might spend their first 5 minutes. Perhaps not literally, since I expect most people will want to spend more than 60 seconds on my suggested steps. But overall I thought my proposal was a reasonable approach. I wanted a 5 minute experience that would gently introduce Windows PowerShell. I know the thought of working from a command line or having to learn a new way of managing Windows is off-putting for many administrators. I wanted someone who had never seen PowerShell or a command prompt before to realize this isn’t a scary or necessarily complicated tool and that you can accomplish a great deal with minimal effort. Sure, I want people to buy books, training videos, attend classes and conferences etc., but I also want them to realize how much they can learn on their own. In fact let me add a few more suggested “minutes”.
