Quick-Hits Friday: Run VBScript from the PowerShell Console, and Search for Multiple Extensions

Quick-Hits Friday: Run VBScript from the PowerShell Console, and Search for Multiple Extensions

  • Comments 2
  • Likes

Summary: The Microsoft Scripting Guys discuss adding Run As capability to VBScript and using Windows PowerShell to run VBScript. Also discussed: Searching for multiple extensions, and creating a local account and adding it to the local administrators group.

 

In this post:

 

 How Can I Run a VBScript Script from the Windows PowerShell Console?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I was just reviewing How Can I Use the RunAs Command to Run a Script Under Alternate Credentials? and I noticed that it was written in way back in 2006. I am therefore hoping that the ConnectServer method described in the aforementioned article was updated in Windows 7 to resolve the local credential issue. Therefore, I looked up the ConnectServer method in the MSDN documentation. It looks like the article was updated in May of 2010 and states it will work with the local computer. Am I reading this article wrong or is the person who wrote this article blowing smoke?

-- JJ

 

Hey, Scripting Guy! Answer Hello JJ,

I hate to say that anyone is wrong or “blowing smoke”; however, I can tell you the following: WMI has not been modified to permit the use of alternative credentials for a local connection. This is true whether you are using the connectServer method from the COM scripting API, using .NET Framework methods via managed code, or using the newest Microsoft scripting language, Windows PowerShell. It is simply a limitation of the architecture, and I know of no plans to change it.

The way I get around this limitation is to make a registry modification to permit “runas” with a VBS file, or to right-click and click Run As on either the Windows PowerShell console or the Windows PowerShell ISE. You can also use a scheduled task to run the script under alternative credentials. By the way, the article is correct. You can use the connectserver method to make a local connection; however, you cannot specify alternate credentials for a local connection (which to me, negates the value of the connectserver method). This is mentioned in the description of each of the parameters:

strUser [in, optional] “…The strUser parameter should only be used with connections to remote WMI servers. If you attempt to specify strUser for a local WMI connection, the connection attempt fails…”

strPassword [in, optional] “…The strPassword parameter should only be used with connections to remote WMI servers. If you attempt to specify strPassword for a local WMI connection, the connection attempt fails…”

You can, of course, use Run As from the command line, as is indicated in this Hey, Scripting Guy! post. Or you can use the VBScript script just below that I wrote to add Run As support for a VBS file. I wrote this script several years ago. On Windows 7, you will need to run the script with administrative rights. Personally, I love using the Windows PowerShell console to run a VBScript script with administrative rights because I can right click the Windows PowerShell console and click Run as administrator from the Windows PowerShell console icon. When I do this, I use Cscript to run the actual VBScript file. This is shown in the following image.

Image of running a VBS script in Windows PowerShell console

After the AddRunAs.vbs script has run, the RunAS registry keys are added to the HKEY_CLASSES_ROOT\VBSFile\Shell\ registry key, as shown in the following image.

Image of RunAS registry keys having been added

It is now possible to right-click a VBScript script, and then click Run As. This is shown in the following image.

QHF-09-17-10-03

The complete AddRunAs.vbs script is shown here.

'==========================================================================
'
' BLOCKED SCRIPT  AUTHOR: Ed Wilson , MS,  9/19/2006
'
' NAME: AddRunAs.vbs
'
' COMMENT: Key concepts are listed below:
'
1.Adds the run as script to the registry for the .vbs file extension. 
'2.Have to ADD in the RUNAS key FIRST, AND assign the &Run AS ... value.
'
2a. Can not use &RunAS here, as it will TRY to add the runas.exe program...
'3. THEN we add in the Runas.exe program. 
'
4. IF you go into "folder options" for example  choose fileTypes, and select vbs, 
'4a. and choose the advanced button, you will see Actions: Edit, open,print,
'
4b. edit with primal script, open with command prompt, printto  
'4c. To add RUNAS … so I choose NEW, and then ADD runas in the program box… 
'
5. Below is how to do it Programmatically. 
'==========================================================================
Option Explicit
Dim objShell
Dim strReg,strReg1
Dim strValue, strValueDEF
strReg = "HKCR\VBSFile\Shell\RunAS\"
strReg1 = "HKCR\VBSFile\Shell\RunAS\Command\"
strValueDEF = "&Run As ..."
strValue = "runas.exe ""%1"""
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite strReg,strValueDEF,"REG_SZ"
objShell.RegWrite strReg1,strValue,"REG_SZ"
If Err.Number <> 0 Then 
               WScript.Echo Err.Number,Err.Description,Err.Source
Else
               WScript.echo strReg1,strValue,"added"
End if

 

 How Can I Search for Multiple Extensions? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! There was a script posted to the Script Center Script Gallery last year that is great at searching for one extension. However, I was hoping that this script could be modified to search for multiple extensions. Any help is greatly appreciated.

-- BB

 

Hey, Scripting Guy! Answer Hello BB,

You would need to add a loop beginning here:

listFile ="\list" & Extension & ".txt"

Create an array of file extensions, and loop through it. Here is a link for learning VBScript. There is also information there about loops and arrays.

 

How to Create a Local Account and Add It to the Local Administrators Group

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am trying to create a function that creates a local account on a server and then adds that newly created account to the local administrators group. I keep getting the following error:

Exception calling "Create" with "2" argument(s): "An invalid directory pathname was passed

"

At :line:69 char:22

+   $user = $comp.Create <<<< ("User", $accountName)

Function create-account ([string]$accountName = "$localuser1") {
 
$hostname = $strComputer
 
$comp = [adsi] "WinNT://$hostname"
 
$user = $comp.Create("User", $accountName)
 
$user.SetPassword("Pass1")
 
$user.SetInfo()
     
$objUser = [ADSI]("WinNT://$strcomputer/$localuser1")
     
$objGroup = [ADSI]("WinNT://$strcomputer/Administrators")
     
$objGroup.PSBase.Invoke("Add",$objUser.PSBase.Path)

-- PJ

 

Hey, Scripting Guy! Answer Hello PJ,

I would do something like this:

Function create-account 
{
Param(
  
[string]$user,
  
[string]$group = "Administrators",
  
[string]$computer,
  
[string]$password
) #end param
 
$comp = [adsi] "WinNT://$computer"
 
$user = $comp.Create("User", $user)
 
$user.SetPassword($password)
 
$user.SetInfo()
     
$objUser = [ADSI]("WinNT://$computer/$user,user")
     
$objGroup = [ADSI]("WinNT://$computer/Administrators,group")
     
$objGroup.PSBase.Invoke("Add",$User.PSBase.Path)
} #end function create-account

 

This concludes another edition of Quick-Hits Friday. Join us tomorrow for the Weekend Scripter as we delve into the mysteries of having our Windows PowerShell ISE retrieve stock quotes and weather forecasts for us. No, I am not kidding. It is way cool and should save you tons of time.

We would love for you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hey, Scripting guy... no matter how many times I try your examples or ones like it (for creating users), I always get the same error.  It's the same exception calling with 2 arguments problem, and copying and pasting your code verbatim simply never works.  Am I missing some key bit?

    If it matters, I'm on W7.

  • Hm, I figured it out.  You have a bug in your script... I changed it:

    $comp = [adsi] "WinNT://$computer"

    $newuser = $comp.Create("User", $user)

    $newuser.SetPassword($password)

    $newuser.SetInfo()

    You were setting $user = $comp.Create("user",$user).  I changed the target to $newuser.

    The second part of your script I'm still trying to figure out... not sure what it does, exactly, and it is still throwing an error.

    $objUser = [ADSI]("WinNT://$computer/$user,user")

    $objGroup = [ADSI]("WinNT://$computer/Administrators,group")

    $objGroup.PSBase.Invoke("Add",$User.PSBase.Path)