Friday, October 19, 2007

Stop or Disable Terminal Server Logons (Change Logon)

Here is a simple command that I had a hard time finding.

Change Logon /Disable
Change Logon /Enable

It prevents users from logging on to the server that the command was ran on. All new connections will be disabled. Existing connections will not be disconnected. So you can run this with remote users logged in to stop new connections while you wait for people to exit.

You can run that from a terminal session. If you get disconnected, you will have to get physical access to the machine to activate it.

From the website: Enables or disables logons from client sessions, or displays current logon status. This utility is useful for system maintenance.

Running VB.NET code in VBScript or other apps

Here is how you can write a .NET component and use it in a script. First, create a library that contains the code you want to run.

Public Class FileWriter
Public Sub Save(ByVal PathAndFileName As String)
Dim FileStream As New System.IO.StreamWriter(PathAndFileName, False)
FileStream.WriteLine("Hello World")

Catch e As Exception
Dim fs As New System.IO.StreamWriter("C:\error.txt", True)
fs.WriteLine("Exception: {1}", 1, e.Message)
Throw e
End Try

End Sub
End Class

Then compile the library and run regasm c:\project\bin\NameOfDLL.dll /CODEBASE to register it. Regasm is in your .net folder. That will register the dll so you can use it later. Regsvr32 is for non .NET dlls. Your script is next. Save it as a .vbs file.

dim fs
set fs = CreateObject("Project1.FileWriter") "c:\file.txt"
set fs = nothing

Anyplace you can call CreateObject, you should be able to use your class. VBA in office is one such place. And I did not test this code, I just gutted another project to have some examples.

Related: Running vbs from .net code

mscorlib: Request for the permission of type Version Culture PublicKeyToken failed.

I wrote a little database access code in that I wanted to use in another script. I was getting this error though. C:\Test.vbs(6, 1) mscorlib: Request for the permission of type 'System.Data.OleDb.OleDbPermission, System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

My dev machine worked fine. And the other box looked like it had the data access and .NET components I needed. Looking closer, it was talking about permissions. The connection string was the same and it contained the password. I decided it was a security zone messing me up.

When I ran regasm, the dll was located on a network share. That was about the only difference. So I unregistered it, moved it locally, and ran regasm again. The problem went away.

mscorlib: Request for the permission of type Version Culture PublicKeyToken failed.

Thursday, October 18, 2007

The system cannot find the file specified. CreateObject

I was attempting to use some code in a vbs file and was getting "The system cannot find the file specified." on the CreateObject line. I tracked it down to the way I registered the DLL.

One sugestion told me to unregister and re register it with regsvr32. Because I was using .NET, I knew that was not it. It turned out to be my use of regasm to register my DLL. I left out the /CODEBASE option.

Once I registered it with that option, the error went away.