VBS Scripting to move computers from one OU to the next - vbscript

I am working on packaging a script for the company that I work for that will allow field service techs to convert computers in a private workstation OU to a team workstation OU and vice versa.
That is a small part of this script for now and one that has had me puzzled for most of the day. I've tried different variations of this script and landed on one that I believe will get me on the right track.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select Name from Win32_ComputerSystem",,48)
For Each objItem in colItems
strPCName = objItem.Name
Next
Set objNewOU = GetObject("LDAP://OU=Computers,OU=Corporate,DC=xxxxx,DC=net")
Set objMoveComputer = objNewOU.MoveHere("LDAP://CN=" & strPCName & ",OU=Computers,OU=Corporate,DC=xxxxx,DC=net",vbnullstring)
I get an error, There is no such object on the server. When I put the computer manually in the OU in question, I don't get that error message. This is where I'm stuck at the moment.
The scripting is in my personal lab at the moment.

I was able to get a solution that did work, by using the following script.
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerDN = objSysInfo.ComputerName
Set objNewOU = GetObject("LDAP://OU=Private Workstations,OU=xxxx,OU=xxxx,DC=xxxx,DC=xxxx")
Set objMoveComputer = objNewOU.MoveHere _ ("LDAP://" & strComputerDN, vbNullString)
Judging by the script's behavior, it uses strComputerDN to determine where the computer is, and the objNewOU determines where the computer is going. The objmoveComputer consolidates this information as best as I can determine to move the computer to it's OU.

Related

Getting actual powershell return value in vb6?

Alright, so I have been trying to do this for a while, and I have come to the realization that this isn't really something that is often asked, and with vb6 getting phased out more and more, there seems to be less help than I would like regarding the language.
The title doesn't say it all actually, as I am looking to do something very specific. I need to execute a shell command (that I know how to do), however, after I execute it, I want to be able to save the return value of that command as a string. For example, if the command is ipconfig, I want the entire return value of that, all the text I would see in powershell after executing that command, saved to a string in my program.
As far as I know, I need to "import" a few things, because I have to use WshShell, which I don't know where to get. So that's part of the question, what classes do I have to add and how, or if there is a way to do it without adding classes then even better. In addition, I have heard a lot about the use of CreatePipe and such regarding similar problems, but I don't know how to use it.
Basically, what I'm saying is that I am quite uneducated regarding the subject, and any insight would be much appreciated, and thanks to all who reply.
There are many ways. Using VBScript's WSHShell.Exec is the easiest.
This is VBScript but VBScript can be pasted into VB (not vice versa though).
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("ipconfig")
Do While oExec.Status = 0
WScript.Sleep 100
Loop
MsgBox oExec.StdOut.ReadAll
Slightly modified from help.
This is how to ping from VBS/VB
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From win32_PingStatus where address='104.43.195.251'")
'msgbox colItems
For Each objItem in colItems
msgbox "Status" & objItem.statuscode & " Time " & objItem.ResponseTime
Next

can you get the pc user name in vbs?

Is there way to find the PC user in visual basic (C:\User\"here").
After we get it, just save it as a string.
I know the answer might be a bit obvious, but I cannot find out how to do this
Fairly simple, from here ( http://blogs.msdn.com/b/alejacma/archive/2008/03/11/how-to-get-the-user-running-a-vbscript.aspx )
Dim networkInfo
Set networkInfo = CreateObject("WScript.NetWork")
Dim infoStr
infoStr = "User name is " & networkInfo.UserName & vbCRLF & _
"Computer name is " & networkInfo.ComputerName & vbCRLF & _
"Domain Name is " & networkInfo.UserDomain
MsgBox infoStr
The simplest way might be to query the environment.
There are USERDOMAIN, USERNAME, USERPROFILE and COMPUTERNAME environment variables containing the obvious values.
Querying those would depend solely on WScript.Shell instead of on WScript.Network as in the accepted (and correct) answer. If you already have a reference to the shell, this might be a slightly more comfortable way.

Get sizes of registry hives using WMI

I want to find out the file sizes of the hives in the registry using WMI and VBScript. This is what I have so far:
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
strComputer & "\root\cimv2:StdRegProv")
strKeyPath = "System\CurrentControlSet\Control\Hivelist"
objReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath, arrVals, arrTypes
WScript.Echo "Values under System\CurrentControlSet\Control\Hivelist"
For Each val In arrVals
objReg.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, val, dwValue
WScript.Echo dwValue
Next
This gives me the correct list, but then I need to get the file sizes. What is the best way to accomplish this?
To avoid type prefix fraud and meaningless variable names, use strRFSpec instead of dwValue
Convert the registry file spec (e.g. "\Device\HarddiskVolume1\Documents and Settings\NetworkService\NTUSER.DAT") into a strFSpec understandable to the FileSystemObject (e.g. "C:\Documents and Settings\NetworkService\NTUSER.DAT")
Check existence and accessability of strFSpec
WScript.Echo goFS.GetFile(strFSpec).Size
(tested under Win XP)
ADDED (wrt comment):
The conversion from strRFSpec to strFSpec may need more effort than a simple Replace() using hardcoded strings. Your Documents and Settings or your WINDOWS could live on F:\. So maybe you'll have to look for a WMI class that maps "\Device\HarddiskVolume... to a drive letter, to use %windir% on strFSpecs containing \system\, or ask WshShell.SpecialFolders("MyDocuments") for a drive letter. As my setup is simple, I can't give further - tested - advice.

Internet explorer grab internal ip address

I'm looking for a solution to grab people's internal ip addresses in IE (not using java or java applets). The equivalent in Java looks like that:
this.sock.bind(new java.net.InetSocketAddress('0.0.0.0', 0));
this.sock.connect(new java.net.InetSocketAddress(document.domain, (!document.location.port)?80:document.location.port));
return this.sock.getLocalAddress().getHostAddress();
Is that something possible in vbscript or jscript? Could you provide me with an example?
Thanks for your time.
You can not get internal IP with JavaScript.
This looks like something you'll need an ActiveX control for, if it is possible.
I think that depending on the security settings in IE you might be able to use WMI. If so you could just use the Win32_NetworkAdapterConfiguration and it's IPAddress property.
The following sample in vbscript:
strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery _
("Select IPAddress from Win32_NetworkAdapterConfiguration ")
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) _
to UBound(IPConfig.IPAddress)
WScript.Echo IPConfig.IPAddress(i)
Next
End If
Next
Is taken from this MSDN page.

Check for daylight saving time with WMI on Vista/Win7

How do I find out if the computer I'm on has daylight saving time in effect? (preferably using WMI)
According to this article at TechNet, I could query SELECT DaylightInEffect FROM Win32_ComputerSystem, but the property DaylightInEffect is not supported on Vista or Win7. As my program will run on various systems (XP, Vista, 7), I would appreciate some portable way of finding out.
The documented supported OS list is not accurate, this works fine on Win7 when I try it. I can't think of any reason it wouldn't be supported on any other OS, it is easy to find out with the Win32 API (GetTimeZoneInformation).
You can use WmiCodeCreator for a quick check.
Here is a boolean function that is implemented using the WMI query referenced in the question.
(Related: How Can I Determine My Time Zone Offset Using VBScript?)
Function IsDaylightInEffect()
Const sComputer = "."
Dim oWmiService : Set oWmiService = _
GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cItems = oWmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For Each oItem In cItems
IsDaylightInEffect = oItem.DaylightInEffect
Exit For
Next
End Function

Resources