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...", "set...", and "start..." ensure the XPS Viewer is functional:
Add-WindowsFeature XPS-Viewer  #this ensures the server has XPS viewer as a printer driver
set-service spooler manual  #if the printer spooler is disabled, this will not work
start-service spooler       #if the printer spooler is not working, the automation will not work

# These next lines print a file:
start-process -FilePath C:\temp\contint.txt -Verb print
start-sleep 1 #wait one second for the application to open
[System.Windows.Forms.SendKeys]::SendWait({"goodName"})  # This input a name into the GUI
[System.Windows.Forms.SendKeys]::SendWait({'~'})  # This saves the file with issuing "Enter"

# To learn more about the PowerShell-to-keyboard mapping, use this link:
http://www.vexasoft.com/pages/send-keyboardinput
# It is possible to use SendKeys to tab up, change the path where the .xps file is saved, and tab down to finally save it.
# The above code is a simple example of something that may be useful.
# To save to a .pdf, try this link.

How Do I Know If I Have a Safe Version of NTP on a Linux Server?

Question  How do I know if I have a safe version of NTP on a Linux server?

Background  The United States Computer Emergency Readiness Team's website indicates that this many versions of NTP are susceptible to a denial-of-service attack.

Answer  Run this command:  ntpd --version

The output should look something like this:

ntpd 4.2.8p9

If it is lower than that (e.g., ntpd 4.2.8p8), then you need to upgrade your ntp version.

How To Install and Configure Salt Stack on RedHat Linux (AWS Instances)

Problem scenario
You want to install SaltStack on RHEL instances for the first time on an AWS server.

Solution in Three Parts
(If you want instructions on how to do this with Debian/Ubuntu and/or Linux SUSE, see this posting.)

Part 1  Create a Salt Master Server
1.  Create a RHEL instance and put it in a Security Group that has access to the Internet for this initial setup, and has connectivity to the other server that will be the Salt Minion.  The IP address you would use is the internal IP address of the server (from an ifconfig command) and not the IP address seen in the EC2 console (one that you would use Putty to connect to).

2.  Assume the root user (sudo su -)

3.  Run these commands:
yum -y install https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm
yum -y install salt-master

4.  Verify it worked:
salt-master --version

5. Start the salt-master service:
systemctl start salt-master

Part 2  Create a Salt Minion Server
6.  Create a RHEL instance and put it in a Security Group that has access to the Internet for this initial setup and has connectivity to the other server that will be the Salt Master.   The IP address you would use is the internal IP address of the server (from an ifconfig command) and not the IP address seen in the EC2 console (one that you would use Putty to connect to).

7.  Assume the root user (sudo su -)

8.  Run these commands:
yum -y install https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm
yum -y install salt-minion

9.a.  vi /etc/salt/minion
9.b.  Search for "master: salt"
9.c.  Uncomment this line.  Change "salt" to "saltmaster" or the hostname of the salt master server.

10.   Update the /etc/hosts file to ensure it can resolve the saltmaster hostname via a ping.
11.  Run this command: salt-minion -l debug

Part 3  Configure Salt Master to Communicate With Salt Minion
12.  Log on to the Salt Master server as root.

13.  Run this: salt-key -L

The output should look something like this:

Accepted Keys:
Denied Keys:
Unaccepted Keys:
salt-minion1.continualintegration.com
Rejected Keys:

14.  Run this command:
salt-key -a salt-minion1.continualintegration.com
respond to the prompt with "Y" with no quotes and press enter.

15.  You are done.
Now these commands should work:

  salt '*' test.ping
  salt '*' disk.usage
  salt '*' cmd.run 'ls -l /etc'
  salt '*' network.interfaces
  salt '*' test.echo 'foo: bar'

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 if(!($contint)) may not work as you expect.  You may have also tried different tests like these: if($contint = "")if($contint == ""), or if(!(Get-Variable $contint)).  Those will not seem to work if you have a multi-line variable with a blank line.

How do you test if the variable is empty while taking into account the variable's value itself spans two or more lines with a top line that is blank? 

Answer
Use this:  if($contint -eq "")

This will resolve to "true" if the variable is empty.  It will evaluate to "false" if the top line in the $contint variable is blank despite other characters on lines below the first line.

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.
3.  Read the resulting value into a variable (e.g., $a) and convert the value to a string.
4.  Use [int] and [convert] terms:

[int]$newa = [convert]::ToInt32($a)

How do you troubleshoot a CM tool doing an unattended installation on Windows Server 2012?

Problem scenario:  Using a CM tool, you transfer a PowerShell script to do an unattended installation on Windows Server 2012.  The CM tool transfers the media and the PowerShell script and launches the script once transferred. However, when you log into the Windows Server, you do not see the application installed.  When you manually run the PowerShell script and transfer the media over, there is no problem.  You want the CM tool to do the installation (transfer of media and execution of the PowerShell script).

Solution:  The CM tool is actually working.  If you manually log into the Windows Server for the first time after the installation happens, when you go to the Windows button, you will not see "x new apps installed" on the first menu.  This is visible only if you have logged into the server one time.  In a world where orchestration and automation are the new norm, we have to remember that software can be deployed to new servers without a human logging in.  Certain messages will not occur in these scenarios.  Go to the Control Panel -> Uninstall a program to verify that the software was in fact installed.  Also, be sure that the execution context of the Windows Server supports running a PowerShell script (unrestricted or remotesigned).

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.  The above error can happen because of pywinrm is 0.1.1.  To find out what version of pywinrm you have, run this:  pip show pywinrm
It may help to upgrade pip too (to cut down on informational warnings etc.):  pip install --upgrade pip

How Do You Boot To CD/DVD-ROM (or USB stick) on an HP Laptop one time?

Question:  How Do You Boot To CD/DVD-ROM on an HP Laptop one time (e.g., to reformat it)?

Answer:

  1. Turn off the laptop.
  2. Turn on the laptop, and when it is booting up, press Escape.
  3. Then choose option "F11" for System Recovery.
  4. You'll see a screen for "Choose an option."
  5. Select "Use a Device."
  6. Then select "Internal CD/DVD-ROM Drive (UEFI)" (or the USB stick option).
  7. When booting up you'll get a window that says "The selected boot device failed. Press <Enter> to Continue".  Press enter.
  8. In the "Boot Manager" window, scroll to "Internal CD/DVD-ROM Drive" (or the USB stick option) and press enter

n.b.  You cannot merely move the .ISO file to a CD/DVD or USB stick.  It must be burned a special way (e.g., using Rufus).  There is a portable version of the software program called Rufus that does not require installation.  Some laptops only have CD ROMs and cannot read DVDs.  Having incorrect boot media (e.g., a DVD in a CD ROM or an .ISO merely placed on a USB stick), will result in the boot order skipping the device after powering on the laptop.

Ansible Documentation Appears To Have Errors

Ansible's documentation (http://docs.ansible.com/ansible/intro_windows.html#inventory) indicates that inventory variables (in .yml files) are assigned with colons (":") and not equals ("=") signs.  However in practice, equals ("=") signs are necessary for key-value assignments and colons (":") do not work.  If you have found their documentation to be correct, please post a comment.

How to Install Virtual Machine Manager on Ubuntu 16

Problem Scenario:  You want to run VMs on your Linux server.  You are running Ubuntu 16.  You want to install Virtual Machine Manager (a free software package).

Solution:
1. Run this command to see if you have processors that can support virtualization:
sudo cat /proc/cpuinfo | grep -c svm || cat /proc/cpuinfo | grep -c vmx

If the result shows a 1 or higher, then you can install Virtual Machine Manager.  If you see "x" or "0" or combinations of the two, then Virtual Machine Manager will not work.

2.  Install these packages (assuming you have a Debian repository on your network or access to the Internet):
sudo apt-get -y install qemu-kvm libvirt-bin bridge-utils virt-manager qemu-utils python-spice-client-gtk

3.  Open Virtual Machine Manager.  Create a new VM or import an existing qcow2 file.

Related Troubleshooting Tip:
If you get the error in the monitor window display that says "spiceclientgtk not found," do the following:

In Virtual Machine Manager for the window monitor for a given VM, click the green "i" box.  On the left column go to Display VNC -> VNC Server -> Type (change "Spice Server" to "VNC Server").  Then reboot the VM.