Enumerating the If... Then: Scripts

Friday, March 20, 2009

C#/.NET: Seriously Refresh Desktop Icons / Clear Desktop Icon Cache

Tested.
In use at work.


using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace ConsoleApplication1
{
class Program
{

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern long SendMessageTimeout(
int hWnd,
int Msg,
int wParam,
string lParam,
int fuFlags,
int uTimeout,
out int lpdwResult
);


// http://www.programmingforums.org/post87847-9.html

static void Main(string[] args)
{

// get the the original Shell Icon Size registry string value to 4
RegistryKey k = Registry.CurrentUser.OpenSubKey("Control Panel").OpenSubKey("Desktop").OpenSubKey("WindowMetrics", true);
Object OriginalIconSize = k.GetValue("Shell Icon Size");

// set the Shell Icon Size registry string value to 4
k.SetValue("Shell Icon Size", "33");
k.Flush(); k.Close();

// broadcast WM_SETTINGCHANGE to all window handles
int res = 0;
SendMessageTimeout(0xffff, 0x001A, 0, "", 0x0002, 5000, out res);
//SendMessageTimeout(HWD_BROADCAST,WM_SETTINGCHANGE,0,"",SMTO_ABORTIFHUNG,5 seconds, return result to res)

// set the Shell Icon Size registry string value to original value
k = Registry.CurrentUser.OpenSubKey("Control Panel").OpenSubKey("Desktop").OpenSubKey("WindowMetrics", true);
k.SetValue("Shell Icon Size", OriginalIconSize);
k.Flush(); k.Close();

SendMessageTimeout(0xffff, 0x001A, 0, "", 0x0002, 5000, out res);


}
}
}


Interesting

Wednesday, March 18, 2009

vbs: Query a list of workstations for local administrators

Note that this is for machines that are English language only.

Tested.
In use at work.


Option Explicit

Const LogFile = "LocalAdmins.log"
Const resultFile = "LocalAdministratorsMembership.csv"
Const inputFile = "workstations.txt"


Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim shl
Set shl = WScript.CreateObject("WScript.Shell")

Dim fil
Set fil = fso.OpenTextFile(inputFile)

Dim results
Set results = fso.CreateTextFile(resultFile, True)

WriteToLog "Beginning Pass of " & inputFile & " at " & Now()
'WScript.Echo "Beginning Pass of " & inputFile & " at " & Now()
On Error Resume Next

Dim grp
Dim line
Dim exec
Dim pingResults
Dim member

While Not fil.AtEndOfStream
line = fil.ReadLine

Set exec = shl.Exec("ping -n 2 -w 1000 " & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, "reply from") Then
WriteToLog line & " responded to ping"
'WScript.Echo line & " responded to ping"

'On Error Resume Next

Set grp = GetObject("WinNT://" & line & "/Administrators")

'WScript.Echo line & ", Administrators"
results.WriteLine line & ",Administrators,"

For Each member In grp.Members
'WScript.Echo "Administrators: " & member.Name
WriteToLog line & ": Administrators - " & member.Name
results.WriteLine ",," & member.Name
Next
Else
WriteToLog line & " did not respond to ping"
'WScript.Echo line & " did not respond to ping"
End If
Wend

results.Close

Sub WriteToLog(LogData)
On Error Resume Next

Dim fil
'8 = ForAppending
Set fil = fso.OpenTextFile(LogFile, 8, True)

fil.WriteLine(LogData)

fil.Close
Set fil = Nothing
End Sub

wscript.echo "Stick a fork in me"