# LeoPonti Blog

Posts
• #### PowerTip: Use PowerShell to Create Hash Table

Summary : Use a Windows PowerShell cmdlet to create a hash table. How can I use Windows PowerShell to create a hash table if do not remember the special syntax? Use the ConvertFrom-StringData cmdlet, and put each key-value pair on its own line. (You can perform this on a single line by using backtick character plus n ( n ) for a new line): PS C:\> convertfrom-stringdata "a=1nb=2`nc=3" Name Value ---- ----- c 3 a 1 b 2
• #### Phantom Elements in a Hash Table

Summary : Microsoft PFE and guest blogger, Chris Wu, talks about working with hash tables. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Chris Wu … Admittedly, hash tables didn’t catch my attention when I started using Windows PowerShell, but over time it has proven to be one of my favorites. This is largely thanks to its ability to assign names to values for fast lookup. Hash tables store key-value (or name-value) pairs, which means typically we use strings as keys to locate corresponding values. Windows PowerShell also takes keys from the command line as strings. So the following examples will create the same hash table: However, it is not a restriction, per se. Any type of data can be used as a key in hash tables. The following example uses int values as keys, which makes a hash table appears like an array, in terms of syntax: Other than indexing, hash tables also support dot notation to access elements. However, in Windows PowerShell 2.0, dot notation syntax doesn’t support non-string keys. This seems to have been improved in Windows PowerShell 3.0. Actually, as long as a hash table is manually crafted, there is no mystery. Everything was plain and straightforward to me, until I was hit by this particular issue where I was trying to get a list of fixed logical disks by accessing a hash table that returned from the Group-Object cmdlet with the -AsHashTable parameter: What? No fixed disks? That’s impossible! So I double checked: A key called 3 was indeed in the list. The workaround I tried previously also proved that the element was in the hash table. So what the heck? I scratched my head hard, until finally I realized that those keys are objects too. So each key has its type as well. Then I investigated: Here we go… The keys were of the unsigned Int32 type, which are different from the signed Int32 type that Windows PowerShell uses by default. So when Windows PowerShell searched the hash table, looking for an element for Int32 value 3, there was no match! When I figured out that, it became apparent how the elements can be accessed: One take-away lesson that I learned: Keys in a hash table are unique only when the data and the type are considered. I could create a hash table that contain four keys, all sharing the same look: Now, how do we get each of those elements? ~Chris Thanks, Chris, for another awesome blog post. I invite...
• #### PowerTip: Display a Blinking Message by Using PowerShell

Summary : Use this one-line Windows PowerShell command to display a blinking message. Is there a one-line command that I could use to blink a message in the Windows PowerShell console to get the users attention? Pipe a range of numbers to the Foreach-Object cmdlet ( % is an alias), clear the screen in the Begin block, and then display the message, pause, clear the screen, and pause in the Process block: 1..3 | % -begin {cls} -process {"hello";sleep 1;cls;sleep 1}
• #### PowerShell Report for a Windows Failover Cluster

Summary : Use Windows PowerShell to create a report for a Windows failover cluster. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Rhys Campbell … I’m involved in the administration of several Failover Clusters , and I wanted to be able to easily report on these and get an alert for any changes in status. Enter Windows PowerShell. This Windows PowerShell script uses various FailoverCluster cmdlets to write information about a Windows Failover Cluster to a HTML file and copy this to a web server directory root. The script also tracks changes to the state of the cluster and sends an email notification with the details. The following steps outline the process to set up this script. 1. Open clstr.ps. We need to change the SMTP details here. Change the following line… sendEmail "smtp.domain.co.uk" 8025 $subject$report $transcript "to@domain.co.uk" "from@domain.co.uk"; –to– sendEmail "your.smtp.server" 25$subject $report$transcript "who.this.goes.to@domain.co.uk" "powershell@domain.co.uk"; Save and close the file. 2. Create a folder somewhere that is appropriate for the Windows PowerShell script and other resources, for example c:\tmp . This should be fully writable for the user who will execute the .ps1 file. Copy clstr.ps1 to the folder. Create a folder called Resources in the same directory and copy the style.css folder here (customize it if you desire). You may need to sign the script depending on your preferred execution policy. (For more information, see Using the Set-ExecutionPolicy Cmdlet .) 3. Now we are ready to test the execution of the script. The script is executed with three parameters inside a Windows PowerShell console: C:\tmp\clstr.ps1 <cluster name> c:\tmp\ c:\path\to\web\server\root\ Note The cluster name refers to the Windows Failover Cluster name, not the SQL instance name. Parameter 2 should be the script directory root. Parameter 3 should be the root of your web server HTML directory. Notice the trailing slashes. If you execute this a couple of times, the script root folder should look something like this: The script uses the text file to keep track of the cluster state and what has changed. If you have multiple clusters, you can set this up for each one. In this situation, I recommend setting up a directory structure similar to this: C:\tmp\ClusterName1 C:\tmp\ClusterName2 and so on... 4. After your setup...
• #### PowerTip: Add Computer to Security Group with PowerShell

Summary : Use Active Directory PoweShell cmdlets to add a computer to a security group. How can I use Windows PowerShell to add a computer to a security group? Use the Add-ADGroupMember cmdlet, and remember to use the SAM account name on the computer: To add a computer called “STATION01” to a security group called “RDPEnabled”: ADD-ADGroupMember “RDPEnabled” –members “STATION01$” Note The SAM account name has a “$ ” added to its name.

• #### PowerTip: Show Group Members with PowerShell

Summary : Use Windows PowerShell to get a list of group members in Active Directory. How can I use Windows PowerShell to regularly show who is in an Active Directory group, for example, Domain Admins? To show who is in the Domain Admins group in Active Directory, run the following command: Get-ADGroupMember DomainAdmins If you’d like to view this in a nicer format, use: Get-ADGroupMember DomainAdmins | FORMAT-Table

• #### PowerTip: Debug a Workflow with PowerShell 4.0

Summary : Use Windows PowerShell 4.0 to debug a workflow. How do I set a breakpoint in a Windows PowerShell script workflow? Add the workflow to a script and use Set-PSBreakpoint to set a line breakpoint in the workflow: Set-PSBreakpoint -Script C:\Test-MyWorkflow1.ps1 -Line 12 ID Script Line Command Variable Action -- ------ ---- ------- -------- ------ 0 WFExample1.ps1 12

• #### PowerTip: Use PowerShell to Discover Console Colors

Summary : Use Windows PowerShell to discover console color assignment. How can I see what colors are assigned in the Windows PowerShell console? Use the Get-Host cmdlet and expand the PrivateData property: (get-host).privatedata
• #### Updates: Coreinfo v3.21, Disk2vhd v2.0, LiveKd v5.31

Coreinfo v3.21 : CoreInfo is a command-line tool for reporting processor topology, NUMA performance, and processor features. The v3.21 release adds microcode reporting. Disk2vhd v2.0 : Disk2vhd, a utility for performing physical-to-virtual conversion of Windows systems, adds support for VHDX-formatted VHDs (thanks to Brendan Gruber for contributions), now supports WinRE volumes, can capture removable media, and includes an option to capture live volumes instead of relying on volume shadow copy (VSS). LiveKd v5.31 : LiveKd is a utility for performing live kernel debugging of native systems and virtual machines from the host operating system. This release fixes a debugger help library search bug and fixes a bug in Windows 8/Windows Server 2012 mirror dump support.