Home » Scripts, Commands, Registry » Page 3

Category: Scripts, Commands, Registry

Excel Auto sorting Macro

Copy of the macro I used in my autosort Spreadsheet

Private Sub Worksheet_Change(ByVal Target As Range)

If Not (Application.Intersect(Worksheets(1).Range(“A150:H180”), Target) Is Nothing) Then
DoSort
End If
End Sub

Private Sub DoSort()
Sheet1.Range(“A150:H180”).Sort Key1:=Sheet1.Range(“A1”), Order1:=xlAscending, _
Key2:=Sheet1.Range(“D1”), Order2:=xlDescending

End Sub

Taking ownership of multiple folders and files

Situation
Numerous roaming profiles without the administrator added for NTFS permissions. Access was denied for copying, moving etc.

Native tools for taking ownership of multiple folders and files for end user profiles.

Command

Taking Ownership
TAKEOWN.exe /F <folderpath> /R /A /D Y

/F = path
/R=recurvise
/A=owner to administrators rather than logged on user
/D = default answer to setting list folder permission Options: Y or N

Set NTFS Permissions on multiple files
Set permissions instead
icacls.exe <folderpath> /grant administrators:F /T

To run this command on a range of folders get the list into a txt file with each entry on a single line and run

for /f %1 in (c:folders.txt) do takeown.exe /F %1 /R /A /D Y

Unblocking numerous files with sysinternals streams.exe

Problem
After extracting a number of files from a ZIP file windows kept track of these files as dangerous by means of the files ‘stream’ (ie tracking where the file originated from)

Typically to allow these files to execute you need to

Right click the file > properties > UNBLOCK

“This file came from another computer and might be blocked to help protect this computer”

Resolution
The problem was all files were blocked. Resulting in 500 individual changes or…..

To change this (effectivly deleting the stream tracking) to remove the block for all files
Download streams.exe from SYSINTERNALS from here
http://technet.microsoft.com/en-us/sysinternals/bb897440

and then run the following command
streams.exe -s -d %FOLDERPATH%

SCRIPT: loop through users CN, tsprofile paths and userParameters

‘vbscript
Option Explicit
Dim objCommand, objConnection, strBase, strFilter, strAttributes, objUser
Dim strQuery, objRecordset, strdistinguishedName, strTSPath, strCN, objFSO,
Set objCommand = CreateObject(“ADODB.Command”)
Set objConnection = CreateObject(“ADODB.Connection”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
objCommand.ActiveConnection = objConnection
Const ForWriting = 2
‘…set the base DN
strBase = “<ldap://DC=domain,DC=/local>”
strFilter = “(&(objectCategory=person)(objectClass=user))”
strAttributes = “sAMAccountName,cn,distinguishedName”
strQuery = strBase & “;” & strFilter & “;” & strAttributes & “;subtree”
objCommand.CommandText = strQuery
objCommand.Properties(“Page Size”) = 100
objCommand.Properties(“Timeout”) = 30
objCommand.Properties(“Cache Results”) = False
Set objRecordSet = objCommand.Execute
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Do Until objRecordSet.EOF
  strdistinguishedName = objRecordSet.Fields(“distinguishedName”).Value
  Set objUser = GetObject(“LDAP://” & strdistinguishedName)
  On error resume next
 wscript.echo “User:” & objUser.cn & vbtab &  “UserParameters:” & objUser.userParameters & vbtab & ” TSProfilePath:” & objUser.TerminalServicesProfilePath
 ‘ & objUser.sAMAccountName _
  ‘& “,” & objUser.TerminalServicesProfilePath & “,” & objUser.profilePath _
 ‘ & “,” & objUser.homeDirectory & “,” &objUser.ScriptPath
 ‘ objRecordSet.MoveNext
  objRecordSet.MoveNext
Loop
objConnection.Close

SCRIPT: query server uptime output to text file

‘vbscript
‘list computers to check in com,puters.txt in same directory script is run from
‘will write server uptime to uptime.txt in same directory
rfile = “computers.txt”
wfile = “Uptime.txt”

on error resume next
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set r = objFSO.OpenTextFile(rfile, ForReading, True)
Set w = objFSO.OpenTextFile(wfile, ForWriting, True)
arycomp = split(r.readall, vbcrlf)
r.close

for each pc in arycomp
Set objWMIService = GetObject(“winmgmts:\” & pc & “rootcimv2”)
dtmBootup = “”
dtmLastBootupTime = “”
dtmSystemUptime = “”
Set colOperatingSystems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
dtmSystemUptime = DateDiff(“d”, dtmLastBootUpTime, Now)
‘how many days since the last reboot.
‘w.writeline “SYSTEM UPTIME”
w.writeline pc & vbtab & “Up Time (Days): ” & dtmSystemUptime & vbtab & vbtab & “last reboot time: ” & dtmLastBootupTime
‘WScript.Echo pc & vbtab & “UpTime (Days): ” & dtmSystemUptime & vbtab & vbtab & “last reboot time: ” & dtmLastBootupTime
dtmBootup = “”
dtmLastBootupTime = “”
dtmSystemUptime = “”
Next
objWMIService = Nothing
colOperatingSystems = Nothing
next
w.writeline “EOF”
Function WMIDateStringToDate(dtmBootup)
WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & “/” & _
Mid(dtmBootup, 7, 2) & “/” & Left(dtmBootup, 4) _
& ” ” & Mid (dtmBootup, 9, 2) & “:” & _
Mid(dtmBootup, 11, 2) & “:” & Mid(dtmBootup, _
13, 2))
‘uncomment the belowline in order to see the actual time and date the computer was last restarted.
‘wscript.echo WMIDateStringToDate
End Function

SCRIPT: check if folder is empty

‘vbscript
Option Explicit
Dim oFS : Set oFS = CreateObject(“Scripting.FileSystemObject”)
Function FolderEmpty(strFolderPathName)
 Dim oFiles, oFile, oFolder, oSubFolders, oSubFolder
 Dim blnFileFound : blnFileFound = False
 Set oFolder = oFS.GetFolder(strFolderPathName)
 Set oFiles = oFolder.Files
 If oFiles.Count > 0 Then
  FolderEmpty = False
  Exit Function
 End If
 Set oSubFolders = oFolder.SubFolders
 For Each oSubFolder In oSubFolders
  If Not FolderEmpty(oSubFolder.Path) Then
   FolderEmpty = False
   Exit Function
  End If
 Next
 FolderEmpty = True
End Function
Dim strFolderPathName1 : strFolderPathName1 = “file://server/c$/folder
If FolderEmpty(strFolderPathName1) Then
 MsgBox “the folder ” & strFolderPathName1 & ” is empty”
Else
 MsgBox “the folder ” & strFolderPathName1 & ” is not empty”
End If

SCRIPT: check Xen servers logons enabled

‘vbscript
const HKEY_LOCAL_MACHINE = &H80000002
Dim oReg, strKeyPath, strValueName, strDisabled, objArgs, strComputer, arrComputers
Set objArgs = WScript.Arguments
arrComputers = Array(“server1″,”server2”)

sOutput = “Logons Enabled(0) or Disabled(1)” & VBCRLF & VBCRLF
For EACH strComputer in arrComputers
 Set oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\” & strComputer & “rootdefault:StdRegProv”)
 strKeyPath = “SoftwareMicrosoftWindows NTCurrentVersionWinlogon”
 strValueName = “WinStationsDisabled”
 oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strDisabled
 sOutput = sOutput & strcomputer & ”    ” & strDisabled & vbcrlf

On Error Resume Next               
Next
        wscript.echo sOutput