Thursday, 21 August 2014

Registry - start cmd as SYSTEM before login (utilman.exe)


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\utilman.exe]
"Debugger"="cmd.exe"

Powershell - Move Mailboxes from a text file in Exchange 2007 with Powershell


Get-Content users.txt | move-mailbox -TargetDatabase 'EXCHANGESERVERNAME\EXCH-SG-001\EXCH-MBX-001'  -Confirm:$false

Wednesday, 20 August 2014

Powershell - delete log files older than 7 days


#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "7"
#----- define folder where files are located ----#
$TargetFolder = "F:\folder"
#----- define extension ----#
$Extension = "*.log"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "Red"
        Remove-Item $File.FullName | out-null
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }

Powershell - get a list of all connected ActiveSync devices to your Exchange


$devices = @()
$mailboxes = Get-CASMailbox -ResultSize:Unlimited | Where-Object {$_.HasActiveSyncDevicePartnership -eq $true -and $_.ExchangeVersion.ExchangeBuild -ilike "8*"}

foreach ($m in $mailboxes)
{
    $devices += Get-ActiveSyncDeviceStatistics -Mailbox $m.Identity
}

$devices | Export-Csv c:\temp\DeviceStats.csv

Powershell - send email with attached file


$date = Get-Date
$file = "C:\temp\report.html"

$smtpServer = "mai.yourdomain.com"
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "sender@yourdomain.com"
$msg.To.Add("receiver.yourdomain.com")
$msg.Subject = "report from " + $date
$msg.Body = "see attachment"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()



$outfile = “c:\temp\sent.txt”

“Email Sent at ” + $date | out-file $outfile

VBS - last reboot time

strComputer = "." ' Local computer

set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
    objWMIDateTime.Value = objOS.LastBootUpTime
    Wscript.Echo "Last Boot Up Time: " & objWMIDateTime.GetVarDate & vbcrlf & _
        "System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,Now) & _
        " (hh:mm:ss)"
next

Function TimeSpan(dt1, dt2)
    ' Function to display the difference between
    ' 2 dates in hh:mm:ss format
    If (isDate(dt1) And IsDate(dt2)) = false Then
        TimeSpan = "00:00:00"
        Exit Function
        End If

        seconds = Abs(DateDiff("S", dt1, dt2))
        minutes = seconds \ 60
        hours = minutes \ 60
        minutes = minutes mod 60
        seconds = seconds mod 60

        if len(hours) = 1 then hours = "0" & hours

        TimeSpan = hours & ":" & _
            RIGHT("00" & minutes, 2) & ":" & _
            RIGHT("00" & seconds, 2)
End Function

VBS - check if service status is "running", if not kill it and start again


strComputer = "."

Set ServiceSet = GetObject("winmgmts:").ExecQuery("select * from Win32_Service where Name='Spooler'")

for each Service in ServiceSet
RetVal = Service.StartService()
'wscript.echo RetVal
if not RetVal = 10 then    'control if service has state running
'WScript.Echo "Name: " & Service.Name & ", " & "Status: " & Service.Status & ", " & "ProcessId: " & Service.ProcessId & ", " & "ServiceState: " & Service.State
strProcessId = Service.ProcessId
Dim oShell, oExec, sLine
Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec("taskkill /f /pid " & strProcessId)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:win32_service.name='Spooler'")
objWMIService.StartService
end if
next

VBS - disable all HP printers notifications / popups


' Author martin junge
' Version 0.1 - 16-7-2014
' -----------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer, intPrinters

strComputer = "."
intPrinters = 1

dim strtemp, strtemp1, strtemp2, strDrucker, strRegkey, WshShell

Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery _
("SELECT * FROM Win32_Printer")

On Error Resume Next
For Each objItem In colItems
if (instr(objItem.DriverName, "HP")) then
'WScript.Echo "Printer: " & objItem.DeviceID & ", " & objItem.DriverName

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
strDrucker = objItem.DeviceID
strtemp = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers\"
'----
strtemp1 = "\PrinterDriverData\SSNPNotifyEventSetting"
strRegkey = strtemp & strDrucker & strtemp1
'wscript.echo strRegkey
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite strRegkey, 0, "REG_DWORD"
'----
strtemp2 = "\PrinterDriverData\SSNPDisableAll"
strRegkey = strtemp & strDrucker & strtemp2
'wscript.echo strRegkey
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite strRegkey, 1, "REG_DWORD"
'----
end if
intPrinters = intPrinters + 1
Next


WScript.Quit

' End of Sample Printer VBScript

Wednesday, 22 February 2012

Registry - Windows 7 - Windows Explorer Settings

Organize
Folder and search options
Navigation pane
Show all folders
Automatically expand to current folder


create a cmd file with

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v NavPaneShowAllFolders /t reg_dword /d 1 /f

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v NavPaneExpandToCurrentFolder /t reg_dword /d 1 /f

Registry - Excel - Trust access to the VBA project object model

Excel 2007 / 2010 -

Options
Trust Center
Trust Center Settings...
Macro Settings
Trust access to the VBA project object model



create a cmd file with

reg add "HKCU\Software\Microsoft\Office\12.0\Excel\Security" /v AccessVBOM /t reg_dword /d 1 /f

reg add "HKCU\Software\Microsoft\Office\14.0\Excel\Security" /v AccessVBOM /t reg_dword /d 1 /f


deploy it per GPO start script