Share this post:

Can I Delete All Items in the Deleted Items Folder?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need a VBScript script that will delete all of the e-mails in the Deleted Items folder. I have tried to do this, but I am not able to get it to delete all of the items in the folder. One time it deletes one or two items, and then on another occasion it appears to not delete any items.

-- AS

Hey, Scripting Guy! Answer

Microsoft Scripting Guy Ed Wilson here. Trying to delete items from the Deleted Items folder in Microsoft Outlook can be a little tricky. In fact, I spent all afternoon writing this script for you. In the end, I had to read the directions (please don't tell the Scripting Wife; she claims I never read the directions, and I don't want to ruin my reputation). The delete method is documented on MSDN, where it states that you must start with the last item in the folder, and work your way backward when you wish to delete all items in the folder. If you do not work backward, the script will only delete a portion of the items in the folder and it might take two or three attempts at deleting the items before it finally clears out all items in the folder. The DeleteAllItemsInDeletedItems.vbs script is seen here.

DeleteAllItemsInDeletedItems.vbs


'==========================================================================
'
'
' NAME: DeleteAllItemsInDeletedItems.vbs
'
' AUTHOR: ed wilson , msft
' DATE  : 3/18/2009
'
' COMMENT:
' according to the delete method docs on msdn:
'
http://msdn.microsoft.com/en-us/library/bb207545.aspx
' you MUST start at the last item, and work your way backwards when
' deleting all items in a folder. Otherwise, you will only delete a
' portion of the items in the folder. It might, for example take three
' or four times running the script to delete everything.
'==========================================================================

olFolder = 3 'olFolderDeletedItems from OlDefaultFolders Enumeration
Set olApplication = CreateObject("outlook.application")
Set olNameSpace = olApplication.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolder)
Set colItems = olFolder.Items
WScript.Echo "There are:" &  colItems.count & " items in the folder"
For i = colItems.count To 1 Step -1
WScript.Echo "Deleting item:" & colItems.item(i).subject
  colItems.item(i).Delete
WScript.Echo "Item " & i & " was deleted"
Next

 

Can I Change the DCOM Permissions on a Remote PC for Making WMI Calls?

Hey, Scripting Guy! Question

 Hey, Scripting Guy! I need a script that changes the DCOM permission on a remote PC so that a non-administrator user can make remote WMI calls into the remote PC. I need this script so that I can carry out the task for 8000+ PCs.

-- VN

Hey, Scripting Guy! Answer

Hello VN,

There is a good article on MSDN that talks about the remote WMI connection. Beginning with Windows Vista, you can use methods from the Win32_DcomApplicationSetting WMI class to make DCOM changes. Prior to Windows Vista, you are pretty much limited to hacking the registry to attempt to make DCOM security changes.  You may want to ping the Official Scripting Guys Forum to see if anyone has already written any scripts to do this. You may also be interested in the scripts found on the TechNet Script Center Gallery.

 

How Can I Ensure Code Integrity?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have found a lot of good advice and examples on your Web site. Thank you very much. I was looking to see if you might have advice and be willing to publish an article regarding something many scripters have difficulty with: code integrity. I am a systems engineer at a large enterprise and have been using VBScript-driven HTAs to provide GUI-driven automation for a number of tasks. The GUI has made using the scripts much easier for my co-workers. Check boxes and radio buttons make it easier to specify options than command-line switches for a number of them (please don't ask the obvious questions here), and operating an interface with buttons instead of a command prompt makes it easier for the operators to perform delegated tasks.

The problem is that once the code is released, some of my less technically enabled comrades have taken the "Script Kiddie" approach to solving their own problems by using my code with less than well thought out modifications. So far nothing really disastrous has come of this, but I would like to avoid that possibility as much as possible. I have some fairly complex code that verifies security settings on servers, installs and uninstalls software, audits and makes changes to DS objects, remotely launches code on servers, and other things that could be troublesome if not used in a judicious and disciplined manner.

So the question\request is: What options do HTA developers have to ensure their code integrity? An article was previously published about using the "Windows Script Encoder." But two minutes of work on a search engine yields a tool called the "Windows Script Decoder," which completely undoes the encoding. I have seen references to compiling an HTA into a .dll using the Resource Compiler (rc.exe), but personally have been unable to get that option to work. I have also seen references to using iexpress.exe to hide the mechanism for launching the script so all the user sees is an executable file. There are also a number of third-party tools out there, but most of them just embed the script in an executable which is easily recovered from a temporary folder while the HTA is running.

While nothing is foolproof, I was hoping to for a "pros and cons" discussion on the various methods by which HTA code integrity may be maintained, as well as a step-by-step guide for making the various options work. Professional opinions such as yours are usually better thought out and always much better articulated than general Web discussion consensus.

-- AB

Hey, Scripting Guy! Answer

Hello AB,

The Script Encoder was never intended for code integrity. It was basically used for network administrators to hide the content of logon scripts. It is a base 64 encode, and as you point out it can easily be unencoded. When we talk about integrity, we are basically talking about script signing. I am not sure if an HTA could be digitally signed or not, but you may wish to investigate that.

Here are a few links that talk about code signing with VBScript. Note that to enforce the checking of a VBScript signature, you must make a change to the registry. This can easily be done using Group Policy. The key is documented below.

·         Scripting and Security Hub on the TechNet Script Center.

·         Programmatically signing a script in the Scripting Guide.

·         Digitally signing a script in the Scripting Guide.

There also are some scripts in the TechNet Script Center Gallery that you might find useful.


 

Can I Query for the Size of a Page File?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How would you modify the SetPageFileSize.vbs script to query for the size of the page file instead of changing the size of the page file?

SetPageFileSize.vbs

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPageFiles = objWMIService.ExecQuery _
    ("Select * from Win32_PageFileSetting")

For Each objPageFile in colPageFiles
    objPageFile.InitialSize = 300
    objPageFile.MaximumSize = 600
    objPageFile.Put_
Next

-- BC

Hey, Scripting Guy! Answer

Hello BC,

You will want to use Wscript.Echo to display the value of the initialSize and the MaximumSize properties. The two lines of code that need to be changed are seen here.  

    Wscript.echo objPageFile.InitialSize
    Wscript.echo objPageFile.MaximumSize

The complete DisplayPageFileSize.vbs script is seen here.

DisplayPageFileSize.vbs

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colPageFiles = objWMIService.ExecQuery _
    ("Select * from Win32_PageFileSetting")

For Each objPageFile in colPageFiles
    Wscript.echo objPageFile.InitialSize
    Wscript.echo objPageFile.MaximumSize
Next

BC, keep in mind that the Win32_PageFileSetting WMI class will not report anything if the page file is not manually configured. You may be interested in the Script Center learning hub, or the Microsoft VBScript Step by Step book. I (Ed Wilson) wrote that book, and it includes lots of step-by-step exercises and hundreds of sample scripts.

 

 

Does the Script Center Have an RSS or Atom Feed?


Hey, Scripting Guy! Question

Hey, Scripting Guy!  This is a great site. I only wish there was a way to subscribe to new content. RSS feed, Atom feed, e-mail list, something like that. It seems you guys are always creating great content, but I feel like I am missing out.

-- NH

Hey, Scripting Guy! Answer

Hello NH,

We now have an RSS feed for the Hey Scripting Guy! Blog and for the Script Center Code Gallery. On the right side of the Hey, Scripting Guy! Blog, there is a Syndication box. You can choose RSS 2.0 or Atom 1.0:

Image of Syndication box on Hey, Scripting Guy! Blog


 

How Can I Convert a Value to Hexadecimal?

Hey, Scripting Guy! QuestionI have a script to show the disk signature, but the script does not show the value in hexadecimal form like the dumpcfg.exe does. I would like to be able to convert the value to hexadecimal.

-- JL

Hey, Scripting Guy! Answer

Hello JL,

The Scripting Guide talks about converting decimal to hexadecimal in the section on handling input and output. 


 

Can I Iterate Through Two Arrays at the Same Time?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have two arrays, one stored in the variable $a and the other one stored in the variable $b. I want to loop through these two arrays at the same time do something for $a[0], $b[0], and then for $a[1], $b[1]. The basic idea is seen here:

foreach ($i in $a) and foreach ($j in $b)
{
   do something on $i and $j
}

The problem is that I can't work out the way the foreach statement needs to be constructed to loop through two arrays at the same time and to give me the ability to pick up one item from each array.

Would you be able to help me with the statement that can iterate through two arrays at the same time?

-- HW

Hey, Scripting Guy! Answer

Hello HW,

The Windows PowerShell code seen here will work its way through both arrays:

$a = 1,2,3,4
$b = "a","b","c","d"

Foreach($i in $a)
{
  Foreach ($j in $b)
  {
    $i ; $j
   }
}

When the code is run, the following results are displayed to the Windows PowerShell console:

1
a
1
b
1
c
1
d
2
a
2
b
2
c
2
d
3
a
3
b
3
c
3
d
4
a
4
b
4
c
4
D

By modifying the code to use a for statement instead of a foreach statement, you now have a script that looks like the one seen here:

$a = 1,2,3,4
$b = "a","b","c","d"

for($i = 0 ; $i -le $a.length-1 ; $i++)
{
  $a[$i]
  $b[$i]
}

When you run the script this time, you receive the results shown here:

1
a
2
b
3
c
4
d

 

This brings us to the conclusion of another week on the TechNet Script Center. Thank you for all of the questions sent to scripter@microsoft.com. Join us next week as we answer additional scripting questions.

If you want to know exactly what we will be looking at next week, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. Have a great weekend.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys