A request came down from the suits asking if we could export the first and last names, titles, and email addresses of our organization for an outside agency. This agency would like to add each all of our employees as contacts on their mail server.
Seeing as how we ask for .csv files when requesting information from other agencies, we decided to reciprocate the favor and provide a .csv file to this agency. Comma Separated Value files make it easier to run scripts against and facilitate automation.
Here's the script that we came up with in order to get the information requested.
Get-User | Where-Object {$_.RecipientTypeDetails -eq "UserMailbox" -and $_.title -ne "intern" -and $_.WindowsEmailAddress -like "*@organization.org"} | select LastName, FirstName, Title, WindowsEmailAddress |Export-Csv "c:\emailAddressList.csv"
Here's what it does:
It gets all users that have a mailbox and do not belong to interns. It verifies that the email address is one from our organization and not a mail enabled contact. It then collects the information that was requested, i.e. name, title, and email address, and exports those items to a .csv file.
Thanks to Powershell, this operation didn't take long to complete and we were thanked for the quick turn around. We were a bit disappointed that the script was completed so quickly that we had to work on other boring and not-so-fun tasks.
I hope you learned something. This could have been accomplished many ways, but we found this to be simple, yet effective.
Batches and Scripts
Wednesday, September 15, 2010
Thursday, July 15, 2010
Google Adsense
If you found a use for any of these scripts or they were helpful in anyway, please click on one of the advertisements. Thanks.
Thursday, July 8, 2010
Welcome
Welcome to my scripting blog.
Automating routine tasks is very important in IT. There are numerous scripting languages and techniques available to tackle these tasks. I will provide a few samples of scripts and batch files that I use.
Hopefully this will help you understand scripting and enable you to realize areas of your operations that you can automate.
Automation reduces human error and increases productivity.
A lot of these scripts borrowed code easily found online in forums and via Microsoft Technet and the Scripting Guys.
I am not responsible for any damage done by using any of the sample scripts. Try them at your own risk.
Automating routine tasks is very important in IT. There are numerous scripting languages and techniques available to tackle these tasks. I will provide a few samples of scripts and batch files that I use.
Hopefully this will help you understand scripting and enable you to realize areas of your operations that you can automate.
Automation reduces human error and increases productivity.
A lot of these scripts borrowed code easily found online in forums and via Microsoft Technet and the Scripting Guys.
I am not responsible for any damage done by using any of the sample scripts. Try them at your own risk.
A simple batch file to get you started.
Copy and paste this sample code into notepad and save the file with the .bat extension.
ren *. *.doc
Should you put this batch file into a folder containing numerous files that were saved without the .doc extension and double-click the batch file, it will add the .doc extension to all of the files without extensions.
This is a simple batch command that was useful when I presented the solution to one of my coworkers.
My coworker took a call from the helpdesk in which one of our users had this problem. I provided the batch file to the tech that took the call and he was amazed that it solved the problem, avoiding having to individually rename all of the files that weren't associated with Word due to the lack of extension.
ren *. *.doc
Should you put this batch file into a folder containing numerous files that were saved without the .doc extension and double-click the batch file, it will add the .doc extension to all of the files without extensions.
This is a simple batch command that was useful when I presented the solution to one of my coworkers.
My coworker took a call from the helpdesk in which one of our users had this problem. I provided the batch file to the tech that took the call and he was amazed that it solved the problem, avoiding having to individually rename all of the files that weren't associated with Word due to the lack of extension.
Another simple batch file
Here's another simple batch file that uses a for loop.
FOR %%A IN (e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO net use %%A: /delete
Copy this code and paste it into notepad and save with the .bat extension.
This code simply deletes mapped drives using any of the above letters.
This isn't practical and I'm sure there are better ways of achieving the results, but in the end, it works.
FOR %%A IN (e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) DO net use %%A: /delete
Copy this code and paste it into notepad and save with the .bat extension.
This code simply deletes mapped drives using any of the above letters.
This isn't practical and I'm sure there are better ways of achieving the results, but in the end, it works.
Wednesday, July 7, 2010
PSGetSid.exe
Another great utility is PSGetSid.exe.
Have you ever had to change a registry setting in the HKey Users registry hive? It's difficult to tell which is which and makes it difficult to change a setting for a particular logged on user due to the hive masked by the SID of the user. PSGetSid.exe will get the currently logged on user's SID.
We use something similar to the commands below in a batch file in order to make the necessary changes.
In order to automate the batch file without intervention by the user or technician, a registry key has to be altered in order for the utility to be registered in the registry.
REGEDIT /S "\\server\share\getSid.reg"
The getSid.reg contains the following to register the utility:
REGEDIT4
[HKEY_CURRENT_USER\Software\Sysinternals]
[HKEY_CURRENT_USER\Software\Sysinternals\PsGetsid]
"EulaAccepted"=dword:00000001
Copy the above and save as getSid.reg in order to use it in the script above.
REM ** The following code will run the PsGetSid.exe and export to a text file.
"\\server\share\PsGetsid.exe" %username% > \\server\Share\%username%.txt
REM ** The next step is to parse through the information and assign it to a variable
FOR /F "skip=1" %%G IN (\\server\share\%username%.txt) DO set userId=%%G
REM ** Using the variable, we create the registry setting.
reg add "HKU\%userId%\Software\Sonic\Registration\Roxio Easy Media Creator 9" /f
The above example uses the Roxio software due to the necessity of registering it under every user profile on the system. I'm not sure about you, but our users call us for every single pop-up message they receive, even a simple "Never Register" check box. So, we try and eliminate calls by automating the process.
This was just an example of the use of PSGetSid.exe, a great utility to automate registry settings for a logged on user.
At the end of the above batch file, you can add a del \\server\share\%username%.txt in order to delete the file when it is no longer necessary.
PSGetSid.exe is a great utility from SysInternals and provided freely by Microsoft.
Download it here - http://technet.microsoft.com/en-us/sysinternals/bb897417.aspx
Happy scripting and good luck.
Have you ever had to change a registry setting in the HKey Users registry hive? It's difficult to tell which is which and makes it difficult to change a setting for a particular logged on user due to the hive masked by the SID of the user. PSGetSid.exe will get the currently logged on user's SID.
We use something similar to the commands below in a batch file in order to make the necessary changes.
In order to automate the batch file without intervention by the user or technician, a registry key has to be altered in order for the utility to be registered in the registry.
REGEDIT /S "\\server\share\getSid.reg"
The getSid.reg contains the following to register the utility:
REGEDIT4
[HKEY_CURRENT_USER\Software\Sysinternals]
[HKEY_CURRENT_USER\Software\Sysinternals\PsGetsid]
"EulaAccepted"=dword:00000001
Copy the above and save as getSid.reg in order to use it in the script above.
REM ** The following code will run the PsGetSid.exe and export to a text file.
"\\server\share\PsGetsid.exe" %username% > \\server\Share\%username%.txt
REM ** The next step is to parse through the information and assign it to a variable
FOR /F "skip=1" %%G IN (\\server\share\%username%.txt) DO set userId=%%G
REM ** Using the variable, we create the registry setting.
reg add "HKU\%userId%\Software\Sonic\Registration\Roxio Easy Media Creator 9" /f
The above example uses the Roxio software due to the necessity of registering it under every user profile on the system. I'm not sure about you, but our users call us for every single pop-up message they receive, even a simple "Never Register" check box. So, we try and eliminate calls by automating the process.
This was just an example of the use of PSGetSid.exe, a great utility to automate registry settings for a logged on user.
At the end of the above batch file, you can add a del \\server\share\%username%.txt in order to delete the file when it is no longer necessary.
PSGetSid.exe is a great utility from SysInternals and provided freely by Microsoft.
Download it here - http://technet.microsoft.com/en-us/sysinternals/bb897417.aspx
Happy scripting and good luck.
Ejecting the CD Tray
During equipment refreshes, it is common to forget to check the CD drive for CDs. We have quite the collection of unclaimed CDs, music and otherwise, due to forgetting to verify that the drive is empty. For some this may be a plus, but for others it is common to find important work-related information on CDs.
Fortunately for those that do not want to search for the owner of a CD, we have a solution. There is a free command-line utility that you can use in a batch file during refresh that will eject the tray of the drive, ensuring that the tech responsible for the replacement will check the drive for a CD.
The utility is nircmd.exe.
It can be found here - http://www.nirsoft.net/utils/nircmd.html
There are many uses for this utility and they can be found at the site. You'd be amazed what this small utility can do! And, best of all, it's free!
You can use it in a batch file like this:
"\\Server\share\nircmd.exe" cdrom open d:
Fortunately for those that do not want to search for the owner of a CD, we have a solution. There is a free command-line utility that you can use in a batch file during refresh that will eject the tray of the drive, ensuring that the tech responsible for the replacement will check the drive for a CD.
The utility is nircmd.exe.
It can be found here - http://www.nirsoft.net/utils/nircmd.html
There are many uses for this utility and they can be found at the site. You'd be amazed what this small utility can do! And, best of all, it's free!
You can use it in a batch file like this:
"\\Server\share\nircmd.exe" cdrom open d:
Subscribe to:
Posts (Atom)