Posted in Powershell

Visual Studio Code Snippets

I am trying to get comfortable with Visual Studio Code for working with PowerShell scripts. Admittedly I still revert back to the ISE to debug the code. Maybe Code will provide that integrated interface one day but we will see. Moving on, one thing I use a great deal of are snippets. Admittedly I was completely lost on how to implement snippets in this IDE, then I found a great article from Keith Hill. This post provides information as well as a great starter file.

Now I want to add my own Snippets. The process is easy enough and the JSON editing in Visual Studio Code is pretty slick. I then found myself struggling with how to create a snippet and using PowerShell syntax create a variable. It starts with a dollar sign, easy enough right? Not really. The dollar sign is part of the snippet language as well. Yes I read this doc but didn’t put one and one together I guess. After some trial and error I found that $$ would then create the PowerShell syntax I was looking to achieve.

https://gist.github.com/jkavanagh58/1e06ac75860340cb1c1399b7676f7ebb.js

image

So in a PowerShell script I start typing the prefix and Intellisense detects possible matches, one of them being my Snippet:

image

Then I click on the User Snippet reference and the result is…

image

Now I have to start converting my library of snippets and then play with the dollar sign operator of Visual Studio Code snippets.

Posted in CIM, Powershell, WMI

Delprof Alternative

So I wrote this ugly code quite some time ago. Since I have seen a few new posts in the script gallery with versions I figured I should at least post a version of what I had done a few years ago and re-used recently. Situations like shared computers and RDS can have a computer looking ragged with local profiles created on login and then not touched again. This causes capacity issues but can also cause performance issues. There used to be a utility called delprof which was great but even if it were around why not script it right?

Function clean-localprofiles {
    [CmdletBinding()]
    Param (
        [Parameter(Position = 0)]
        [ValidateNotNullorEmpty()]
        [int]$Days = 30,
        [string]$rptFile = "somefile name here"
    )
    BEGIN 
    {
        "---- Internal RDS Profile Monitor Start {0}" -f (get-date -f "MM/dd/yy hh:mm") | out-file $rptFile -Append
        Write-Warning "Filtering for user profiles older than $Days days"
        $profs = Get-CimInstance win32_userprofile -ComputerName computer1, computer2  |
        Where { $_.LastUseTime -lt $(Get-Date).Date.AddDays(- $days) -AND $_.SID.Length -gt 8 -AND $_.Loaded -eq $False }
    }
    PROCESS
    {
        ForEach ($obj in $profs) {
            $uname = $obj.LocalPath.Split("\") | select -Last 1
            "Removing profile for {0} from {1} which was last used {2}" -f $uname, $obj.pscomputername, $obj.LastUseTime |
            out-file $rptFile -Append
            Try {
                Remove-CimInstance -InputObject $obj -ComputerName $obj.pscomputername
            }
            Catch {
                "Unable to remove profile" | out-file $rptFile -Append
            }       
        }
        # Complete
    }
    END
    {
        "---- Internal RDS Profile Monitor Complete {0}" -f (get-date -f "MM/dd/yy hh:mm") |
            out-file $rptFile -Append
    }    
}
Clean-localprofiles

That’s about it. I updated the process from WMI to CIM (where possible) and tweaked based on changes to Powershell but relatively unchanged. Yeah checked my OneDrive, first script was dated in 2012…

Posted in Powershell

Powershell Here

Remember the Power Toys where you could add open prompt here? Did you know there is an even easier way?

Navigating the file system isn’t that hard in the Powershell CLI to start with considering tab completion and all of that but there is an even easier way….

In your file explorer window type powershell in the address bar. A new Powershell window will start and as the included video a simple get-location will show your working directory is same as the explorer window you launched powershell from. It may look different than what you expect when starting the Powershell CLI but you can change that with the window’s defaults settings.

 

A quick example (video) here.

Posted in Powershell

Stop loading iTunes

Yeah I am an iPhone user these days. No I don’t need to have the software installed but I installed it. Now when I plug my phone in, just to charge, stupid iTunes loaded. I have a ton of windows open pushing my laptop to the max so sometimes clicking on the x to close the window just doesn’t work… my new favorite one-liner is now…

(Get-Process -Name iTunes).Kill()

So of course I took this and added it to my profile.ps1

function kill-itunes
{
    [CmdletBinding()]
    param ()
    (Get-Process -Name itunes).Kill()
}
Posted in Powershell

Get-CloudManagementTools

Very handy

mukesh notes

This PowerShell script can be used to download some of the common cloud (Azure and AWS) management tools available on Windows Platform.

Earlier I had seen Download and Install SharePoint 2013 Prerequisites on Windows Server 2012.  However this technet script uses Start-BitsTransfer cmdlet from BitsTransfer module. I was toying with similar idea while working on my last workshop (Cloud Automation Using PowerShell) preparation. Since some of the URLs do redirect the Start-BitsTransfer wasn’t working for me initially.

I started with simple Invoke-WebRequest cmdlet and added second variation to handle redirected URLs and pass it to Start-BitsTransfer. So let’s go through the script and output for the both the approaches.

I am using Downloads directory to store the files. For demo I have created CloudTools folder inside. I already have AWS Tools for Windows PowerShell downloaded in the folder.

15012017-01

I am already into the working directory where the script is saved.

View original post 607 more words

Posted in CLI

Quick Hit – PowerCLI as a Module and Loading it up

PowerCLI is a great tool, has been since about day one of Powershell. As Powershell has progressed and PSSnapins where replaced with Modules, PowerCLI took a long time to make the jump to modules. While stiff functional it was a point of question. So with version 6 it is mostly modules which is great, however now I have to tweak my Powershell profiles. Much like other customized shells (Operations Manager, SharePoint, etc …), I hate the single purpose shells so I had to customize my profiles. So what I have put together is real simple, and gets me started loading it up:

(get-module -Name vm* -ListAvailable).Foreach{Import-Module $_.Name}