How Do You Ensure That a PowerShell Pop-Up Window (mini-GUI) Always Appears in the Front?

Problem scenario:
You have a PowerShell script that launches a pop-up window.  This pop-up appears behind the PowerShell ISE GUI.  Without being in the foreground, the users of this script fail to realize there is a prompt waiting for the user to take action.  You want the pop-up window to be in the front.  You tried things like these options to no avail:

  • New-Object System.Windows.Forms.Form -Property @{TopMost = $true}
  • -ArgumentList([System.Diagnostics.Process]::GetCurrentProcess().MainWindowHandle)
  • form.TopMost = “True”
  • form.TopMost = $true
  • form.TopMost = $True
  • $form.TopLevel = $true
  • set-foregroundWindow(Get-Process PowerShell).Main.WindowHandle
  • $form.BringToFront()
  • set-WindowsFocus
  • [void]$showWindowAsync::ShowWindowAsync((Get-Process -id $pid).MainWindowHandle,

How Do You Print One Attribute (or Element) From The Results of a Built-in PowerShell Command?

Problem Scenario
You are writing a PowerShell script.  You want to extract one value from the results of a command.  You get a set of values all on one line.  It is difficult to parse just what you need. The output values for each attribute are separated by spaces.  Sometimes the output values are blank.  You want to reliably extract one property value from the command. You know that piping the output to an “sls” command will retrieve an entire row. 

Puppet Needs To Use a Windows Network File Share: How Is This Done Consistently?

Problem Scenario:  You are using Puppet Master on Linux and Puppet Agent on Windows servers.  The manifest attempts to copy to or from a Windows network file share, but you are getting an error about the network path not existing.  The network file share has permissions that are open to Everyone.  What do you do?

Solution:  Puppet Agent will run PowerShell commands under the security context of the user who runs the “puppet agent -t -d” command. 

How Do You Install IIS In An Automated Way Without The Install-WindowsFeature Command?

Problem scenario:  You want to install IIS on Windows Server 2016 in an automated way.  But you are prevented from using Install-WindowsFeature or Add-WindowsFeature commands for business related reasons.
Normally something like this would work: 
Import-module servermanager; Add-WindowsFeature web-server, web-webserver -Verbose
What do you do?

Solution:  Use PowerShell with the System.Windows.Forms.SendKeys feature.

servermanager
start-sleep 2
[System.Windows.Forms.SendKeys]::SendWait{%”M”}
[System.Windows.Forms.SendKeys]::SendWait{%”M”}
start-sleep 1
[System.Windows.Forms.SendKeys]::SendWait(“{DOWN}”)
[System.Windows.Forms.SendKeys]::SendWait({~})
[System.Windows.Forms.SendKeys]::SendWait({nnn})
start-sleep 1
[System.Windows.Forms.SendKeys]::SendWait({w})
[System.Windows.Forms.SendKeys]::SendWait{~~}
start-sleep 1
[System.Windows.Forms.SendKeys]::SendWait(“%{n}”)
[System.Windows.Forms.SendKeys]::SendWait(“%{n}”)
[System.Windows.Forms.SendKeys]::SendWait(“%{n}”)
[System.Windows.Forms.SendKeys]::SendWait(“%{n}”)
[System.Windows.Forms.SendKeys]::SendWait(“%{i}”)

How do you update the hosts file on a Windows server with PowerShell?

Problem scenario:  Whenever you use PowerShell to append to the c:\windows\system32\drivers\etc\hosts file you get spaces before and after every character of every string.  The problem does not happen with ordinary text files.

To illustrate, here is a line of PowerShell code and take not of the destination flat file involved:
echo “8.8.8.8        continualintegration.com” >> c:\windows\system32\drivers\etc\hosts

The above code results in a line like this inside the file:
8 .

How Do You Automate a Process That Involve a GUI Operation with Powershell?

Question:  How Do You Automate a Process That Involve a GUI (e.g., to save an .xps or .oxps file) Operation with Powershell?  You do not want any part to be interactive.

Solution:  This applies to PowerShell version 4 and above.  This will save the content of c:\temp\contint.txt to goodName.xps in the default folder (often the user’s Documents folder) without needing to attend to the process.

# These three lines (starting with “Add…”,

How Do You Use Conditional Logic on PowerShell Variables To Test If They Are Empty When The Variable’s Value Can Span Two Or More Lines?

Problem scenario:  Sometimes variables get values you do not expect.  Handling these exceptions is the mark of a good programmer.  

A PowerShell variable (e.g., $contint) may have the following content:

second line
third line”

When a variable’s value can span lines, conditional tests act unpredictably when they use such a variable.  For example a conditional test of the variable like this 

In PowerShell, how do you convert an array value into an integer value?

Question:  In PowerShell, how do you convert an array value into an integer value?

Answer:  Assuming that the value is an integer (e.g., not value that is legal for an array but not an integer), do the following:

1.  Write the array value to a file.
2.  Use the key word trim to eliminate blank space or other characters that would not be supported by the integer data type.

Troubleshooting Ansible With Windows Managed Nodes

Problem scenario
When running various Ansible operations, such as “ping,” result in an error like this “SSL: 500 WinRM Transport. [Error 104] Connection reset by peer.”  What should you do to get Ansible to work with Windows managed nodes?

Solution

Prerequisites
Install pip. If you need assistance, see this posting.

Procedures
Upgrade pywinrm to 0.2.2. 

How To Resolve “Certificate_Verify_Failed” error when trying an AWS CLI command?

Problem Scenario  You try to use an AWS CLI command, but you get an error.  The error says “[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:…)”

Possible solutions

#1  Redo the AWS configuration.  Issue this command: aws configure
Leave the AWS Access Key ID and the AWS Secret Access Key the way they are.  Look at the “Default region name.”  Verify there is no letter at the end.