Archive for the ‘Code’ Category

PowerShell: Get Current User’s Security Permissions

I was writing a little script and thought that needs to ensure it has sufficient privileges. I thought it would be more useful to find if the current user is part of any of the built in user roles. Here is my effort:

Get-CurrentUserRoles {

-ArgumentList ([System.Security.Principal.WindowsIdentity]::GetCurrent())

= @{}

    [System.Enum]::GetNames(“System.Security.Principal.WindowsBuiltInRole”) | ForEach-Object {

        $RolesHash[$_] =





PS C:\> Get-CurrentUserRoles


Name Value

—- —–

Administrator True

User True

Guest False

PowerUser False

AccountOperator False

SystemOperator False

PrintOperator False

BackupOperator False

Replicator False



PS C:\> (Get-CurrentUserRoles).Administrator



PS C:\> (Get-CurrentUserRoles).Guest




As you can see, you get back PowerUser, Administrator, SystemOperator, etc.

Categories: PowerShell

Team Foundation Services and PowerGUI – Using PowerShell with Version Control

April 29, 2013 15 comments

Version control allows you and others to work on the same scripts, check them in and out, etc. By following these steps, it is possible to use version control system for PowerShell as well as a number of other programming languages.

For PowerShell, PowerGUI is by far my favourite GUI/Script Editor, it is free, far superior to ISE (which is improving) and my preference to PrimalScript. Although PowerGUI is my preference for PowerShell development, for pretty much all other scripting, PrimalScript is superb.

Primal’s PowerShell Studio is an excellent PowerShell GUI/Script Editor. It has a similar feature set to PowerGUI, with some advantages and some disadvantages- one thing that it excels at is PowerShell GUI development.

One thing that PowerGUI, PowerShell Studio and PrimalScript all have in common, is that they support the MSSCCI provider/Source Control Plug-in API. The following guide shows you how to install the provider and configure PowerGUI to use it.


Installation Instructions

As there are many options for TFS, I will not go into installation and setup of TFS. For the examples, I will be using Team Foundation Services Online.

  1. Install the PowerGUI Script Editor.
  2. Accept the license agreements and install the defaults.
  3. Launch setup for Team Explorer for Visual Studio 2012.
  4. Agree to the license agreement and press the Install button.
  5. Press the ‘Launch’ button once installation has completed.
  6. Launch the Microsoft Visual Studio Team Foundation Server 2012 MSSCCI Provider. This is the 32-bit installer and is required for PowerGUI and other script editors.

Configure Team Foundation Server Explorer

  1. Press the ‘Connect to Team Foundation Server’ and press the ‘servers’ button.
  2. Press ‘Add…’ and enter details of the Team Foundation Server. Press OK. (you may be prompted to sign in)
  3. Press the close button.
  4. Select the Team Projects you wish to import. Press ‘Connect’.
  5. Click the Source Control Explorer link.
  6. Right click and choose ‘Add Items to Folder’. Choose a PowerShell Script to upload.

Configure PowerGUI to use Team Foundation Server

  1. Launch PowerGUI Script Editor.
  2. Select the ‘Tools’ menu -> ‘Options’.
  3. Select ‘Version Control’. Select ‘Team Foundation Server MSSCCI Provider’. Click Advanced.
  4. Select Check In and press OK, and OK again.
  5. PowerGUI will now have a ‘Version Control’ menu.
  6. Choose ‘Get Files from Version Control’.
  7. The Team Foundation Server should be available in the list. Click OK.
  8. Expand $/ and select the project beneath.
  9. Click the ‘Browse’ button. Create a new folder for the local copy of the script repository.
  10. Press OK.
  11. After a few moments (depending on speed of connection to the TFS server) an Open dialog will be shown.
  12. Open the sample script.

Using Team Foundation Server with PowerGUI

  • Creating a new script in the folder linked with the repository will cause it to be uploaded to TFS.
  • Modifying a script checks it out.

Find All Old Files The PowerShell Way

Last week, a colleague asked for help to find files older than a certain date. For this, it appears that Symantec NetBackup has a bug where if it is performing a backup with files that are created with a date older than 01/01/1980, NetBackup performs a full system backup instead of differential. Symantec have knowledge article TECH23000 (opens in new window) about it ‘STATUS CODE 0: INCREMENTAL backups run as a FULL backup despite the last FULL having backed up all files with Status 0. The bpbkar log reports “folder <folder_name> has been created recently (since 11/9/02 11:35:32 AM). It will be backed up in full.”’

The article discusses methods of resolving this behaviour- that include performing the following command “C:\dir *.* /S /TC >C:\creation.txt” (which is incorrect and should be dir “c:\*.* /S /TC >C:\creation.txt“) but does not give any suggestion as to how to find the offending items.

Being on a Windows 2008 server, I thought that as this server is running Windows Server 2008, PowerShell is installed and would provide the easiest way. Here is the script that I cam up with:

$OldestDate = Get-Date "29/02/2012" # Set the oldest date. Enter it in the local date/time format.
$Files = @() # Define $Files variable as an empty array.
$Drives = Get-WmiObject Win32_Volume | Where-Object {($_.DriveType -eq 3) -and ($_.Label -ne "System Reserved")} # Get a list of volumes from wmi, and find drivetype 3 (local disk) that do not have the volume label "System Reserved"
foreach ($Drive in $Drives)
 Get-ChildItem -Path ($Drive.DriveLetter + "\") -Recurse | foreach {if ($_.CreationTime -le $OldestDate) {$Files += $_}
} # get each file, test if it's creation time isolder than $OldestDate and add to $files}
$Files | Format-Table FullName, CreationTime #Format $Files as a table for display. Could also Export-CSV or the like.

PowerShell Script to Download SysInternals Suite Files

February 28, 2012 Leave a comment

Below is a very simple PowerShell script to copy any new or newer versions of the SysInternals tools:

$SysInternals=Get-ChildItem \\\tools\*-Exclude *.txt, *.scr
foreach ($Filein$SysInternals) {
    if (Test-Path$File.Name) {
        if ($File.LastWriteTime-ne (get-Item$File.Name).LastWriteTime) {
            Write-Host$File.Nameis out of date. Downloading new version…   
#end If LastWriteTime
            else {
               Write-Host$File.Nameis up to date.
#end If LastWriteTime
        } #end Test-Path
    else {
        Write-Host$File.Nameis new. Downloading…
#end else Test-Path
} #end foreach $file

Excel Split Rows Where Columns Different

December 9, 2010 Leave a comment

I caught a colleague of mine working through a CSV document in  Excel, he was putting blank rows in to separate data by a column, to make it easy for him to analyse the text, he was trying to achieve the results bellow:


1 Server Name Event ID Description
2 Server1 12 Details
3 Server1 12 Details
4 Server1 1 Details
6 Server2 12 Details
7 Server2 12 Details
9 Server3 12 Details

My colleague was manually putting the extra rows in, as row 5 and row 8. Problem was there was about 18,000 rows, over 200 servers. So, I wrote him a short script to do the sorting.

A few days later, he asked me for help modifying the script to select the new column, and to start on a different row. Instead of doing this every time, I modified the script to do it starting on the highlighted cell. So, in the example above click cell A2, and run the script below.

I hope you find it useful:

   1:  Sub InsertLine()
   2:      Dim i As Long
   3:      PreviousLine = ActiveCell.Row
   4:      CurrentLine  = PreviousLine + 1
   5:      ColumnNumber = ActiveCell.Column
   6:      While CurrentLine < Rows.Count
   7:          If Cells(CurrentLine, ColumnNumber) <> Cells(PreviousLine, ColumnNumber) Then
   8:              Rows(CurrentLine).Insert
   9:              CurrentLine = CurrentLine + 1
  10:          End If
  11:          PreviousLine = CurrentLine
  12:          CurrentLine  = CurrentLine + 1
  13:      Wend
  14:  End Sub