Wednesday, February 13, 2008

Code Snippet plugin for Windows Live Writer

Thank you Leo Vildosola! You just made me a very happy blogger :)

 

I found your plugin for Windows Live Writer and it works great with PowerShell scripts. Inserting well-formatted, color-codedPowerShell code is now as easy as selecting 'Insert Code Snippet', pasting the code and pressing ok. There are only two drawbacks -

  1. Is is not copy-friendly, as there are no line breaks. But my current method is not that either
  2. The formatting is called MSH and not PowerShell, but I think I can live with that ;)

 

Some examples

With line numbers and a container

 

   1: # Set-SMSCacheSize
   2: param([int]$newSizeInMB=2000)
   3:  
   4: $sms=new-object -com UIResource.UIResourceMgr
   5: $ci=$sms.GetCacheInfo()
   6: if ($ci.TotalSize -ne $newSizeInMB) {
   7:     $ci.TotalSize=$newSizeInMB
   8:     "Size set to $newSizeInMB"
   9: }
  10: else {
  11:     "Size $newSizeInMB already correct"
  12: }

 


Alternating lines, no container, no line numbers -



# Set-SMSCacheSize
param([int]$newSizeInMB=2000)
 
$sms=new-object -com UIResource.UIResourceMgr
$ci=$sms.GetCacheInfo()
if ($ci.TotalSize -ne $newSizeInMB) {
    $ci.TotalSize=$newSizeInMB
    "Size set to $newSizeInMB"
}
else {
    "Size $newSizeInMB already correct"
}

 


No line numbers, no container


# Set-SMSCacheSize
param([int]$newSizeInMB=2000)

$sms=new-object -com UIResource.UIResourceMgr
$ci=$sms.GetCacheInfo()
if ($ci.TotalSize -ne $newSizeInMB) {
$ci.TotalSize=$newSizeInMB
"Size set to $newSizeInMB"
}
else {
"Size $newSizeInMB already correct"
}

 


No line numbers, no container, alternating lines



# Set-SMSCacheSize
param([int]$newSizeInMB=2000)
 
$sms=new-object -com UIResource.UIResourceMgr
$ci=$sms.GetCacheInfo()
if ($ci.TotalSize -ne $newSizeInMB) {
    $ci.TotalSize=$newSizeInMB
    "Size set to $newSizeInMB"
}
else {
    "Size $newSizeInMB already correct"
}

 


No line numbers, container, alternating lines


 



# Set-SMSCacheSize
param([int]$newSizeInMB=2000)
 
$sms=new-object -com UIResource.UIResourceMgr
$ci=$sms.GetCacheInfo()
if ($ci.TotalSize -ne $newSizeInMB) {
    $ci.TotalSize=$newSizeInMB
    "Size set to $newSizeInMB"
}
else {
    "Size $newSizeInMB already correct"
}

test3

# Format SDDL strings in a more readable format
# If -TranslateSid is present, Sids will be translated to their account equivalent
param([switch]$translateSid)
process {
$sddl=$_
if ($sddl -is [string]) {
# Simply use the value
}
else {
# If input is any thing else, pick up SDDL property
# This makes it possible to pipe in the output from Get-Acl or Select
$sddl=$_.sddl
}
# Insert linefeed before G: D: or S: blocks
# Insert linefeed and indent values in ()
$sddl=$sddl -replace "([GDS]):","`n`$1:" -replace "(\([^\)]+\))","`n `$1"
if ($translateSid.isPresent) {
# Match all SIDs and translate them to NTAccount format
[regex]::Matches($sddl,"(S(-\d+){2,8})") | sort index -desc | % {
# Save value in case translatation fails
$name=$_.value
$sid=[system.security.principal.securityidentifier] $name
# Remove matched value
$sddl=$sddl.remove($_.index,$_.length)
# Translate, suppress non-translatable exception
trap [System.Management.Automation.MethodInvocationException] {continue} `
$name=$sid.Translate([system.security.principal.ntaccount])
# Insert translated name
$sddl=$sddl.insert($_.index,$name)
}
}
$sddl
}

 


 

# Format SDDL strings in a more readable format
# If -TranslateSid is present, Sids will be translated to their account equivalent
param([switch]$translateSid)
process {
$sddl=$_
if ($sddl -is [string]) {
# Simply use the value
}
else {
# If input is any thing else, pick up SDDL property
# This makes it possible to pipe in the output from Get-Acl or Select
$sddl=$_.sddl
}
# Insert linefeed before G: D: or S: blocks
# Insert linefeed and indent values in ()
$sddl=$sddl -replace "([GDS]):","`n`$1:" -replace "(\([^\)]+\))","`n `$1"
if ($translateSid.isPresent) {
# Match all SIDs and translate them to NTAccount format
[regex]::Matches($sddl,"(S(-\d+){2,8})") | sort index -desc | % {
# Save value in case translatation fails
$name=$_.value
$sid=[system.security.principal.securityidentifier] $name
# Remove matched value
$sddl=$sddl.remove($_.index,$_.length)
# Translate, suppress non-translatable exception
trap [System.Management.Automation.MethodInvocationException] {continue} `
$name=$sid.Translate([system.security.principal.ntaccount])
# Insert translated name
$sddl=$sddl.insert($_.index,$name)
}
}
$sddl
}


test2

# Format SDDL strings in a more readable format
# If -TranslateSid is present, Sids will be translated to their account equivalent
param([switch]$translateSid)
process {
$sddl=$_
if ($sddl -is [string]) {
# Simply use the value
}
else {
# If input is any thing else, pick up SDDL property
# This makes it possible to pipe in the output from Get-Acl or Select
$sddl=$_.sddl
}
# Insert linefeed before G: D: or S: blocks
# Insert linefeed and indent values in ()
$sddl=$sddl -replace "([GDS]):","`n`$1:" -replace "(\([^\)]+\))","`n `$1"
if ($translateSid.isPresent) {
# Match all SIDs and translate them to NTAccount format
[regex]::Matches($sddl,"(S(-\d+){2,8})") | sort index -desc | % {
# Save value in case translatation fails
$name=$_.value
$sid=[system.security.principal.securityidentifier] $name
# Remove matched value
$sddl=$sddl.remove($_.index,$_.length)
# Translate, suppress non-translatable exception
trap [System.Management.Automation.MethodInvocationException] {continue} `
$name=$sid.Translate([system.security.principal.ntaccount])
# Insert translated name
$sddl=$sddl.insert($_.index,$name)
}
}
$sddl
}



test ps

# Format SDDL strings in a more readable format
# If -TranslateSid is present, Sids will be translated to their account equivalent
param([switch]$translateSid)
process {
$sddl=$_
if ($sddl -is [string]) {
# Simply use the value
}
else {
# If input is any thing else, pick up SDDL property
# This makes it possible to pipe in the output from Get-Acl or Select
$sddl=$_.sddl
}
# Insert linefeed before G: D: or S: blocks
# Insert linefeed and indent values in ()
$sddl=$sddl -replace "([GDS]):","`n`$1:" -replace "(\([^\)]+\))","`n `$1"
if ($translateSid.isPresent) {
# Match all SIDs and translate them to NTAccount format
[regex]::Matches($sddl,"(S(-\d+){2,8})") | sort index -desc | % {
# Save value in case translatation fails
$name=$_.value
$sid=[system.security.principal.securityidentifier] $name
# Remove matched value
$sddl=$sddl.remove($_.index,$_.length)
# Translate, suppress non-translatable exception
trap [System.Management.Automation.MethodInvocationException] {continue} `
$name=$sid.Translate([system.security.principal.ntaccount])
# Insert translated name
$sddl=$sddl.insert($_.index,$name)
}
}
$sddl
}

 


 


# Format SDDL strings in a more readable format
# If -TranslateSid is present, Sids will be translated to their account equivalent
param([switch]$translateSid)
process {
$sddl=$_
if ($sddl -is [string]) {
# Simply use the value
}
else {
# If input is any thing else, pick up SDDL property
# This makes it possible to pipe in the output from Get-Acl or Select
$sddl=$_.sddl
}
# Insert linefeed before G: D: or S: blocks
# Insert linefeed and indent values in ()
$sddl=$sddl -replace "([GDS]):","`n`$1:" -replace "(\([^\)]+\))","`n `$1"
if ($translateSid.isPresent) {
# Match all SIDs and translate them to NTAccount format
[regex]::Matches($sddl,"(S(-\d+){2,8})") | sort index -desc | % {
# Save value in case translatation fails
$name=$_.value
$sid=[system.security.principal.securityidentifier] $name
# Remove matched value
$sddl=$sddl.remove($_.index,$_.length)
# Translate, suppress non-translatable exception
trap [System.Management.Automation.MethodInvocationException] {continue} `
$name=$sid.Translate([system.security.principal.ntaccount])
# Insert translated name
$sddl=$sddl.insert($_.index,$name)
}
}
$sddl
}

This page is powered by Blogger. Isn't yours?