Thursday, July 12, 2012

size take from folders

$Dir = "z:\common"
$Server = "server100"
$Volume = "common"
$folders = get-childitem $Dir | where{$_.PSiscontainer -eq "True"}
"Server `tVolume `tFolder `tSize (MB)"
foreach ($fol in $Folders){
$colItems = (Get-ChildItem $fol.fullname -recurse | Measure-Object -property length -sum)
$size = "{0:N2}" -f ($colItems.sum / 1MB)
"$Server `t$Volume `t$($fol.name)`t$size"


for example:
Save the line as common.ps1 - powershell

Powershell e:\mig\common.ps1 >> 20090517.txt

or

@Echo Off
Net Use Y: \\server100\common
Powershell e:\mig\common.ps1 >> 20090517.txt
Net Use Y: /D

To take folder size from vb script (cscript sourcefile.vbs)

Dim objShell  'holds WshShell object   
Dim objExecObject  'holds what comes back from executing the command
Dim strText  'holds the text stream from the exec command.
Dim command  'the command to run
Dim Directory  'directory to query

Directory = Wscript.Arguments(0)

command = "cmd /c dir " & Directory & " /s"

'WScript.echo command
'WScript.echo "starting program " & Now ' used to mark when program begins

Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(command)


Do Until objExecObject.StdOut.AtEndOfStream

   strText = objExecObject.StdOut.ReadAll

Loop

aryText = Split(strText, VbNewLine)

'wscript.Echo "There are " & ubound(aryText) & " items in the " & Directory & " Folder"
Wscript.Stdout.Write Directory
Wscript.Stdout.Write aryText(ubound(aryText) -2)
'Wscript.Stdout.Write aryText(ubound(aryText) -1)

for example :

cscript size.vbs \\server100\newfolder\user1 //NoLogo >> sourcesize.txt

Add run command to excel

Function RunCommand(strCommand)
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec(strCommand)
    While objExec.Status = 1
        Application.Wait "00:00:01"
    Wend
    RunCommand = objExec.StdOut.ReadAll
End Function

LDAP command to Excel

Private Sub Worksheet_Change(ByVal Target As Range)
    arrAddress = Split(Mid(Target.Address, 2), "$")
    strCol = arrAddress(0)
    intRow = arrAddress(1)
    strObjectType = "user"
    strSearchField = Cells(1, strCol).Value
    strObjectToGet = Cells(intRow, strCol).Value
    strCommaDelimProps = ""
    For intCount = 1 To Cells(1, 256).End(xlToLeft).Column
        If strCommaDelimProps = "" Then
            strCommaDelimProps = Cells(1, intCount).Value
        Else
            strCommaDelimProps = strCommaDelimProps & "," & Cells(1, intCount).Value
        End If
    Next
    'MsgBox "Get_LDAP_User_Properties(" & strObjectType & "," & strSearchField & "," & strObjectToGet & "," & strCommaDelimProps & ")"
    strDetails = Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
    arrDetails = Split(strDetails, "|")
    'MsgBox strDetails
    Application.EnableEvents = False
    For intCount = LBound(arrDetails) + 1 To UBound(arrDetails) + 1
        Cells(intRow, intCount).Value = arrDetails(intCount - 1)
    Next
    Application.EnableEvents = True
End Sub

Function Get_LDAP_User_Properties(strObjectType, strSearchField, strObjectToGet, strCommaDelimProps)
     
      If InStr(strObjectToGet, "\") > 0 Then
            arrGroupBits = Split(strObjectToGet, "\")
            strDC = arrGroupBits(0)
            strDNSDomain = strDC & "/" & "DC=" & Replace(Mid(strDC, InStr(strDC, ".") + 1), ".", ",DC=")
            strObjectToGet = arrGroupBits(1)
      Else
            Set objRootDSE = GetObject("LDAP://RootDSE")
            strDNSDomain = objRootDSE.Get("defaultNamingContext")
      End If

      strDetails = ""
      strBase = "<LDAP://" & strDNSDomain & ">"
      ' Setup ADO objects.
      Set adoCommand = CreateObject("ADODB.Command")
      Set ADOConnection = CreateObject("ADODB.Connection")
      ADOConnection.Provider = "ADsDSOObject"
      ADOConnection.Open "Active Directory Provider"
      adoCommand.ActiveConnection = ADOConnection


      ' Filter on user objects.
      'strFilter = "(&(objectCategory=person)(objectClass=user))"
      strFilter = "(&(objectClass=" & strObjectType & ")(" & strSearchField & "=" & strObjectToGet & "))"

      ' Comma delimited list of attribute values to retrieve.
      strAttributes = strCommaDelimProps
      arrProperties = Split(strCommaDelimProps, ",")

      ' Construct the LDAP syntax query.
      strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
      adoCommand.CommandText = strQuery
      adoCommand.Properties("Page Size") = 100
      adoCommand.Properties("Timeout") = 30
      adoCommand.Properties("Cache Results") = False

      ' Run the query.
      Set adoRecordset = adoCommand.Execute
      ' Enumerate the resulting recordset.
      Do Until adoRecordset.EOF
          ' Retrieve values and display.
          For intCount = LBound(arrProperties) To UBound(arrProperties)
                If strDetails = "" Then
                    If IsArray(adoRecordset.Fields(intCount)) = False Then
                      strDetails = adoRecordset.Fields(intCount).Value
                    Else
                      strDetails = Join(adoRecordset.Fields(intCount).Value)
                    End If
                Else
                    If IsArray(adoRecordset.Fields(intCount)) = False Then
                      strDetails = strDetails & "|" & adoRecordset.Fields(intCount).Value
                    Else
                      strDetails = strDetails & "|" & Join(adoRecordset.Fields(intCount).Value)
                    End If
                End If
          Next
          ' Move to the next record in the recordset.
          adoRecordset.MoveNext
      Loop

      ' Clean up.
      adoRecordset.Close
      ADOConnection.Close
      Get_LDAP_User_Properties = strDetails

End Function

LDAP commands


LDAP command details.

=Get_LDAP_User_Properties("user", "samaccountname", <corp_id>, <LDAP provider property name>)


<corp_id>                                                           à           Requested Corp ID(axxxxxx)
<LDAP provider property name>              à           Return Value (column3)


ADUC Tab
Label in AD Users and Computers
LDAP provider property name
WinNT provider property name
Syntax
Multi- Valued ?
LDAP Property Metdod

cn

String


General

givenName

String

FirstName

initials

String



sn

String

LastName

displayName
fullName
String

FullName

description
description
String



physicalDeliveryOfficeName

String

OfficeLocations

telephoneNumber

String



otherTelephone

String
Yes


mail

String

EamilAddress

wWWHomePage

String

HomePage

url

String
Yes

Address

streetAddress

String



postOfficeBox

String



l

String



st

String



postalCode

String



c, co, countryCode

Predefined values


Account

userPrincipalName

String



sAMAccountName
name
String



userAccountControl

Boolean

AccountDisabled

logonHours

Binary

LoginHours

userWorkstations

String
Yes
LoginWorkstations

pwdLastSet

Boolean



userAccountControl

Boolean



userAccountControl

Boolean



userAccountControl

Boolean



accountExpires

Date

AccountExpirationdate
Profile

profilePath
profile
String



scriptPath
loginScript
String

LoginScript

homeDirectory
homeDirectory
String



homeDrive
homeDirDrive
String



homeDirectory
homeDirectory
String


Telephones

homePhone

String

TelephoneHome

otherHomePhone

String
Yes


pager

String

TelephonePager

otherPager

String
Yes


mobile

String

TelephoneMobile

otherMobile

String
Yes


facsimileTelephoneNumber

String

FaxNumber

otherFacsimileTelephoneNumber

String
Yes


ipPhone

String



otherIpPhone

String
Yes


info

String


Organization

title

String



department

String



company

String



manager

Distinguished Name