Uploading files to Sharepoint using powershell

Today I got a bit creative and threw together a small example for my students about how you can use powershell to upload files to Sharepoint. The script doesn’t have any error handling or anything of the sort, I leave that up to whomever uses the code as a bas.. . It takes a site url, and library name as parameters and then uploads the argument list. The file is also attached to this post.

  1. param($site=$(throw “You must specify a site”), $list=$(throw “You must specify a list”))
  2. if ($args.count -le 0) { throw “You must supply atlest one file to upload” }
  3.  
  4. [void][System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
  5. $spSite = New-Object Microsoft.SharePoint.SPSite($site)
  6. $spWeb = $spSite.OpenWeb()
  7. $spList = $spWeb.GetFolder($list)
  8. $spFiles = $spList.Files
  9.  
  10. for ($i=0; $i -le $args.count;$i++)
  11. {
  12.  $fileInfo = Get-ChildItem $args[$i]
  13.  $fileContent = Get-Content $fileInfo -encoding byte
  14.  $fileName = “{0}/{1}” -f $list, $fileInfo.Name
  15.  $fileMetadata = New-Object HashTable
  16.  $spFile = $spFiles.Add($fileName, $fileContent, $fileMetadata, $true)
  17. }

Inspired by Sharepoint MVP Tobias Zimmergren and his example of doing it in C#

Small Business Server 2008 and Home Server

I’m delivering a SBS 2008 and Home Server seminar the 29:th of may 2009 at Informator utbildning in Stockholm. There are a limited number of seats left so don’t miss out on it. Go and register. Its free http://www.informator.se/product.aspx?ArticleNr=PS0908

If you can’t make it for this seminar don’t miss the Sommarkollo 2009 at Microsoft which features lots of speakers, about lot of topics, over several days. Open at no charge for microsoft partners. Read more about my presentations here or about the event here.

Sharepoint 2010 preliminary system requirements

I’ve been busy prepping courses and delivering courses so I haven’t been that around the blogs and haven’t had time to share so much. But still: the sharepoint team released some information about the Sharepoint 2010 version. As expected it will be x64 only, and supported SQL will be x64 also. They have also done a lot of work to be XHTML 1.0 compatible so there is no extensive support for older readers; and thats about time in my opionion!

Read http://blogs.msdn.com/sharepoint/archive/2009/05/07/announcing-sharepoint-server-2010-preliminary-system-requirements.aspx

MCT/MVP night out – grill and knowledge

Swedish only posting: Invitation to MCT/MVP night out – grill and knowledge for swedish MCT/MVP community.

Den 10:e juni anordnar jag “MCT/MVP night out – grill and knowledge”, klockan 1800-2230 på Gärdet i stockholm, en tillställning med både kunskap och nätverkande i sikte. Alla MCT/MVP är hjärtligt välkomna, sprid gärna inbjudan till de du känner!

Boule, eller kubb, grill och öl kommer finnas på plats. Microsoft bidrar med krubbet. Vi inleder med en presentation (ej bestämt vem ännu men ett par namn finns som jag väntar ok från), körs via Groove självklart om det behövs, sedan kubb/bouleturnering och därefter “open space” vid grillen..

RSVP senast 3:e juni via mail till daniel@sorlov.com, ange subject GRILL

Truly random numbers and passwords in Powershell

This week I have been teaching a MS6434 course (Powershell) and during the students labs I always sit and try some new technologies. One thing that one of my participants brought up was how to generate random passwords and that the standard RANDOM isn’t always realy random. Surely there must be a solution to this I tought and here is the resulting function:

  1. function Get-RealyRandomInt
  2. {
  3. $randomBytes = New-Object byte[] 4
  4. $(new-object System.Security.Cryptography.RNGCryptoServiceProvider).GetBytes($randomBytes)
  5. return [BitConverter]::ToInt32($randomBytes,0)
  6. }

Short, sweet and truly random. Use this to your heats content. I have also wrote a small function to generate a password:

  1. function Get-Password
  2. {
  3.  param ([int]$length=8,[switch]$noSpecialChars,[switch]$noNumbers,[switch]$noLowerCase,[switch]$noUpperCase,[switch]$includeUbiquitous)
  4.  
  5.  #Setup valid chars for generation of password
  6.  if ($includeUbiquitous) { $chars += “L”,”l”,”i”,”I”,”j”,”J”,”O”,”o”,”0″,”1″}
  7.  if (!$noUpperCase)      { $chars += “A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”K”,”M”,”N”,”P”,”Q”,”R”,”S”,”T”,”U”,”V”,”X”,”Y”,”Z” }
  8.  if (!$noLowerCase)      { $chars += “a”,”b”,”c”,”d”,”e”,”f”,”g”,”h”,”k”,”m”,”n”,”p”,”q”,”r”,”s”,”t”,”u”,”v”,”x”,”y”,”z” }
  9.  if (!$noSpecialChars)   { $chars += “%”,”#”,”&”,”(“,”)”,”=”,”?”,”-“,”_”,”§”,”;”,”:”,”<“,”>”,”@”,”*” }
  10.  if (!$noNumbers)        { $chars += “2”,”3″,”4″,”5″,”6″,”7″,”8″,”9″ }
  11.  if ($chars.length -le 0)  {  throw “You must cannot exclude all classes of chars, passwords without chars are not good.”  }
  12.  
  13.  #Loop generation process for specified length and return the password
  14.  for ($i=1; $i -le $length; $i++)  {  $output += $chars[$(New-Object Random(Get-RealyRandomInt)).Next($chars.Length)]  }
  15.  return $output
  16. }

Both theese examples can be downloaded from the attached file

Updated at 2009-05-20 21:45: A big thanks to Niklas Goude for correcting a spelling mistake in the variable $includeUbiquitous above. Thanks!

I just had to do it: crack a hotel key system

I have always wondered whats so great about the plastic cards you get when you check in at a hotel. Whats the magic touch? They are cheap and easy to replace when you loose them – which keys are not. So that must be the reason. Cheap price – cost savings. However something in me tells me if they are cheap they cannot be secure? Or can they? So what is on the cards? I just had to know; I took my key and read it into a magnetic stripe reader. The information on the key was:

  • Room number
  • Valid from
  • Valid to

Okey; so the doors must be connected to some computer? No. The room number is set in the door it-self and kept safe there by a EEPROM. So how could this be used? Well I just changed the room number from my own to a collegues – it worked. Like a charm. Thats not good. But I think its to much work to just re-key for every room I wish to enter so I decided to be creative, whats on the housekeeping key? Must be more secure? Right? No. It wasn’t; when I changed the room number value to 0, 9999, or -1 the key worked in both our rooms at the same time – this could be different between many systems but just a 20 minute guessing game and we were in.

I have disclosed this information to the selected hotel and they were a bit concerned, and they are looking into this right now. The equipment used was a magnetic card reader/writer (as found on http://www.hackershomepage.com/section6.htm), but lended from a POS VAR here in Sweden. Should it realy be this easy? Secure or easy? Your choice.

So what am I trying to say? Computer security starts with physical security. Because in the hotel room you leave your computer, with all the passwords to your network. If your lucky you will just get robbed of your money or laptop – but worst case scenario is that you ruin your company – disclosing all sensitive business information.

Selected to present SBS & EBS at "Microsoft Sommarkollo 2009"

I have been selected to present on SBS and EBS at the Microsoft Sommarkollo 2009 in Sweden. The Sommarkollo is a free (or has been the previous years) microsoft event arranged yearly where partners and customers to microsoft during the summer can deep dive into lots of subjects in seminars and hands on labs. As of now I have no further information than this.

Small Business Server 2008: Small Business Server is a classic, this session is aimed at technicans about to plan, implent and administer SBS.
Date: 25/6 Gothenburg, 0900-1200
Date 20/8 Stockholm, 0900-1200

Essential Business Server 2008: Learn more about the EBS concept, effectiveness and gains. Hands on labs.
Date: 25/6 Gothenburg, 1300-1600
Date 20/8 Stockholm, 1300-1600

More information when avilable will be here (I think, right now its just last years Sommarkollo): http://www.microsoft.se/sommarkollo

Quick introduction to Windows Virtual PC

Finally a new version of Virtual PC, this time around the product have changed name to Windows Virtual PC and is totally integrated with Windows Explorer. There is no separate GUI any more and there is a lot of exciting features such as application publishing to the host OS (such as used for Windows 7 XP Mode). The Virtual PC requires one of the premium editions of Windows 7.

The current release (beta) is distributed as a hot fix, a single .exe file which you double click and “next-next-reboo”t. After reboot there is two new icons on your start menu Windows Virtual PC and Virtual Windows XP. I decided against installing the Windows Virtual XP system and opted only for the Windows Virtual PC install. The Virtual Windows XP is basically a pre configured Windows XP machine running virtual.

after_install

After install you will find a special folder called My Virtual Machines. This is the main “console” where you administer your virtual machines. The machines will be listed here and can be started, stopped and configured here. To add a new machine you just click the “Create new Virtual Machine” in the toolbar.

In the first step you specify a name and location for the image. This is just as in Virtual PC 2007.

New_machine

Then basic settings of memory and networking. The guest can support four network connections if configured manually later on. If you check the box a shared NAT adapter will be set up.

New_machine_2

The last dialog is to setup virtual hard disks. You can select dynamic, existing or advanced. Advanced mode lets you setup differencing disks also.

New_machine_3

After the virtual machine is created it will be listed and you are able to left-click and set options and control the state of the running virtual machines.

after_machine_creation 

The settings dialog is quite similar to the earlier versions. You are able to setup IDE controlled disks (4), network controllers, connect serial ports to physical ports, text files, etc. The most exciting features are the Auto Publish which applications installed on the virtual machine available on the host. The feature requires Integration Components to be installed.

settings 

The machines are running exactly like in Virtual PC 2007 and besides the above features. Read more and download the beta today from MSDN or TechNet. If you dont have TechNet then you will be able to find it public on http://www.microsoft.com/windows/virtual-pc/ after May 5th. You are able to download Virtual PC and Virtual XP as separate downloads.

Running