Bookmark and Share

In this post:

 

Can I Save Outlook 2007 Attachments to a Folder?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is it possible to run Windows PowerShell scripts as a macro in Outlook 2007? If so, is it possible to create a Windows PowerShell script that will save attachments from the selected e-mail and into a set folder? In other words, I have Outlook 2007 and I receive PDF attachments daily from several senders. I need these autosaved to a folder on the local computer. Is this possible? I have tried it with VBScript, but I could not get it to work. 

-- EM

 

Hey, Scripting Guy! AnswerHello EM,

It might be possible to do so, but I have not tested it. However, you might be able to have your macro launch Windows PowerShell and execute a command. An example of launching Windows PowerShell and having it execute a command is seen here.

powershell -command &{get-process}

The above command starts Windows PowerShell, executes the Get-Process cmdlet, and then exits. I tested it by clicking Start, clicking Run, and then typing the command in the Run dialog box as seen here:

Image of typing the command in the Run box


If you want to leave the Windows PowerShell console window open after running the command, use the -noexit switch before the -command parameter. This is seen here:

powershell -noexit -command &{get-process}

I would therefore expect that you could use an Outlook macro to call either of the above command lines to execute your Windows PowerShell code. Keep in mind that I myself have never written an Outlook Macro, and therefore have no way of testing this. If it works, I would love to hear back from you!


 

Troubleshooting a Script That Uses the Win32_OperatingSystem Class 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Greetings. One of our customer’s application runs into an issue on Windows Server 2008. For enumerating the Win32_OperatingSystem class, they get the IenumWbemClassObject. After they have that object, they get the Win32_OperatingSystem instance (IwbemClassObject). When they have the Win32_OperatingSystem instance, they then use GetNames to return the name of the operating system. When they do SafeArrayGetUBound, they are getting lUBound as -1. This is happening only for Windows Server 2008 and the Windows Vista. On Windows 2003 and Windows XP they are not observing this behavior.

I have attached two pictures taken from WbemTest that illustrate what I think is going on. In Windows Server 2008, seen in the first image, the Name property is not defined as a key:

Image of Name property not defined as a key

In Windows Server 2003, seen in the second image, the Name property is defined as KEY:

Image of Name property defined as KEY


Is this a code defect or by design behavior on Windows Server 2008? If it is a by design change, why was this done? Thanks a lot.

-- KZ

 

Hey, Scripting Guy! AnswerHello KZ,

This is because the class was converted to a singleton, which means there is only one instance of the WMI class. This allows you to work directly with the members and to not worry about dealing with a collection or trying to determine which instance to retrieve. This change first came about in Windows Vista. In your second image, in the qualifiers section you can see the singleton qualifier and note that it is set to true. A singleton class does not have a key property.


 

Where Can I Find a Complete List of Singleton Classes in Windows Server 2008? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Me, again. I passed along your answer to the question about the missing key property in the Win32_OperatingSystem WMI class, and now my customer raised another query in this regard. Is there a complete list of singleton classes in Windows Server 2008 and later operating systems? In addition, how can they be advised about the changes if any class will be converted to singleton class? For example, can they subscribe to a newsletter? Thanks! 

-- KZ

 

Hey, Scripting Guy! AnswerHello KZ,

I do not know of a complete list of singletons. One could write a script easily enough to look for the presence of the singleton qualifier on all the WMI classes in the namespace. Because many applications create their own WMI classes, no single list issued by Microsoft could be comprehensive because it depends on which software is installed on the computer, which roles are activated on the server, and which management packs and other utilities have been added to the base operating system installation.

There are no RSS feeds or newsletters that I know of that are specific to your query … however, three resources come to mind.

1.     The WMI Team Blog

2.     The Scripting Guys Web site

3.     On MSDN you will find a WMI-related article called, What’s New?. Changes, additions, and so on are always highlighted.

 

The WMI Team Blog has great information about WMI. They always highlight new release information. It is a fun read, and you will find great information. They also have an RSS feed.

The Microsoft Scripting Guys Web site on TechNet (also known as the Script Center) focuses solely on Microsoft scripting technologies (VBScript and Windows PowerShell). We write a daily Hey, Scripting Guy! post, and often cover WMI topics because of its popularity as a management tool. The Scripting Guys Blog and the new TechNet Script Center Gallery both support RSS feeds.

The What’s New WMI page on MSDN does not support RSS feeds. 

 

How Can I Remotely Execute a Script in Windows PowerShell 2.0? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! In reading your Windows PowerShell article about Windows Update. I think the script is great. It would be better if I had Windows PowerShell installed on all of my servers, but I do not. With VBScript, I can specify a remote server in the ActiveX CreateObject, but I cannot figure out how to do that in Windows Powershell with the New-Object cmdlet.

-- JK

 

Hey, Scripting Guy! AnswerHello JK,

You will need Windows PowerShell to be installed on all your servers. With Windows PowerShell 2.0, you can remotely execute the script on a remote server, but it still requires Windows PowerShell to be installed.  

 

Can These Two Things Be Done? 

Hey, Scripting Guy! Question

I have two quick questions. Using one VBScript, how can I mimic the mouse action (double-click + select)? Also, can you provide an example where one could use Windows PowerShell within an HTML page? In other words, I want to get some WMI data and then display it in HTML.

-- HG

 

Hey, Scripting Guy! AnswerHello HG,

Unfortunately, neither can be done. I can use Windows PowerShell and have it output HTML code. Then I can use that code as a Web page, but I cannot use Windows PowerShell within a Web page directly. See the Help topic for the ConvertTo-Html cmdlet for examples of creating a Web page from within Windows PowerShell. 

 

Why Won't This VBScript Script Work in Windows PowerShell? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! There is a script on your page which is perfect for a file operation on one of my own servers. It’s the “Delete File older than X days“ script. When I copy the script into the Windows PowerShell ISE and try to run it, I get this error message:

 

PS O:\> . c:\scripts\dfot7.ps1

Missing '(' after 'if' in if statement.

At C:\scripts\dfot7.ps1:4 char:4

+ If  <<<< Len(strDay) < 2 Then

    + CategoryInfo          : ParserError: (OpenParenToken:TokenId) [], ParseException

    + FullyQualifiedErrorId : MissingEndParenthesisInIfStatement

 

And here is the script itself:

dtmDate = Date - 7

strDay = Day(dtmDate)

 

If Len(strDay) < 2 Then

    strDay = "0" & strDay

End If

 

strMonth = Month(dtmDate)

If Len(strMonth) < 2 Then

    strMonth = "0" & strMonth

End If

 

strYear = Year(dtmDate)

 

strTargetDate > strYear & strMonth & strDay

 

strComputer = "."

 

Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\\netapp1\transfer")

 

Set FileList = objWMIService.ExecQuery

        ("ASSOCIATORS OF {Win32_Directory.Name='C:\Scripts'} where

        "& ResultClass = CIM_DataFile")

       

For Each objFile in FileList

    strDate = Left (objFile.CreationDate, 8)

    If strDate < strTargetDate Then

        If objFile.Extension = "*.*" Then

            objFile.Delete

        End If

    End If

Next

 

What have I done wrong?

-- TB

 

Hey, Scripting Guy! AnswerHello TB,

You are trying to edit VBScript code by using the Windows PowerShell ISE. That will never work.

If you were running this script in VBScript, your script still would not work because you do not create the strTargetDate variable. You will probably need to use the CDATE or the DateSerial function to create a date for evaluation purposes.

In addition, I do not understand what you are trying to do with your WMI moniker. It looks as if you are trying to connect to a share on some remote computer. This is the line of code I am talking about:

Set objWMIService = GetObject ("winmgmts:\\" & strComputer & "\\netapp1\transfer")

 

You cannot connect to WMI in this manner. I have revised the moniker so that it will work:

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

 

Well, this concludes another edition of Quick-Hits Friday. It also concludes another exciting week on the Script Center. Join us next week as we delve into the mysteries of…let’s keep that a mystery for now.

If you want to know exactly what we will be looking at on Monday, 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. See you on Monday. Until then, have an awesome weekend.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys