Posts

Showing posts with the label Scripts

Simple array in PowerShell script

Here is a baseline of nested arrays in PowerShell script: PS: Currently output is not sorted, looking for improvements: <#  JD Script to find Last logon time #> $array = @() foreach ($ID in $IDs) { $User = Get-ADUser -Server $dc -Filter {Name -like $ID} -Properties * $obj = New-Object psobject -Property @{  Name = $User.Name            LastLogonDate = $User.LastLogonDate             } $array += $obj }                       #$array | Select * | FT -AutoSize $array | Export-Csv -path $outfile -NoTypeInformation  

PowerShell Script to Get Last logged-on time of AD users of different domain

Scenario: You need to get Last logged-on time of AD users of different domain. Below are the steps: Step 1 : Find a DC of that domain: Get-ADDomainController -DomainName -Discover -NextClosestSite Above command outputs server name of given domain. Step 2: Get-ADUser properties in that server Get-ADUser -Server -Filter {Name -like " "} | Get-ADObject -Properties lastLogon Possible Filters that can be used: GivenName                : First Name Surname                      : Last name Name                          :Login id SamAccountName      :  Login id UserPrincipalName     : Login id@domain.com My PowerShell script: $GivenDomain = "mydomain.com" $IDs = @ ( "myadaccount1" " myadaccount2 " " myadaccount3 " " myadaccount4 " ) $ErrorActionPreferenc...

Script to get disk space in MB (including MountPoints).

I'm looking for a simple script to get disk space in MB (including MountPoints). Below is the latest vbs script I'm going to try: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk where drivetype <> 5") WScript.Echo "Name Capacity Free Free %" For each objDisk in colDisks percentfree = (objdisk.Freespace / objdisk.Size) * 100 With objDisk WScript.Echo .DeviceId & " " & pad(FormatNumber(.Size/1048576, 2)) & " " & pad(FormatNumber(.Freespace/1048576, 2)) & " " & FormatNumber(percentfree, 2) End With Next function pad(strText) strText = Space(12) + strText strText = Right(strText, 10) pad = strText end function Ref: http://www.computerhope.com/forum/inde...

Script to get disk space in MB (including MountPoints).

I'm looking for a simple script to get disk space in MB (including MountPoints). Below is the latest vbs script I'm going to try: strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colDisks = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk where drivetype <> 5") WScript.Echo "Name Capacity Free Free %" For each objDisk in colDisks percentfree = (objdisk.Freespace / objdisk.Size) * 100 With objDisk WScript.Echo .DeviceId & " " & pad(FormatNumber(.Size/1048576, 2)) & " " & pad(FormatNumber(.Freespace/1048576, 2)) & " " & FormatNumber(percentfree, 2) End With Next function pad(strText) strText = Space(12) + strText strText = Right(strText, 10) pad = strText end function Ref: http://www.computerhope.com/forum/inde...

Script with Robocopy to copy folder to multiple destinations

Below was the script I used to copy folders from one source to multiple destination servers: @Echo off   rem below command will generate something like this 2013-09-01-1100 AM if your date/t=Sun 09/01/2013 & time/t=11:03 AM rem rem below command will generate something like this 2013-09-01-0552 if your date/t = 01/09/2013 & time/t = 05:52   For /f "tokens=1,2,3 delims=/ " %%a in ('date/T') do set CDate=%%c-%%b-%%a   For /f "tokens=1,2 delims=:" %%f in ('time /t') do set CTime=%%f%%g set CDate-CTime=%CDate%-%CTime%   rem @echo CDate-CTime is %CDate-CTime% rem for /F %%i in (serverlist.list) do ( echo Processing %%i... >>RobocopyResult_%CDate-CTime%.txt   robocopy E:\MySourceFolder \\%%i\c$\MyDesinationFolder\ /S /E /ZB /R:5 /NP /TEE  >>RobocopyResult_%CDate-CTime%.txt ) Echo . Echo DONE!!!