PowerShell Logging – The Basics

PowerShell Logging will be the best thing you learn.  The biggest topic I get asked for help outside the specific activities I am assigned to is PowerShell.    I will be the first to understand everyone has their own style.  I never judge the style or syntax format.  Besides that, PowerShell logging ALWAYS needs to occur.  If you are not logging, your running blind. PowerShell logging is the one the most powerful features you can learn and implement.  

PowerShell does have some native abilities, “Start-Transcript“, and if that is all you want to understand, that is fine.   But, this post will take you through some basic understandings on capturing your outputs in a format that is readable and standard.  With some tricks in creating some great logic around managing your log.

PowerShell Logging – The Setup

The first step into your new logging endeavors is to understand the ability to call a function to simplify your process. 

For this function to become usable we first must set the variable “$logpath” so the function can understand where it is writing to.   My standard is to log to “%SystemDrive%\Windows\Temp”

Calling the Function

During your process of creating your script you will want to call the PowerShell logging function to say your going to run a process, log the process file call, its arguments, other module calls, and most important capturing any error output. 

PowerShell Logging – Catching the Exception

When starting any process, module, function, or block you should wrap it in “try and catch”.  This will ensure you script never exits unexpectedly and you always capture what is happening.  This is what I have found to be the easiest way to handle most scenarios. 

PowerShell Logging – Using the Error Variable

A option for logging functions is with the built in “cmdlet” switch “-ErrorVariable”.  This parameter captures the error of the function you are running.  To test use the following example. 

PowerShell Logging – Capturing the Exit Code

When you run “Start-Process” you can capture the exit code when you assign it to a variable with the “-wait” switch included.   This helps when you need to capture non-failure exit codes. For example the “3010” exit  code means there is a “Soft Reboot” required.  This exit code is not a failure.  

Bringing It All Together

The below script block brings the above examples together.  Calling the process through a try and catch loop and checking the exit code. 

PowerShell Logging – SCCM Task Sequence

If your responsibilities have you running a lot of processes in a SCCM task sequence environment the following code block will be most helpful.  This will allow you to log to the same directory the SCCM task environment is logging to. The below script block outlines loading the COM object to test if you are in such an environment and gather the directory where logging is occurring. 

When the Microsoft Deployment Toolkit (MDT) is integrated, and utilizing the “Copy Logs on Error” function, these logs will be copied to the log monitoring directory.  

PowerShell Logging with a Process

When developing a process that runs on a schedule you will want to manage the file.  The below script block shows how to parameter the log location and size of the file; with applying a header to a new log file.

PowerShell Logging to the Event Viewer

When developing workflows, services, or schedule tasks you can use the system’s event viewer to capture your necessary output.  Utilizing the system’s Event Viewer with PowerShell Logging will allow you to monitor the process with a tool like System Center Operations Manager, Microsoft Operations Manager Suite, or any other log analytic system. 

With All That Being Said

When developing your next PowerShell script or process; I hope this post has outlined some necessary tools or ideas that will ultimately lead to more PowerShell logging.  Just having this in tool box has saved me countless of hours sifting through command windows and debugging the script.  

PowerShell Uninstall Automation

I have authored the PowerShell Uninstall script.  It gives the ability for IT Operations / ConfigMgr Admins to uninstall multiple versions of a particular software with one process.

Does your organization struggle when trying to cleanup software when standardizing on a application version? I.E. Oracle Java, Adobe Flash, RightFax, IBM products, etc…  Not all the versions you have are in ConfigMgr / SCCM or not in the application model?  If the answer is yes, the PowerShell Uninstall script is for you.

The PowerShell Uninstall script takes inputs from a CSV file.  Using the delimiter of “^”, don’t worry I have included a Excel file to build your CSV.

Since by nature when uninstalling legacy software there maybe pre-or post- process you must run. The PowerShell Uninstall script has functions to call a PowerShell script before or after the uninstall logic.

The PowerShell Uninstall script also includes a Boolean to  create the script as an ConfigMgr / SCCM Application.  In which you can attach to other programs for requirements.

To detect the PowerShell Uninstall script, have ConfigMgr / SCCM check existence of “%SystemDrive%\Program Files\Pomeroy\AppName”.  It will be an empty directory.  For the uninstall just use the command “rmdir /Q /S %SystemDrive%\Program Files\Pomeroy\AppName”

Continue reading

Introducing: PowerShell Gallery

The wonderful PowerShell folks announced a great new PowerShell feature called gallery that is coming in 5.0

Introducing the PowerShell Gallery – PowerShellGet is an exciting new feature releasing in WMF 5.0 Preview.  With it, you can easily discover, install, update, and publish PowerShell modules to and from online repositories – simplifying the process of sharing PowerShell content.

Find out all the details over here:

http://blogs.msdn.com/b/powershell/archive/2014/11/19/powershell-gallery-live.aspx

 

PowerShell DSC Resource Kit Wave 9

Over on the “Windows PowerShell Blog” they announced the release of PowerShell DSC Resource Kit Wave 9.

Head over to the blog to read all about it:

Here’s a holiday gift from the PowerShell team – the DSC Resource Kit wave 9. You can find it right here.

This wave has something for everyone, we hope. There are 34 new DSC resources across 13 modules! We have also incorporated your feedback, and made fixes to several existing resources that address issues you have encountered.

Last year we delivered the first DSC Resource Kit as a holiday gift, with 11 DSC resources. In case you are counting, we now have 172 DSC resources, enabling use of DSC for deploying and configuring Active Directory, Azure VMs, Certificate Services, SQL Server, DNS, file shares, Failover clusters, Internet Explorer, Exchange, web applications, Hyper-V, networking, Just Enough Administration, DNS, Remote Desktop Services, the System Center products (SCDPM, SCOM, SCSMA, SCSPF, SCSR, SCVMM), and more. In all, this is a great start in a single year, with more to come in 2015!

http://blogs.msdn.com/b/powershell/archive/2014/12/17/another-holiday-present-from-the-powershell-team-dsc-reskit-wave-9.aspx

SCCM 2012 Folder Creation Script

I have created a script that will automate SCCM 2012 folder creation.  Either in bulk or single by single. It is a simple script with no parameters.  You just run the script and follow the instructions.  If you want to bulk folder create the text file must be in the format where the folder names is per line.

Example.

folder name 1
folder name 2
folder name 3
folder name 4
…..

Here is a walk through.

Get Help:

Get-Help.

Single Folder Creation:

Single folder creation.

Bulk Folder Creation:

Bulk folder creation.

Click the download link to give it a try.

CM2012 Node Folder Creation v2.0

 

Randomize Local Administrator Password

I created this tool to randomize local administrator passwords.

This application takes two strings and creates a calculated and complex password based on the input.   Now the local administrator’s password will be unique to each computer.

Here it is at screen launch:


A. The organization’s asset identification string.
B. The organization’s custom identification number.  Up to 10 digits long.
C. The selected password length for added complexity.
D. After all input is entered the user generates the complex password.
E. The result of the calculation.

Result:

The best part of this application is also supports it being used in the console.  This gives the application the ability to be scripted during an OSD build, deployed enterprise wide, or required update. Below is the console help menu and a PowerShell script I wrote to automate the process.  This is included in the *.zip file.

Enterprise Help Desk Dynamic Admin – Version 1.0.0.1

 

System Center 2012: PowerShell – Connecting to Configuration Manager

From over on the TechNet blogs, explains the PowerShell connection with System Center 2012

Welcome to installment #2. In case you missed installment #1 – Getting Started – here’s the link. With the Getting Started blog, you tried a few basic Windows PowerShell cmdlets. I hope you took a bit of time to explore what other cmdlets are available in Windows. But now it’s time to get Configuration Manager hooked up to your PowerShell environment. Let’s start with the easy method………

Read the full post over on the “System Center Configuration Manager Team’s Blog Site”……

http://blogs.technet.com/b/configmgrteam/archive/2013/03/27/powershell-connecting-to-configuration-manager.aspx

SCCM Admin Tools

I have created a new tool, SCCM Admin Tools, using the new SAPIEN PowerShell Studio 2012.This tool will become more robust, but for right now all you can do is remotely start / stop / or check status of the SMSEXECUTIVE and WDSServer services on your SCCM sites.

You will be able to save the settings for the site server’s name and site code.  You will want to use your central site if you have one.  Or the top most primary.

Here are some screenshots:

 

You can download the executable from here.  Get in touch with me if you have any other suggestions. Once I get the application to a certain goal I will release the source files.

SCCM Admin Tools v1.3.2.1

Snow Land’s SCCM-Commands

I have had the privilege of getting my hands on the SAPIEN PowerShell Studio 2012.  Using SCCM-Commands on the backend is so easy.  As a SCCM Administrator I have mostly focused right now on making my life easier on the job.  Cutting all the unnecessary process I need to do manually.  So if you are an SCCM Administrator and you love you some PowerShell than check this link out.

http://www.snowland.se/sccm-posh/

This is a great module.

Find AD Stale Computers

Active Directory Stale Computer Report

I have created a nice PowerShell script to find your stale computer objects in Active Directory. With being a SCCM Administrator it is crucial you have a healthy AD environment.

Requirements:

Quest Active Directory CMDLET
http://www.quest.com/powershell/activeroles-server.asp

Usage:

.AD_StaleComputer_Cleanup.ps1 -DC “Domain Controller” -Old “PC Not logged in for X amount days” -Path “Path of the folder you want the report saved too”

Example:

.AD_StaleComputer_Cleanup.ps1 -DC “DCServer01” -Old 90 -Path “C:PathofFolder”

 

AD_StaleComputer_Cleanup.ps1 v1.0.0.1