Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (11/27/09)


Bookmark and Share
In this post:

 

Please Clarify This Script You Published Previously

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a question about your Hey, Scripting Guy! post

How Do I Back Up Files That Have Been Modified in the Last Day? In the article, you say the following:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Next we use the Get-Date cmdlet to retrieve the current date and time. We store this in a variable named $dte as seen here:

$dte = get-date

I understand that. You are storing the current date in a variable. So far so good. Then, a little bit later, you decide to turn the date into a string. This is what you said:

Now we want to turn the datetime object that was returned by the Get-Date cmdlet into a string.

We do? Why do we want to do that? I don't follow. But leaving that for a minute, you continue:

To do this, we use the tostring() method. When we have a string, we have something that looks like this:

PS C:> (get-date).tostring()
12/12/2008 1:00:59 PM"

Wait a sec...why did you not use $dte.ToString()?

-- MM

 

Hey, Scripting Guy! AnswerHello MM,

Microsoft Scripting Guy Ed Wilson here. In this particular case, it would work. However, from a best practice standpoint, it would require a revision to the code.

The $dte variable is created in the previous function. Because the other function is calling the previous function, it means that variables created within that function will be available to the following function. I do not like to rely on that because it makes the code more difficult to read and to troubleshoot. Therefore, when I plan to use a variable from a calling function I like to make sure that I actually pass the variable as a parameter to the called function—and I like to use the same variable names if possible.

However, you are correct. In this case, the $dte variable was available and could have been used. Why did I convert the date to a string? Because I was using regular expressions to create a particular pattern for the file names. The pattern was a requirement from the originator of the original e-mail question.


 

How Can I Report a Bug I Found in the Windows PowerShell ISE? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I love the Microsoft TechNet Script Center, but there is one bit of information I cannot find. How do I report a bug that I’ve found in the Windows PowerShell ISE?

-- PC

 

Hey, Scripting Guy! AnswerHello PC,

I just checked with one of the PMs, and he says you can use the Microsoft Windows PowerShell connect site to file a bug on Windows Windows PowerShell or on the Windows PowerShell ISE. You can see in the following image that you can browse the bugs that have been submitted, report a bug, or submit a suggestion:

Image of Windows PowerShell Connect site

In most cases, you should browse the list of reported bugs before submitting a new bug because you can see the status of the bug. The bug status page is shown here:

Image of bug status page


The bugs will be listed as “Active” or “Closed.” If the bug is “Closed,” it might be fixed or marked as “By design,” which means that the behavior seen is the way Windows PowerShell or the Windows PowerShell ISE was designed to behave. If your bug is marked as “Closed by Design,” you could submit a feature request to request that the design be changed. If your feature request is accepted, it will be put on the work list for the next version of Windows PowerShell, which is version 3.0. We are currently accepting requests for features for Windows PowerShell 3.0, and now is the time to get those feature requests in.


 

<

p style="MARGIN: 0in 0in 8pt" class="MsoNormal">

How Can I Close an Open Folder on a Network Drive? 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m using scripting to remove and map network drives. However, this does not work if the drive I am trying to map to is currently open. I’m looking for code to close an open folder on drive X: even if it’s opened to a subfolder. Do you have any ideas?

-- DB

 

Hey, Scripting Guy! AnswerHello DB,

Here is the answer from Michael Frommhold, one of our PFEs (thanks, Michael):

Check out the subroutine Sub CheckDrive in the sample below. The Sub CheckDrive uses the WMI Class Win32_Networkconnection to query for existing connections on the same drive letter or for any connection to the same remote path on any drive letter. If the drive letter is already in use, the connection will be terminated. If the remote path is already connected, the connection will also be terminated.

CheckForOpenConnectionToFolderCloseConnectionAndDisconnectDrive.vbs

Dim oWSH
      
Set oWSH = CreateObject("WScript.Network"
)

Const cPath = "\Servershare"

Const cDrive = "Y:"

Dim blDriveConnected
       blDriveConnected =
False

Dim blPathConnected
       blPathConnected =
False


Dim sDoubleDrive
       sDoubleDrive = vbNullString
      
CheckDrive cDrive, cPath, blDriveConnected, blPathConnected, sDoubleDrive

If blDriveConnected Then oWSH.RemoveNetworkDrive cDrive, True, True


If blPathConnected And Not (LCase(cDrive) = LCase(sDoubleDrive)) Then oWSH.RemoveNetworkDrive sDoubleDrive, True, True

oWSH.MapNetworkDrive cDrive, cPath


Sub CheckDrive(ByVal sDrive, _
                          
ByVal
sPath, _
                          
ByRef
blDrive, _
                          
ByRef
blPath, _
                          
ByRef
sPathDrive _
                           )
      
      
Dim
oWMI
      
Dim
colItems
      
Dim
oItem
      
      
Set oWMI = GetObject("winmgmts:\.rootCIMV2"
)
             
Set colItems = oWMI.ExecQuery("SELECT LocalName, RemotePath FROM Win32_NetworkConnection"
)
      
      
For Each oItem In
colItems
            
            
If oItem.LocalName = sDrive Then blDrive = True

            
            
If
(LCase(oItem.RemotePath) = LCase(sPath)) _
            
Or
(InStr(LCase(oItem.RemotePath), LCase(sPath)) > 0) _
            
Or (InStr(LCase(sPath), LCase(oItem.RemotePath)) > 0) Then

                   
                    blPath =
True
                    sPathDrive = oItem.LocalName
                   
            
End If

            
      
Next


End
Sub

 

Can I Automate the Scheduling of Offline File Tasks?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I am wondering if you could help me with a request that I have regarding offline files. I currently need to automate the process of creating the task to schedule offline files settings under Windows 7. The graphical user interface allows the user to configure these settings. I am talking about settings such as sync occurs on logout/unlock/interval. However, I cannot find a documented way of automatically generating these tasks be it via WMI (providers), Windows PowerShell, or an admin tool. Would you be able to help me with this request? I will greatly appreciate your help on this—it will mean glory or bust for me around the office.

-- FK

 

Hey, Scripting Guy! AnswerHello FK,

Have you taken a look at the WMI Offline Files provider? The documentation for the WMI Offline Files provider on MSDN is quite extensive, and there is a decent amount of capability supplied via that provider. For example the WMI Class Win32_OfflineFilesCache has the methods that are listed in Table 1.

Table 1  Methods of the Win32_OfflineFilesCache WMI Class

Comments (1)

  1. Commentator says:

    Regarding the moving of offline files under windows 7 see blogs.technet.com/…/updating-your-offline-files-cache-in-windows-vista-to-point-to-a-new-server.aspx for a full functioning vb script.

Skip to main content