Minggu, 13 Februari 2011

Powershell - Delete Temporary Internet Files

I can't recall how I got interestd in trying to delete temporary internet files with Powershell, but, after much digging I came across this link:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/1511ac62-0d48-4c8e-837e-11940a5b7b94
After checking out the link, Richard's blog:
http://msmvps.com/blogs/richardsiddaway/archive/2010/07/12/cookie-time.aspx
had a great entry. I was originally looking to see if the inetcpl.cpl (http://blogs.techrepublic.com.com/window-on-windows/?p=574) some sort of COM interface, but, none ever surfaced. As I dug around the COM object I went through each level to get a feel for what sort of aspects of the object were exposed via the COM interface.

Here are my walkdown steps:

Create the reference:
$app = New-Object -ComObject Shell.Application

Get members of app to see what objects exist at that level
$app | gm


TypeName: System.__ComObject#{866738b9-6cf2-4de8-8767-f794ebe74f4e}

Name MemberType Definition
---- ---------- ----------
AddToRecent Method void AddToRecent (Variant, string)
BrowseForFolder Method Folder BrowseForFolder (int, string, int, Variant)
CanStartStopService Method Variant CanStartStopService (string)
CascadeWindows Method void CascadeWindows ()
ControlPanelItem Method void ControlPanelItem (string)
EjectPC Method void EjectPC ()
Explore Method void Explore (Variant)
ExplorerPolicy Method Variant ExplorerPolicy (string)
FileRun Method void FileRun ()
FindComputer Method void FindComputer ()
FindFiles Method void FindFiles ()
FindPrinter Method void FindPrinter (string, string, string)
GetSetting Method bool GetSetting (int)
GetSystemInformation Method Variant GetSystemInformation (string)
Help Method void Help ()
IsRestricted Method int IsRestricted (string, string)
IsServiceRunning Method Variant IsServiceRunning (string)
MinimizeAll Method void MinimizeAll ()
NameSpace Method Folder NameSpace (Variant)
Open Method void Open (Variant)
RefreshMenu Method void RefreshMenu ()
ServiceStart Method Variant ServiceStart (string, Variant)
ServiceStop Method Variant ServiceStop (string, Variant)
SetTime Method void SetTime ()
ShellExecute Method void ShellExecute (string, Variant, Variant, Variant, Variant)
ShowBrowserBar Method Variant ShowBrowserBar (string, Variant)
ShutdownWindows Method void ShutdownWindows ()
Suspend Method void Suspend ()
TileHorizontally Method void TileHorizontally ()
TileVertically Method void TileVertically ()
ToggleDesktop Method void ToggleDesktop ()
TrayProperties Method void TrayProperties ()
UndoMinimizeALL Method void UndoMinimizeALL ()
Windows Method IDispatch Windows ()
WindowsSecurity Method void WindowsSecurity ()
WindowSwitcher Method void WindowSwitcher ()
Application Property IDispatch Application () {get}
Parent Property IDispatch Parent () {get}

Look at the $app.Namespace object (Shell.Application.Namespace):
$app.Namespace


MemberType : Method
OverloadDefinitions : {Folder NameSpace (Variant)}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : Folder NameSpace (Variant)
Name : NameSpace
IsInstance : True

Examine the members:
$app.Namespace | gm


TypeName: System.Management.Automation.PSMethod

Name MemberType Definition
---- ---------- ----------
Copy Method System.Management.Automation.PSMemberInfo Copy()
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
Invoke Method System.Object Invoke(Params System.Object[] arguments)
ToString Method string ToString()
IsInstance Property System.Boolean IsInstance {get;}
MemberType Property System.Management.Automation.PSMemberTypes MemberType {get;}
Name Property System.String Name {get;}
OverloadDefinitions Property System.Collections.ObjectModel.Collection`1[[System.String, mscorlib, Version=2.0.0.0...
TypeNameOfValue Property System.String TypeNameOfValue {get;}
Value Property System.Object Value {get;set;}

Look specifically at the entries as noted in the blog:
$app.namespace(0x21)

Note: The output of decimal (21) and hex (0x21) references will yield different results:
$app.NameSpace(0x21)


Title : Cookies
Application : System.__ComObject
Parent :
ParentFolder : System.__ComObject
Self : System.__ComObject
OfflineStatus :
HaveToShowWebViewBarricade : False
ShowWebViewBarricade : False

versus this:
$app.NameSpace(21)

Title : Templates
Application : System.__ComObject
Parent :
ParentFolder : System.__ComObject
Self : System.__ComObject
OfflineStatus :
HaveToShowWebViewBarricade : False
ShowWebViewBarricade : False

Now, what's interesting is that, without the reference (shown above) I get a pretty bland member set. However, when I add the (0x21) reference and run Get-Member cmdlet, I see different objects:
$app.Namespace(0x21) | gm


TypeName: System.__ComObject#{a7ae5f64-c4d7-4d7f-9307-4d24ee54b841}

Name MemberType Definition
---- ---------- ----------
CopyHere Method void CopyHere (Variant, Variant)
DismissedWebViewBarricade Method void DismissedWebViewBarricade ()
GetDetailsOf Method string GetDetailsOf (Variant, int)
Items Method FolderItems Items ()
MoveHere Method void MoveHere (Variant, Variant)
NewFolder Method void NewFolder (string, Variant)
ParseName Method FolderItem ParseName (string)
Synchronize Method void Synchronize ()
Application Property IDispatch Application () {get}
HaveToShowWebViewBarricade Property bool HaveToShowWebViewBarricade () {get}
OfflineStatus Property int OfflineStatus () {get}
Parent Property IDispatch Parent () {get}
ParentFolder Property Folder ParentFolder () {get}
Self Property FolderItem Self () {get}
ShowWebViewBarricade Property bool ShowWebViewBarricade () {get} {set}
Title Property string Title () {get}

Taking this a level down I work with the Self property:
$app.Namespace(0x21).Self


Application : System.__ComObject
Parent : System.__ComObject
Name : Cookies
Path : C:\Users\Will\AppData\Roaming\Microsoft\Windows\Cookies
GetLink :
GetFolder : System.__ComObject
IsLink : False
IsFolder : True
IsFileSystem : True
IsBrowsable : False
ModifyDate : 1/18/2011 7:16:28 PM
Size : 0
Type : File folder

This actually gave me a good selection of objects when I ran the Get-Member cmdlet.
$app.Namespace(0x21).Self | gm


TypeName: System.__ComObject#{edc817aa-92b8-11d1-b075-00c04fc33aa5}

Name MemberType Definition
---- ---------- ----------
ExtendedProperty Method Variant ExtendedProperty (string)
InvokeVerb Method void InvokeVerb (Variant)
InvokeVerbEx Method void InvokeVerbEx (Variant, Variant)
Verbs Method FolderItemVerbs Verbs ()
Application Property IDispatch Application () {get}
GetFolder Property IDispatch GetFolder () {get}
GetLink Property IDispatch GetLink () {get}
IsBrowsable Property bool IsBrowsable () {get}
IsFileSystem Property bool IsFileSystem () {get}
IsFolder Property bool IsFolder () {get}
IsLink Property bool IsLink () {get}
ModifyDate Property Date ModifyDate () {get} {set}
Name Property string Name () {get} {set}
Parent Property IDispatch Parent () {get}
Path Property string Path () {get}
Size Property int Size () {get}
Type Property string Type () {get}

Taking this to the last noted level, Path, I was able to get very specific:
$app.Namespace(0x21).Self.Path
C:\Users\Will\AppData\Roaming\Microsoft\Windows\Cookies

The majority of the members at this level were really members of the File object:
$app.Namespace(0x21).Self.Path | gm

TypeName: System.String

Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int CompareTo(string strB)
Contains Method bool Contains(string value)
CopyTo Method System.Void CopyTo(int sourceIndex, char[] destination, int destinationIndex,...
EndsWith Method bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...
Equals Method bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf...
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), i...
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normaliz...
LastIndexOf Method int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int...
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startI...
Normalize Method string Normalize(), string Normalize(System.Text.NormalizationForm normalizat...
PadLeft Method string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)
PadRight Method string PadRight(int totalWidth), string PadRight(int totalWidth, char padding...
Remove Method string Remove(int startIndex, int count), string Remove(int startIndex)
Replace Method string Replace(char oldChar, char newChar), string Replace(string oldValue, s...
Split Method string[] Split(Params char[] separator), string[] Split(char[] separator, int...
StartsWith Method bool StartsWith(string value), bool StartsWith(string value, System.StringCom...
Substring Method string Substring(int startIndex), string Substring(int startIndex, int length)
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToLower Method string ToLower(), string ToLower(System.Globalization.CultureInfo culture)
ToLowerInvariant Method string ToLowerInvariant()
ToString Method string ToString(), string ToString(System.IFormatProvider provider)
ToUpper Method string ToUpper(), string ToUpper(System.Globalization.CultureInfo culture)
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), string Trim()
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property System.Int32 Length {get;}

Out of curiousity, I wanted to see how many different folders paths I could get, so, I set up this loop:
$collection = @()
for($i = 1; $i -lt 1000; $i++)
{
$collection += $app.Namespace($i).Self.Path
}

foreach($i in $collection)
{
Write-Host $i
}

What turned out to be interesting that the output showed a pattern where, after every 256 lines (the same size as a uint) it would start repeating. Below are the entries I found with the corresponding numbers in front:
1 ::{871C5380-42A0-1069-A2EA-08002B30309D}
2 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
3 ::{26EE0668-A00A-44D7-9371-BEB064C98683}\0
4 ::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{2227A280-3AEA-1069-A2DE-08002B30309D}
5 C:\Users\Will\Documents
6 C:\Users\Will\Favorites
7 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
8 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Recent
9 C:\Users\Will\AppData\Roaming\Microsoft\Windows\SendTo
10 ::{645FF040-5081-101B-9F08-00AA002F954E}
11 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Start Menu
13 C:\Users\Will\Music
14 C:\Users\Will\Videos
16 C:\Users\Will\Desktop
17 ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
18 ::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}
19 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Network Shortcuts
20 C:\Windows\Fonts
21 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Templates
22 C:\ProgramData\Microsoft\Windows\Start Menu
23 C:\ProgramData\Microsoft\Windows\Start Menu\Programs
24 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
25 C:\Users\Public\Desktop
26 C:\Users\Will\AppData\Roaming
27 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Printer Shortcuts
28 C:\Users\Will\AppData\Local
29 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
30 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
31 C:\Users\Will\Favorites
32 C:\Users\Will\AppData\Local\Microsoft\Windows\Temporary Internet Files
33 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Cookies
34 C:\Users\Will\AppData\Local\Microsoft\Windows\History
35 C:\ProgramData
36 C:\Windows
37 C:\Windows\System32
38 C:\Program Files
39 C:\Users\Will\Pictures
40 C:\Users\Will
41 C:\Windows\System32
42 C:\Program Files
43 C:\Program Files\Common Files
44 C:\Program Files\Common Files
45 C:\ProgramData\Microsoft\Windows\Templates
46 C:\Users\Public\Documents
47 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools
48 C:\Users\Will\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools
49 ::{21EC2020-3AEA-1069-A2DD-08002B30309D}\::{7007ACC7-3202-11D1-AAD2-00805FC1270E}
53 C:\Users\Public\Music
54 C:\Users\Public\Pictures
55 C:\Users\Public\Videos
56 C:\Windows\Resources
57 C:\ProgramData\OEM Links
58 C:\Users\Will\AppData\Local\Microsoft\Windows\Burn\Burn
61 ::{F02C1A0D-BE21-4350-88B0-7367FC96EF3C}

0 komentar:

Posting Komentar

 
Powered by Blogger