Archive for the ‘Scripts’ Category.

Find en Windows klients Logonserver

I forbindelse med debug af feks. AD Site Boundaries, GPO eller Logonscript ude på en Windows klient, er det rart at kunne se hvilken Domæne Controller klienten er blevet valideret af. Det kan være at klienten ikke bliver valideret af en HQ DC, men af en DC i et Branch Offices kontor som tilgåes via en WAN forbindelse.
Denne DC/Logonserver kan man se på en nem måde ved at at starte command prompt ude på klienten og skrive følgende.
set logonserver – Man vil herefter få vist klientes DC/Logonserver.

Samme information kan man få via PowerShell.
$a = $env:logonserver ; $a

Eksempel på et VBS logon script


Nedenstående script kan benyttes på workstation og i et Teminal/Citrix miljø.
Gruppe funktionen medtager også nested groups.

‘Globale Variabler
Dim objNetwork,strUser ,wShell,wshnetwork

‘Ingen fejlmeddelser
on error resume next

‘***************** Get Username + Nested Groups *********************

Set objNetwork=CreateObject(“Wscript.Network”)
strUser
=objNetwork.UserName

Function IsMember(sGroup)
If IsEmpty(oGroupList) Then
Set
oGroupList = CreateObject(“Scripting.Dictionary”)
Call LoadGroups(oADObject)
End If
IsMember = oGroupList.Exists(sGroup)
End Function

Sub LoadGroups(oADObject)
On Error Resume Next
Dim
colsGroups, oGroup, j
oGroupList.CompareMode
= vbTextCompare
colsGroups
= oADObject.memberOf
If IsEmpty(colsGroups) Then
Exit Sub
End If

If TypeName(colsGroups) = “String” Then
Set
oGroup = GetObject(“LDAP://” & colsGroups)
If Not oGroupList.Exists(oGroup.sAMAccountName) Then
oGroupList(oGroup.sAMAccountName) = True
Call
LoadGroups(oGroup)
End If
Set
oGroup = Nothing
Exit Sub
End If

For j = 0 To UBound(colsGroups)
Set oGroup = GetObject(“LDAP://” & colsGroups(j))
If Not oGroupList.Exists(oGroup.sAMAccountName) Then
oGroupList(oGroup.sAMAccountName) = True
Call
LoadGroups(oGroup)
End If
Next
Set
oGroup = Nothing
End Sub

‘********Tjek om brugeren skal afvikle Login script****************

If IsMember(“CtxAdmin”) Then
Wscript
.Quit
End If

‘*********** Map Network Drives ***********************************

set wshnetwork = wscript.createobject(“wscript.network”)
wshnetwork.mapnetworkdrive
“H:”,“\WIN-FIL-001Home$” & strUser & “”
wshnetwork.mapnetworkdrive “M:”,“\WIN-FIL-001Teknik”
wshnetwork.mapnetworkdrive “N:”,“\WIN-FIL-001IT”
wshnetwork.mapnetworkdrive “O:”,“\WIN-FIL-001Public”

‘*********** Map Clint Printers ***********************************

wshnetwork.addwindowsprinterconnection “\WIN-FIL-001HP4100”
wshnetwork.addwindowsprinterconnection “\WIN-FIL-001HP4700”

‘Sætter default printer
wshnetwork.setdefaultprinter “\WIN-FIL-001HP4100”

‘********************Redirect af diverse foldere***************************

Dim WSHShell
Set WSHShell = Wscript.CreateObject(“WScript.Shell”)

RegRedirect = “HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerUser Shell Folders”
WSHShell.RegWrite RegRedirect & “Personal”, “H:My Documents”,“REG_EXPAND_SZ”
WSHShell.RegWrite RegRedirect & “Desktop”, “H:CitrixDesktop”,“REG_EXPAND_SZ”
WSHShell.RegWrite RegRedirect & “Favorites”, “H:CitrixFavorites”,“REG_EXPAND_SZ”
WSHShell.RegWrite RegRedirect & “Cookies”, “H:CitrixCookies”,“REG_EXPAND_SZ”

‘********************Opretter Genveje på deskop*****************************

Dim oShell
Set oShell = Wscript.CreateObject(“WScript.Shell”)

Const OverwriteExisting = True
Set
objFSO = CreateObject(“Scripting.FileSystemObject”)

If IsMember(“MSvisio”) Then
objFSO.CopyFile “\WIN-FIL-001GenvejMS Visio.lnk” , “H:CitrixDesktop” , OverwriteExisting
End If

If IsMember(“SAP”) Then
objFSO.CopyFile “\WIN-FIL-001GenvejSAPlogon.lnk” , “H:CitrixDesktop” , OverwriteExisting
Set oShell = Wscript.CreateObject(“WScript.Shell”)
CmdResult
= oShell.run (“\WII-FIL-001GenvejSAP.cmd”)
CmdResult
= oShell.run (“regedit /S \WIN-FIL-001GenvejSAP.reg,0,True”)
End If

Remove files older than X days


Nedenstående VBScript sletter alle filer og sub-foldere indenfor de sidste 7 dage.

Dim objFile,colFileCollection
Set objFSO =
CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(“C:MyTest”)
Set colSubfolders = objFolder.Subfolders
For
Each ObjSubFolder In colSubfolders
Set objTargetFolder = objFSO.GetFolder(“C:MyTest”)
Set colTargetSubfolders = objTargetFolder.Subfolders
Set colFileCollection = objFolder.Files
For
Each ObjTargetSubFolder In colTargetSubfolders
If
DateAdd(“d”, –7, Now()) > ObjTargetSubFolder.dateCreated Then
ObjTargetSubFolder.Delete True
End If
Next
Next

Et andet eksempel kunne være at benytte komandoen FORFILES, som kommer med windows 2003.

FORFILES /p C:Backup-file /s /m *.* /d -7 /c “CMD /C del /Q @FILE”

For mere info se nedenstående link.
http://forums.webhostautomation.com/showpost.php?s=b2301a35427e72673ac5ab14fcf828dd&p=129664&postcount=1

Et sidste eksempel kunne være at benytte PowerShell.
http://myitforum.com/cs2/blogs/yli628/archive/2007/10/02/powershell-script-to-delete-files-older-than-certain-days.aspx

Quick Launch og VBScript


Nedenstående script, tilretter Quick Launch menuen med ens ønskede genvejs applikationer.
Man kan afvikle det i et logon script.

‘Publish applications to Quick Launch

Dim wShell,strQLFolder,strProgram
Set wShell = Wscript.CreateObject("WScript.Shell")
Set colEnvironmentVariables = wShell.Environment("Volatile")
strProgram = wShell.ExpandEnvironmentStrings(
"%ProgramFiles%")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strQLFolder = colEnvironmentVariables.Item(
"APPDATA") & "MicrosoftInternet ExplorerQuick Launch"
If objFSO.FileExists(strQLFolder & "Word.lnk") Then
Else
Set
objShortcut = wShell.CreateShortcut(strQLFolder & "Word.lnk")
objShortcut.TargetPath = strProgram & "Microsoft OfficeOFFICE11Winword.exe"
objShortcut.Save
Set objShortcut = wShell.CreateShortcut(strQLFolder & "Excel.lnk")
objShortcut.TargetPath = strProgram & "Microsoft OfficeOFFICE11Excel.exe"
objShortcut.Save
Set objShortcut = wShell.CreateShortcut(strQLFolder & "Powerpoint.lnk")
objShortcut.TargetPath = strProgram & "Microsoft OfficeOFFICE11POWERPNT.EXE"
objShortcut.Save
End if