Category Archives: Powershell

Compare folder inheritance

 

inheritanceIn the middle of file server-migration where we migrated hundreds of folders in to a new folder structure on another server we had to compare the folder inheritance since it on the old sever where randomized. We made this script that uses an import from an csv file we used to handle the copy jobs source and target. The csv looked like this:

Job;Source;Target
BUF01;N:\Groups\XX\BUF ADMINISTRATION;\\srv-fil05\Gemensam$\XX\Administration och nämnd
BUF02;N:\Groups\XX\EKONOMI;\\srv-fil05\Gemensam$\XX\Administration och nämnd

The script results in a file that displays all source paths that doesn´t compiles with the targets inheritance.

Simple way to create quota reports with powershell

reports

A week after we began to charge departments for large quotas the IT coordinator requested a report of all their users quotas.
In this case i chose to use an CSV-file so i easy can feed the CSV with other users if a get further requests in the future.
The CSV looks like this:
förvaltning,mapp,mail
KSF,D:\Forvaltningar\KSF,user1@huddinge.se
KUF,D:\Forvaltningar\KUF,user2@huddinge.se
SAF,D:\Forvaltningar\SAF,user3@huddinge.se;user6@huddinge.se
MSB,D:\Forvaltningar\MSB,user4@huddinge.se
BUF,D:\Forvaltningar\BUF,user5@huddinge.se

The New-FsrmScheduledTask schedules a task 15:00 the fourteenth every month.

Renew certificate on Exchange 2010

SSL-padlock

 

The last day before a week of to Spain with the family the boss booked me to consult her on renewing the certificate on our exchange-servers. With little time and no will to work on my vacation, I threw together this script so that she could perform the task herself.

Function get-lastlogon

An easy function to get last logon timestamp in domain and convert it to readable time and date.

Migrate quota to new disk

In our project to migrate all our servers from our old SAN and VMware environment to our brand new top of the line EMC VNX VPlex SAN and new Dell VMware-hosts, we only have one server left, the server that hosts the home directories for all our users(except students). In our old environment we have an 12 terabyte raw device mapping, so the only way to migrate it with VMware is by cold migration and approximately 48 hours of down time. we also wanted to shrink it and convert it to a vmdk instead of an rdm. We decided to create a new vmdk-disk and use robocopy to copy all the files, doing so we reduce downtime for our users and gets a new smaller disk. Robocopy don´t copy folder quotas and we have pretty complex quota structure with individual quotas  on each users(about 8000) home directory, therefor me and my college Stefan Lozancic created a script to that migrates all quotas to the new disk.

List directories where object have explicit permissions set.

Today I got a case from a user who wanted to know which directories that would be effected if he deleted a security group. This is a typical case that that often end up on second or third-line, therefor I wrote a function so that help-desk easily can do it the next time a user adds a similar case. First it lists all sub directories of the  path specified, it then uses a foreach loop  to find out if the object specified exists in  the IdentityReference property and list the pspath. since pspath includes “Microsoft.PowerShell.Core\FileSystem::” when you try to export to csv i had to do a replace with nothing to get a clean path. when trying to replace i got an error “The regular expression pattern”…”is not valid”  since there is a “\” and -replace parses the string as an regular expression you need to escape it by putting another “\” in front of it. I made the export to file so my colleagues would get a file to mail to the user.

 

Monitor solresor and notify when hotel is available

Last friday me and my girlfriend finally decided a hotel and date for the summer holiday. She had found a hotel to reasonable price at the travel-agent solresor. I told her I just had to eat and would book the trip right after lunch. When I came back the hotel was sold-out at the date we had planned for. I called the travel-agent and asked if they had any notification-service, if someone would cancel their trip, unfortunate they had no such service and they told me to check their website manually to see if the hotel would reappear. Since I am lazy and didn´t want to manually check their website a built a script to do it for me.
I found out that on step 4 in their booking the website created an unique URL with hotel short-name and departure date, if I used that URL I would get an answer with body if the hotel and date were available and I would get an empty answer if the date was sold-out.
So I made a script that checked the URL once every minute and would send me an email if the URL existed. Unfortunately the hotel never showed up, but it was fun to create the monitor.

List and count users that never logged on to domain

neverloggedin

I got an urgent case from the boss, we had to compare a list of users from our meta-catalog with users in our AD and list and count which users who never logged on to domain. I imported the csv in an variable and then used an foreach-loop to check if the users had logged in or not. We also wanted to list and count all users that where included in the csv-file but not in the active driectory, I used the ErrorVariable to do that and the appended each entry to a text-file with the out-file append switch. As you can see I used two different techniques to export multiple data from the foreach-loop. The first one when I exported the users that hadn´t logged on, where I first created an variable with an empty array  “$users = @()” , then instead of running the command to get the users I used the + to fill the array with result “$users +=”, on the second I used the out-file as I explained above.  When the lists where done I used the count and length property.

 

Complete network-setup on ESX host with vSphere powerCLI

PowerCLI

Me and my colleague Stefan Lozancic had the task to configure the networks on our new Vsphere esx hosts. It was 14 hosts and 54 vlans, 1 vmotion and 1 management-network on each host that had to be configured. We used vSphere powerCLI to do it automated. First we populated a csv-file with all the vlans from one of our old hosts.

After that we rebuild the vmotion and production network from scratch and populated the production network with the vlans from the csv. Since the hosts where prepared by two different person some hosts had the management network cards in an active/passive mode, so we made a check and changed those to active/active.

Bulk change password at next logon

heartbleed

Because one of our services that are exposed to the internet had the famous Heartbleed bug we had to do a bulk change password at next logon for 1100 users. A pretty easy task (maby to easy for this blog?) but since it´s smoking hot and one of my colleges (let`s call him J.B) asked if I could post the script on the blog, here it is. First I collected all the users in a csv-file, then I imported the csv-file and put the content in an foreach loop and changed the ChangePasswordAtLogon attribute to true. I am not sure if picking the cn attribute is correct but set-ADUser says -Identity should be “LDAP display name” and 5 seconds of sloppy googling I got an answer that cn equels LDAP display name, but I got some errors when setting the attribute in the second script som it might be better to pick sam account name.