Powershell Multi-Line Input Box Dialog, Open File Dialog, Folder Browser Dialog, Input Box, and Message Box

Powershell Multi-Line Input Box Dialog, Open File Dialog, Folder Browser Dialog, Input Box, and Message Box

http://blog.danskingdom.com/powershell-multi-line-input-box-dialog-open-file-dialog-folder-browser-dialog-input-box-and-message-box/
Advertenties

Keep PowerShell console open when script is ran from File Explorer

Keep PowerShell console open when script is ran from File Explorer

When running a script by double-clicking it, if the script completes very quickly the user will see the PowerShell console appear very briefly and then disappear.  If the script gives output that the user wants to see, or if it throws an error, the user won’t have time to read the text.  The typical work around is to open the PowerShell console and manually run the script.  The other option is to adjust our new registry key value a bit.

So to keep the PowerShell console window open after the script completes, we just need to change our new key value to use the –NoExit switch:

“C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” –NoExit “& \”%1\””

And here is the .reg script with the –NoExit switch included:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit \"& \\\"%1\\\"\""

Using Select-Xml With XmlNamespaces

Using Select-Xml With XmlNamespaces

#  Select-XML is a nifty cmdlet included in PowerShell V2,             
#   and it lets you perform xPath queries.            
#  It also makes a pair of scenarios that are             
#   otherwise a little tricky with xpath fairly simple.            
#  Read below, or check out this video             
#   that walks thru Select-XMl and namespaces            
#  Let's look at a quick example.             
#  Here's a sample service definition for Azure:            

$xml=[xml]@"<?xml version="1.0" encoding="utf-16"?>
<ServiceDefinition name="foobar" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole1" enableNativeCodeExecution="true">
    <Sites>
      <Site name="Web1">
        <Bindings>
          <Binding endpointName="DefaultWebSiteEndPoint" name="Binding1" hostHeader="foo.bar.com" />
        </Bindings>
      </Site>
      <Site name="Web2">
        <Bindings>
          <Binding endpointName="DefaultWebSiteEndPoint" name="Binding1" hostHeader="bar.baz.com" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="DefaultWebSiteEndPoint" protocol="http" port="80" />
    </Endpoints>
  </WebRole>
</ServiceDefinition>
"@            

#  If I want to find all of the Bindings in this XML with xPath,
#   .NET has some surprises in store.
#  That is: // in Xpath grabs a node at any depth,             
#   but only if you specify a namespace.            

$xml.SelectNodes("//Binding")            # results in nothing!            

#  The .NET code to deal with namespaces is pretty tedious,            
#   and you do not need to know it.            
#  Select-Xml can make it a lot easier with the -Namespace parameter.             
#  -Namespace is a hashtable, and you put a shortname             
#   for the way you want to find the XML.            
#  XMLNS is reserved, so I'll use "Azure"            
#  Using this, it's quick and easy to zoom right in             
#   on the piece of information we're after            

Select-Xml -Xml $xml -Namespace @{                        
    Azure='http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'                        
} -XPath //Azure:Binding |                        
    Select-Object -ExpandProperty Node |                        
    Select-Object -ExpandProperty HostHeader                            

# And That Works !                

Formatting of Numeric Strings

Formatting of Numeric Strings

The general format of a Format String is:

  • “{<argument index>[,<alignment>][:<formatString><zeros>]}”

e.g.:   {0,-8:G2}

argument index: mandatory This represent which argument goes into the string
alignment: optional This represent the minimal length of the string
Postive values, the string argument will be right justified
format string: optional This represent the format code.
C(urrency), D(ecimal), E(xponential), F(ixed), G(eneral), N(umber), P(ercent), X(adecimal)
zeros: optional It actually has a different meaning depending on which numeric specifier you use

A number of examples

“{0:C4}”, 10 $10.0000
“{0:D4}”, 10 0010
“{0:D4}”, -10 -0010
“{0:E4}”, 10 1.0000E+001
“{0:E4}”, -10 -1.0000E+001
“{0:F4}”, 10 10.0000
“{0:F4}”, -10 -10.0000
“{0:G4}”, 10 10
“{0:G4}”, -10 -10
“{0:N4}”, 10 10.0000
“{0:N4}”, -10 -10.0000
“{0:P4}”, 10 1,000.0000%
“{0:P4}”, -10 -1,000.0000%
“{0:X4}”, 10 000A
“{0:X4}”, -10 FFFFFFF6
“{0:4}”, 10 4
“{0:4}”, -10 -4

Get-GUIDate: A Powershell Calendar Tool

Get-GUIDate: A Powershell Calendar Tool

http://www.sapien.com/blog/2009/08/24/writing-form-centered-scripts-with-primalforms/

Mapping Cluster.exe Commands to PowerShell Cmdlets for Failover Clusters

Mapping Cluster.exe Commands to PowerShell Cmdlets for Failover Clusters

http://blogs.technet.com/b/josebda/archive/2010/09/24/mapping-cluster-exe-commands-to-windows-powershell-cmdlets-for-failover-clusters-extended-edition.aspx

Encoding a Powershell Command

# If you ever need to invoke a complex piece of code directly from CMD.exe,            
# You can run PowerShell and do the following to get the string to pass to -EncodedCommand :            
            
$Script = {gps |where {$_.handles -ge 900}|sort handles}.ToString()            
[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($script))            
            
# Output you could get:            
ZwBwAHMAIAB8AHcAaABlAHIAZQAgAHsAJABfAC4AaABhAG4AZABsAGUAcwAgAC0AZwBlACAAOQAwADAAfQB8AHMAbwByAHQAIABoAGEAbgBkAGwAZQBzAA==            
            
# You can then use that string from CMD.EXE:            
C:\>PowerShell -EncodedCommand ZwBwAHMAIAB8AHcAaABlAHIAZQAgAHsAJABfAC4AaABhAG4AZABsAGUAcwAgAC0AZwBlACAAOQAwADAAfQB8AHMAbwByAHQAIABoAGEAbgBkAGwAZQBzAA==            
             
# And get a processes top-3 as a result!