This confused me for a minute. But once I realized what was going on I felt like a dolt. $Query = $UriBuilder.Query if ($Query.Length > 1) { $Query = $Query.Substring(1) + ‘&’ } $UriBuilder.Query = $Query + $KeyValuePair Access to the path ‘C:\Windows\system32\1′ is denied. At C:\Blah\Blah.psm1:366 char:32 + if ($Query.Length > <<<< 1) { [...]
Posts tagged PowerShell
Tip for adding Silverlight references in Blend
I find it very frustrating sometimes to get something to build in Blend. Like many developers, I have a "Me.dll" that contains a lot of commonly used classes, custom controls, etc. As you might expect, this DLL often takes dependencies on other DLL’s that must also then be referenced. In Blend, this can be a [...]
PowerShell Team announces FxCop rules for cmdlets
PowerShell Team has announced FxCop rules for cmdlet authors. List of rules AcceptForceParameterWhenCallingShouldContinue AllCmdletsShouldAcceptPipelineInput CallShouldProcessOnlyIfDeclaringSupport DefineCmdletInTheCorrectNamespace DoNotAccessPipelineParametersOutsideProcessRecord DoNotCallCertainHostMethods DoNotUseConsoleApi FollowCmdletClassNamingConvention OverrideProcessRecordIfAcceptingPipelineInput ParameterShouldHaveConsistentTypePerNoun UseCredentialAttributeForPSCredentialParameter UseOnlyApprovedCharactersInVerbsAndNouns UseOnlyStandardVerbs UsePascalCasingInVerbsAndNouns UseRecommendedParameterTypes UseSingularNouns UseSingularParameterNames UseSwitchParameterInsteadOfBoolean
Dock TweetDeck to the side of your screen with PowerShell
I wanted to post this last night but I did not have an internet connection. The best part about being a developer is that when software drives you nuts, many times you can do something about it that mere mortals cannot. That’s the case with TweetDeck. I love it but I am sick of having [...]
Windows 7 PowerShell Tip
If you’re a system administrator (or like many developers), chances are you use PowerShell a lot and have the PowerShell console or PowerShell ISE on your Windows 7 taskbar. On Windows Vista and on Windows Server 2008 prior to R2 I was annoyed by having both ISE and console on the quick launch bar or [...]
LINQ for PowerShell
Download LINQ.psm1 I’m gonna start off by saying the module I’m posting about does not follow the PowerShell naming guidelines. In fact, not only does it use non-standard verb names, but the "verb" part of all the function names aren’t even verbs. I hate breaking the rules like this but the Verb-Noun naming couldn’t really [...]
Useful PowerShell Function: Use-Location
It’s a simple one, but useful. And it can clean up some of your ugly scripts that have a lot of redundant paths embedded in strings. # Reduced for the sake of the example # The full advanced function is included at the end. function Use-Location($Path, $Body) { Push-Location $Path try { &$Body } finally [...]
Checking a PowerShell script for external dependencies
I’m pretty excited about this new module. Originally I threw something very crappy together just to get a rough idea of which commands I was calling. But then I started polishing it up and polishing it up and I arrived at something I just had to post.
I will post back with some more information later, but the basic idea is that this uses the new PSParser class to parse a PowerShell script into tokens. It then analyzes the tokens to figure out some basic facts about the script:
Which global variables are being referenced
Which modules does the script import that are not currently imported
Which commands are being referenced by the script?
Which commands are functions defined in the current script?
Which command are aliases and which commands do they resolve to?
Which commands are built-in functions/cmdlets/aliases/etc?
Once all of the above has been determined, it’s presented in a format that is easy to read.
I added mine to the custom menu of PowerShell ISE.
Until PowerShell ISE gets PowerGUI’s “Open Profile” feature…
I hate to admit it, but the convenience of having PowerShell ISE installed with the core installation of PowerShell 2.0 has been enough to get me to switch from PowerGUI, which is in my opinion a much more full featured free editor/debugger.
One thing that I sorely missed from PowerGUI (which is compounded by the fact that unless you’re on Windows 7, PowerShell ISE doesn’t have a “Recent Files” menu) is the ability to quickly open my profile script without having to browse for it.
Well PowerShell ISE is extensible, so if it lacks a feature, you can just add it in most cases. I’ve uploaded my PowerShell ISE profile which has some useful functions for automating the ISE. But the one in particular that implements the “Open Profile” feature is called….(wait for it)…. Open-Profile!
So you can type that in the immediate window, or use the custom menu item that gets added. It opens the user/global/host/generic/etc profiles if they exist.
function Open-File([String]$Path,[Switch]$Quiet) { $AllowedExtensions = @(‘.ps1′,’.psm1′,’.psd1′,’.ps1xml’,’.txt’,’.txt’) $Extension = [System.IO.Path]::GetExtension($Path) try { if ( -not ($AllowedExtensions -contains $Extension) ) { throw “OpenFile: $Path cannot be opened in PowerShell ISE.” } if ( -not (Test-Path $Path) ) { throw “OpenFile: $Path does not exist.” } $Path = (Resolve-Path $Path).ProviderPath $PSISE.CurrentOpenedRunspace.OpenedFiles.Add($Path) } catch { if ( -not $Quiet ) { Write-Warning $_ } } } function Open-Profile { Open-File $PROFILE.CurrentUserAllHosts -Quiet Open-File $PROFILE.CurrentUserCurrentHost -Quiet Open-File $PROFILE.AllUsersAllHosts -Quiet Open-File $PROFILE.AllUsersCurrentHost -Quiet }
A PowerShell Prompt with a Purpose
Everybody has their own idea of what the perfect prompt in PowerShell consists of. My prompt is pretty boring, but it’s got one pretty unique feature – my prompt function doubles as a useful command!
You see, a lot of times I wind up in deep directories of solution and project folders in Visual Studio and I wind up with a prompt that barely fits on one line, or even worse, wraps. In this cases, I often only care about having a vague idea of where I am.
# sometimes I only want a little information about where I am. C:\Users\Josh Einstein\Documents\Visual Studio\Projects> Prompt -Minimize # sometimes I don’t care at all PS Projects> Prompt -Hide # if I need to bring it back to the way it was, I can just do this. PS> Prompt -Show C:\Users\Josh Einstein\Documents\Visual Studio\Projects>
This has really come in handy for me and it’s something you could easily do with a separate script or function in your profile. But I figured, hey the prompt function is already there right? What’s it gonna hurt if I add a few parameters to it?
############################################################################## #.SYNOPSIS # The all-important Prompt function called by PowerShell in order to display # the prompt, with an added twist using the built-in parameters. # #.DESCRIPTION # Customizing the prompt is common in PowerShell, but this function adds some # parameters to the function that enable some dynamic behavior. These # parameters will not affect the way PowerShell calls the function, but they # allow you to minimize or hide the prompt by passing switches. # #.PARAMETER Minimize # Shrinks the prompt so that it shows only the leaf portion of the current # provider path. Restore it using the -Show parameter. # #.PARAMETER Hide # Sets the prompt to an ultra-compact mode, displaying only an indicator, # and no information about the current location. # #.PARAMETER Show # Restores the prompt to it’s default mode which displays the full location # of the current provider path. # #.LINK # about_prompts # #.EXAMPLE # Prompt -Minimize # Prompt -Hide # Prompt -Show ############################################################################## function Prompt { [CmdletBinding(DefaultParameterSetName='Prompt')] param ( [Parameter(ParameterSetName='Minimize')] [Switch] $Minimize, [Parameter(ParameterSetName='Hide')] [Switch] $Hide, [Parameter(ParameterSetName='Show')] [Switch] $Show ) switch ( $PSCmdlet.ParameterSetName ) { Minimize { $Global:PromptVisibility = ‘Minimized’ } Hide { $Global:PromptVisibility = ‘Hidden’ } Show { $Global:PromptVisibility = ‘Default’ } Prompt { switch ( $PromptVisibility ) { Minimized { “PS $(Split-Path $PWD -Leaf)> ” } Hidden { “PS> ” } Default { “$PWD> ” } } } } }
Posts