Sep 06 2015

Delete Files Older Than in DOS / Powershell

Published by under Windows

System admins face more and more ever-growing directories storing auto-generated reports by scripts or scheduled tasks. Same goes to temp directories and nobody bother cleaning them up. But isn’t it our job too?
Here are 2 simple scripts that can be run every so often to delete files older than say 30 days for instance in Powershell and DOS.


Remove Files in DOS

The first in DOS is more limited since ForFiles can only deal with the last modified date.

Echo @off
Cls

Set Folder=C:\Reports

if exists %Folder% (
  rem *******************************************
  rem Remove system attributes and cache files
  rem Not processed by the del command
  Forfiles /S /P "%Folder%" /M * /D -30 /C "cmd /c attrib -s -h @path"
  rem Remove files older than 30 days
  Forfiles /S /P "%Folder%" /M * /D -30 /C "cmd /c del /F /Q @path"

  rem Remove empty directories
  for /f "delims=" %%d in ('dir /S /B /AD %SrcDir% ^| sort /R') do rmdir "%%d"
)


Delete Files in Powershell

Powershell lets you work with the 3 parameters LastAccessTime, LastWriteTime and CreationTime.

$limit = (Get-Date).AddDays(-30)
$path = "C:\Temp"

if (Test-Path $path) {
  # Delete files older than $limit days.
  Get-ChildItem -Path $path -Recurse | 
  Where-Object { !$_.PSIsContainer -and $_.lastAccessTime -lt $limit } |
  Remove-Item -Force

  # Delete any empty directories left behind after deleting old files.
  Get-ChildItem -Path $path -Recurse |
  Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse |
  Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse
}


To run the script, check powershell restrictions with

Get-ExecutionPolicy

If restricted, run

Set-ExecutionPolicy RemoteSigned

And run the following command in a schedule task to delete files on a regular basis:

powershell C:\Scripts\ClearFolder.ps1
 

No responses yet

Jul 11 2015

Exchange Addressbook Does Not Update in Outlook

Published by under Exchange

When I update Exchange 2013 Address Book (phone number or any other details), Outlook does not seem to get updated, even the next day. It does not seem to be a problem on the client side since it tries to get the latest address book updates from Exchange at regular intervals.

You can workaround this by disabling Outlook cache but most people will lose access to the address book while offline, and they certainly do not want that.

Another way is to force Exchange to refresh the offline address book (OAB), from which Outlook client get updates.
Write this short Powershell script UpdateAddressBook.ps1

add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Get-GlobalAddressList | Update-GlobalAddressList
Get-AddressList | update-AddressList
Get-OfflineAddressBook | Update-OfflineAddressBook


And add a scheduled task to be run a few times a day

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe


with the previous script in arguments (on a single line)

-command ". 
'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1';
Connect-ExchangeServer -auto;" C:\Scripts\UpdateAddressBook.ps1


End users will not get the latest updates straight away but a synchronisation a few times a day should be sufficient.

 

No responses yet

Jun 04 2015

Uninstall Microsoft Exchange 2010

Published by under Exchange

Microsoft Exchange 2010 uninstall wizard throws some errors unless you previously go through the following steps.

This will remove all mailboxes, contacts, etc…! There’s no way back

We assume this is the last Exchange server. If not, be more specific on mailboxes to be removed, with -database for instance, or -server on connectors.

Remove user mailboxes but not AD user accounts!

Get-mailbox | disable-mailbox


Delete arbitration mailboxes

Get-Mailbox -Arbitration | Disable-Mailbox -Arbitration -DisableLastArbitrationMailboxAllowed


Remove offline address book

Get-OfflineAddressBook | Remove-OfflineAddressBook


Remove connectors

Get-SendConnector | Remove-SendConnector


Finally remove Public folder. This is a little bit tricky since Exchange does need one public folder database.
It can be removed with the tool ADSIEdit

Remove Exchange public folders with ADSIEdit


Run ADSIedit
Right click on Configuration and Settings…
Under “Select a well known Naming Context:” choose “Configuration”

Then browse to:

CN=Configuration,DC=DOMAIN,DC=LOCAL
CN=Services
CN=Microsoft Exchange
CN=EXCHANGE_ORG
CN=Administrative Groups
CN=Exchange Administrative Group (FYDIBOHF23SPDLT)
CN=Databases
CN=PUBLIC_FOLDER_DATABASE


Now you can delete the Public Folder Database and uninstall Microsoft Exchange 2010 with no hassle.

 

No responses yet

May 26 2015

Run AS400 Session on Windows Startup

Published by under AS400

You may need to launch AS400 sessions at boot for various reasons such as automated scripts or print sessions. Here are a few pitfalls you may encounter.

First of all, sessions wouldn’t launch without pcsws.exe extra parameters /H – for Hidden – and eventually /Q – to hide the IBM popup window. pcsws.exe options are all listed on IBM for reference.

This will work for a single session. In order to deal with 2 or more sessions, they have to be launched with the “start” command. Add /B parameter to hide the DOS command window followed by “”, or the main command parameters will be being ignored.

This makes me come up with the following simple batch.

@echo off

Set ScriptPath=C:\Scripts
Set client_Access_Home=C:\Program Files (x86)\IBM\Client Access
Set AS400=My_iSeries_Host
Set iSeries_User=myUser
Set password=myPassword

"%client_Access_Home%\cwblogon.exe" %AS400% /u %iSeries_User% /p "%password%"

start /B "" "%client_Access_Home%\Emulator\pcsws.exe" %ScriptPath%\session.ws /Q /H
start /B "" "%client_Access_Home%\Emulator\pcsws.exe" %ScriptPath%\other_session.ws /Q /H

pause


Create a new entry in the task scheduler to be run at server boot.
Select the user it should be run under and tick “Run whether user is logged on or not” and set the bat file defined above under Actions.

The “pause” command is mandatory. If removed, the task will end, the cmd process shuts leaving the pcsws.exe processes up but orphans. The printer writer will remain in the END state on IBM i !
If anybody has a clue as to why, please leave a comment.
cwblogon.exe is far from ideal on the security side, it is best to define a restricted profile! Set the same user in iSeries Navigator or in the session parameters.

This was tested on Windows Server 2012. I have no clue if it works on other OS but there’s no reason why it would not.
You can now schedule reboots anytime you like without having to deal with starting sessions manually.

For printer sessions though, you’re better off printing to a Windows printer share. It’s probably a better and cleaner way to do it.
You can connect with SSH protocol if you want to run shell scripts or CL programs (with the “system” command) and do many other things.

 

2 responses so far

May 23 2015

AS400 Printing to a Windows Printer Share

Published by under AS400

IBM i can communicate with a Windows printer share using the LDP Unix protocol. This is so much better than a Client Access printer session since it relies on a simple share and a Windows service. There is no need to start a session manually.

On Windows

On the Windows server, open server manager, add the Print Server role and make sure to select LPD Service. The service should be up and running after reboot.

LPD Service on Windows


Once this is done, add a printer, install the correct drivers and create a share from the printer properties. Make sure the test page is printing correctly.

On AS400

IBM recommend to create a remote OUTQ but this is not ideal for some applications that need a print device.
I will then go for a virtual printer coupled with a remote OUTQ. This is a 2 steps matter.

Create a virtual printer device:

CRTDEVPRT DEVD(MYPRINTER) DEVCLS(*VRT) TYPE(3812) MODEL(1) ONLINE(*NO) FONT(11)

Set the proper settings to the matching OUTQ:

CHGOUTQ OUTQ(QUSRSYS/MYPRINTER) RMTSYS(*INTNETADR) RMTPRTQ('share')
AUTOSTRWTR(1) CNNTYPE(*IP) DESTTYPE(*OTHER) TRANSFORM(*YES) 
MFRTYPMDL(*WSCST) WSCST(QSYS/QWPDEFAULT) INTNETADR('XXX.XXX.XXX.XXX')


Windows printer share can be a solution if your printer is not supported on AS400/IBM i and you have Windows drivers.

 

No responses yet

« Prev - Next »