System 5 Error in Windows Server 2012

Problem Scenario:  In Windows Server 2012 from a command prompt, you run a .bat file that launches a PowerShell script.  You get an error that says "System Error 5" and "Access is Denied."

Solution:  Open a new command prompt as an Administrator.  Click the Windows button, search for "cmd."  Right click the cmd prompt chose "Open as Administrator."  Try running the .bat file again.

If that does not work, verify the relevant computers have correct time synchronization.

How to Resolve a Puppet Error About Unclosed Quotes

Problem scenario
When running "puppet agent -t" on a Windows server with Puppet Agent installed and configured, you get an error like this:  "Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Unclosed quote after """ followed by ..."

Solution
For the relevant manifest, verify there are no unclosed quotes.  Then verify that you are using double backslashes in any path with a backslash "\".  For example, here are paths that may look unusual, but they are correct in a .pp file:
file { 'c:\\temp\\contint.txt':
         ...}
exec { 'action':
      path => 'c:\\temp\\fun.txt'

Root cause 
Windows can use backslashes as escape characters.  So the parsing of backslashes can cause confusion for Puppet running on Windows.

How to Use Conditional Logic With A Puppet Manifest For Executing a Command

Scenario:  You want an exec command in a manifest to be invoked only if a certain file is on the Windows Server.  You do not want the execution of a command to happen if a file does not exist in a certain location.
Solution:  Incorporate the following lines into your manifest (.pp file).
exec { 'cont_int":
    command => "echo continual > contint.txt",
    provider => powershell,
    path => 'c:\\temp\\',
    onlyif => 'if(test-path c:\\temp\\contualint.txt) {exit 0;} else {exit 1;}',
    }

This is an example of a command that will only execute on a Puppet Agent node if c:\temp\continualint.txt is present.  If it is not found, then it will not run.

How Do You Find Out if The Server Has Puppet Master or Puppet Agent?

Problem scenario
Assuming that you know Puppet is installed, you want to find out if you are on a Linux server that has Puppet Master or Puppet Agent.  This posting applies strictly to Linux or Unix.

Solution
Run this command:
cat /etc/puppet/puppet.conf
Then use "ip addr show" (or ifconfig) to verify the IP address of the server that you are on. 

An alternative way to look for clues, depending on whether or not the server was at one time fully configured for Puppet, you may want to try one of these five commands:
history | grep master
history | grep agent
puppet --version
puppet master --version
puppet agent --version

Troubleshooting PowerShell Functions

Part 1
Problem scenario:
  You have a PowerShell function that should add two parameter numbers and display their sum.  It is displaying the input when it should not.  No sum is being displayed.  There are no error messages. What should you do?
Solution:  Your code may look like this:

function contint ($a, $b) {
    $c = $a + $b
    return $c}

$d = contint (5, 2)
echo $d

The solution is to change the "$d = contint" line to be like this:
$d = contint 5 2
#With just spaces between the function name and its positional parameters, the PowerShell function will be invoked properly.

Part 2
Problem scenario:  You have a PowerShell function that is not working.  When you run the script that invokes it, you get an error like this:

"Select-String : Cannot bind argument to parameter 'Pattern' because it is null.
At C:\temp\contint1.ps1:2 char:34
+     $ca = cat $a | select-string $b
+                                  ~~
    + CategoryInfo          : InvalidData: (:) [Select-String], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SelectStr
   ingCommand"

Here is the function in the script:
function contint ($a, $b) {
    $ca = cat $a | select-string $b
    return $c}

$d = contint ('C:\temp\foo.txt', 'alphaString')
echo $d

Solution:  The solution is to call the function without parentheses and without the comma like this:
function contint ($a, $b) {
    $ca = cat $a | select-string $b
    return $ca}

$d = contint 'C:\temp\foo.txt' 'alphaString'
echo $d
#  The single quotes on the string arguments are optional. 
# There can be no commas between the arguments when calling the function.

How To Troubleshoot the mkfs.ext4 Utility

Scenario / Goal: You are trying to add a hard drive (or new storage) to a Linux server.  You want the drive usable.  You attach the drive and use fdisk.  Now you want to use mkfs to create a file system on it.

Problem: You are trying to use mkfs.ext4 but you get this error:  "mkfs.ext4 no such file or directory while trying to determine filesystem size." 

Root cause: Wrong /dev/xxx argument.  This error message is misleading.

Use "cat /proc/partitions" to find the potential /dev/sdx options to choose from.  Then try your mkfs.ext4 command using a /dev/ item in the results of "cat /proc/partitions".  The problem should go away.

How To Install Pip On Red Hat version 6 with Python 2.7.x Installed

Goal:  You want to install pip on RedHat 6 with Python version 2.7.x installed.
Problem scenario:  
You try "yum install python-pipbut get an error about python(abi) = 2.6 being an unmet dependency.  Trying to install an older version may require you to downgrade other components.  The latest version of Python packages is often safer and recommended. 
Solution:  Obtain a copy of python-pip-7.1.0-1.el7.noarch.rpm.  Install it with yum.  This way you do not have to downgrade various components of Python.

Using Python and Ruby To Read Files

Problem:  You have a Python program that reads in a file named coolFile (in /tmp), and outputs the content to the screen.  The program prints an extra blank line after each line of content of the file named coolFile.  You want the output to not have an extra blank line.  Here is the code:

#/usr/bin/python

x = open("/tmp/coolFile", "r")
for line in x:
        print (line)

How do you have the output now print an extra blank line after each line?

Solution:  Insert a comma after the print (line) stanza.  Here is an example of source code that works:

#/usr/bin/python

x = open("/tmp/coolFile", "r")
for line in x:
        print (line),

Equivalent program in Ruby:  Here is the equivalent method in Ruby (a program that reads in a file, /tmp/coolFile, and prints each line out with no extra line):

#/usr/bin/ruby

File.readlines('/tmp/coolFile').each do |line|
    puts line
end

In PowerShell Diskpart Script Does Not Work (Despite No Error) Via “diskpart /s nameofscript.txt”

Goal:  To use a diskpart.exe script (a file of commands that could be ran interactively in sequence).
Problem:  When you run this command via PowerShell it does not work:  diskpart /s C:\Full\Path\script.txt
The result says something like "Microsoft DiskPart ..." and there is a list of alphabetic diskpart subcommands (ACTIVE to RECOVER).  There is no explicit error.  The diskpart command and script does no work.
Background:  When a PowerShell redirect (>) command is used to save a file, the default encoding is usually Unicode.  This encoding will not work for a diskpart script file (e.g., a .txt file of commands that could run interactively in sequence once diskpart is entered). 
Solution:  To resolve this, open the script, go to Save As, change the "Encoding" from "Unicode" to "ANSI."  Alternatively, if the diskpart script you are using is generated via PowerShell on an ongoing basis, use the Out-File and Encoding flags to designate the file to be ASCII.

How To Solve the PowerShell Error “The term ‘script’ is not recognized…”

Goal:  You want to run a .ps1 file.
Problem:  You get the error "Powershell : script : The term 'script' is not recognized as the name of a cmdlet, At line:1 char:1 + ..."
Solution:  Use the "script" resource section inside of a DSC configuration exclusively.  It will not work or run as a standalone PowerShell script.  Scripts with GetScript, SetScript, and TestScript must be run as part of a DSC configuration.  Also, DSC must be installed for configurations to work.  The "script" block should appear under the "configuration nameOfConfiguration" {} section of the .ps1 file.