Archive | November, 2011

PowerShell: Return, ForEach,ForEach-Object and a pipe

I just figured this out late last night. I was pulling my hair out. If you look at the following PowerShell code

($MyArray=1,2,3,4,5) | foreach-object {
write-host $_
if ($_ -eq 3 ){return}

You get a result of:

Next, if you look at this code (the only real difference being the lack of a pipe.)

foreach ($item in ($MyArray=1,2,3,4,5)) {
write-host $item
if ($item -eq 3 ){return}

You get:

That stumped me. Why would they not produce the same thing? Then I ran across this thread. The replier said

I think because ForEach-Object processes each item in the pipeline, but the foreach statement processes the collection as a whole.

This led me to this page, and specifically this quote clears it up for me (kinda):

 . . . [the object] it is sent into the pipeline and execution continues in the next section of the pipeline.  . . . the foreach alias gets executed and the object is run through the process script block of ForEach-Object.  Once the process script block completes, the object is discarded and the next object is [sent] . . .

In summary, last night I learned that when using a pipeline, the first item is passed to the script block, the script block is run, and then the next item is passed to the script block, and so on.

That sound correct? Can you come up with a better description?

PowerShell code to split a space delimited string – with double spaces

I am working on a PowerShell wrapper script to run multiple commands on multiple machines. The first thing I wanted to do was to chop up a space or comma delimited argument. (If you make an argument mandatory, and the user does not provide it, PowerShell will prompt you for it – and when prompted you can use space delimited values).

I was going to use the split() function, but I did not know how to handle “multiple spaces”. For example:

("123  4 5678").Split() = 


I would end up with “empty” array members. I found this post, and they guy used “| where-object {$_ -ne ”“}” to filter out the empty values. But then I stumbled upon a better way to do it, using the built in “StringSplitOptions” of  RemoveEmptyEntries.

("123  4 5678").split(" ",[StringSplitOptions]'RemoveEmptyEntries')

would result in:


If you don’t know if you are going to have space or comma delimited then I used:

$Argument.split(" ",[StringSplitOptions]'RemoveEmptyEntries') | foreach {

PowerShell wrapper scripts to find locked accounts and prompt to unlock

I wanted a quick way to find if an account is locked out (you get the call “I can’t log in”) and unlock it. I had a wrapper script that just called “Search-ADAccount –LockedOut” but I took it a bit further. The first of these two wrapper scripts/functions gets all the accounts that are  locked out, then it asks if you want to unlock the account – if yes, it calls the second unlock function. If no, then it loops to the next locked account.

function JBMURPHY-AD-GetLockedOut {
	Search-ADAccount –LockedOut | foreach-object {
	write-host "`n$UserName is locked out`n"
	$message = "Do you want to unlock $UserName"
	$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Yes nnlock $UserName"
	$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","No, don't unlock $UserName"
	$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
	$result = $host.ui.PromptForChoice($title, $message, $options, 0)
	if ($result -eq 0){
		JBMURPHY-AD-UnlockAccount -UserName $SamAccountName

The second function is a simple script that wraps the unlock-adaccount function:

function JBMURPHY-AD-UnlockAccount {
	Param([parameter(Mandatory = $true)]$UserName)
	Unlock-ADAccount -Identity $UserName

Start Visual Studio form PowerShell

I am moving on to a new project – our migration from SharePoint 2007 to 2010. First thing I wanted to do was to upgrade my Solutions/Features from 2007 to 2010. I installed Visual Studio and started looking at how to recreate my Delegate JQuery Control (more on that later). The first thing I found annoying was that every time I added an “Empty SharePoint Project”, I received a warning “This task requires the application to have elevated permissions”. That is annoying. Since I usually leave a PowerShell prompt running as administrator (It has a red background to distinguish it), I wanted to quickly open VisualStudio from that PowerShell prompt. Here is my wrapper script to open VisualStudio 2010 from PowerShell:

function JBMURPHY-Start-VisualStudio {
Start-Process "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" -workingdirectory "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\"

Deploy Visual Studio 2010 vai SCCM (silently)

I wanted to have Visual Studio 2010 on my machine. My motto is that if I have to install it, I should package it up in SCCM, so when I re-image, I can just press a button. Here were my steps. Nothing ground breaking in this post, but I wanted to document it for my self (and anyone else that could use it). Also, I am only documenting the steps that I feel are important, so maybe this will be a quick reference for someone.

First I needed to create an answer file: D:\setup\setup.exe /createunattend c:\VS2010_deployment.ini This will walk you through the install and save the settings to an ini file.

Second I added Visual Studio 2010 to SCCM via “new Package from definition” and changed the command line to Setup\setup.exe /UnattendFile \\SCCMServer\smspkgc$\PackageID\Setup\VS2010_deployment.ini

Third I changed the advertisement to “Run program from distribution point”

That was about it. Pretty easy. on my iPhone crashes, and Mac does not see any photos on my iPhone (iOS5)

Crap!!!! I had close to 400 pictures on my iPhone. I back it up all the time, but I had not “processed them” (my workflow is to use Image to pull photos off of the iPhone, and into a folder, that I then import into iPhoto. I zip up (compress) the original folder containing the originals.)

Every time I opened the Photos app on my iPhone, it crashes. Every time, I look at previous photos from the, I see a lot of “Black photos”. Something was screwed up. I figured I needed to import the photos into my workflow and start fresh.

I fired up “Image” to pull the photos in, and it saw nothing. I started to get nervous, because I had Halloween photos on there.

All my photos were showing up in “Photo Stream”, but I wanted the originals (does Photo Stream copy the originals? Full quality? I did not know).

My Solution

I figured that since I backed up my iPhone in iTunes all the time, there should be a way to extract the photos from a backup.

If you have a valid backup that has your photos in it, and you have not encrypted your backups, then use iPhone Backup to restore the photos. Fire up the app, select read backups, and then select iOS files. The restore will put all your photos in “iOS Files/Media/DCIM/100APPLE/”

Hope that helps someone.

Error: Request for the permission of type System.Web.AspNetHostingPermission failed

Every so often I have to go into some historical code to try and see what is happening. And every time I make the same mistake, I try to debug the code off my mapped user drive (I do everything in my user drive which is on a network share. And every time, I get the same error:

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Web.AspNetHostingPermission, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Note to self, don’t debug from a local drive!

Powered by WordPress. Designed by WooThemes