Tuesday, April 19, 2016

ZTIOSDNotify.ps1

Sometime ago I started using Mikael Nystrom's ZTISendmail.ps1.  It was a great script however what I wanted and needed to do in my environment grew over time and I have expanded the script.  I therefore present ZTIOSDNotify.ps1 with much thanks and Credit to Mikael for providing the jumping off point.  

While nothing was wrong with Mikael's script there was a need to do some additional things.  I was asked to post my results of OSD deployments to Sharepoint, the original script didn't handle SCCM deployments and the generated log files in in a way I liked (didn't give me everything), and I was asked to also post the logs to a network share (this part is actually in the TS and not the script,  but they work together so I'm including all of it)

I am including links to the Script itself and to an exported SCCM TS with component parts that need to be added to a TS to use the script one Section however requires the SCCM NAA account so I will point out where to add that and how to set it up, since I can't save and export that step without valid details.

ZTIOSDNotify.ps1

NotifyTS Parts


I keep the script in my toolkit package, however it could be a package of its own as well so long as the toolkit package is also loaded since it depends on functionality from the Toolkit package.

Using the script requires a number of TS Variables (either in CS.ini or the TaskSequence, I actually do some in both places) and some specific setup in the Task Sequence to use all of the functionality.  I will document everything below.

Task Sequence Variables (Where and how to set):
Set or create the following TS variables in the Customsettings.ini (or Task Sequence Initialization)


[Settings]
Priority=Sendmail, Default
Properties=OSDSendMailFrom, OSDSendMailToPri, OSDSendMailToSec,  OSDSendMailSMTPServer, OSDSendMailIncludeBDDLog

[SendMail]
OSDSendMailFrom=ImagingTeam@domain.com
OSDSendMailToPri=ImagingTeam@domain.com
OSDSendMailToSec=OtherPartys@domain.com
OSDSendMailSMTPServer=smtp1.domain.com


or
(if your organization has more than one SMTP Server and you want to route traffic regionally for example)

[Settings]
Priority=SetSMTP, Sendmail, Default
Properties=OSDSendMailFrom, OSDSendMailToPri, OSDSendMailToSec,  OSDSendMailSMTPServer, OSDSendMailIncludeBDDLog

[SendMail]
[SendMail]
OSDSendMailFrom=ImagingTeam@domain.com
OSDSendMailToPri=ImagingTeam@domain.com
;this could be removed from CS.ini and populated in the gather phase to be generated some other way into a list of people to be notified, say the Local IT technicians for the office the machine is being imaged in.
OSDSendMailToSec=OtherPartys@domain.com
;OSDSendMailSMTPServer=smtp1.domain.com

[SetRegion]
UserExit=RegionExit.vbs
OSDRegion=#GetRegion()#


[SetSMTP]
;You will need some sort of Userexit or other code which returns the OSDRegion similar to the reference to RegionExit.vbs above
Subsection=SMTP-%OSDRegion%

[SMTP-AMR]
OSDSendMailSMTPServer=smtp1.domain.com
[SMTP-EUR]
OSDSendMailSMTPServer=smtp2.domain.com
[SMTP-ASP]
OSDSendMailSMTPServer=smtp3.domain.com

Set or Create the Following Task Sequence Variables in the Task Sequence Initialization:

TSVersion  - This is a custom variable I use in my environment which stamps a version of the TS in the log files so the script can reflect which TS and version of it.  My environment has a couple TS in deployment so its good to know which one generated the Logs, Email, SharePoint Post, etc  Its typically set to a Major and a Minor version xx.yy (ex 12.34, 14.22)

OSDSendMailIncludeBDDLog - Custom Variable a YES/NO Value should the script Capture and send the OSD logs, for SCCM the will cause the SMSTSLOG folder to be zipped up and captured, since the BDD.log doesn't contain everything like MDT.  This Variable instead be set in the Success or Failure sections later to get more control over if the logs should be included or not depending on success or failure

Logcapture - Custom Variable to indicate under what conditions the results should be emailed, copied to network, posted to sharepoint - Success/Failure/Both

OSDPostToSharePoint - Custom Variable a YES/NO Value should the script attempt to post the OSD results to a sharepoint site defined in the script. This Variable could instead be set in the Success or Failure sections later to get more control over if the logs should be included or not depending on success or failure

TestSettings - This has some extended use in my environment, but for the purpose of the script it indicates in the email and postings if the TS was in testing which is a quick reference to those seeing the results if the results should be followed up or not.  Is a YES/NO Value

LaunchSUI - I use this to trigger the launching of the Service UI Command line for debugging purposes of an image

Using the Script and Integrating with the TS:

Two sections are added to the TS to handle success and failure notifications in the TS, they are substantially the same, but need to be located in different places, particularly the Failure sections so it will properly process the Task Sequence Error code.

For a failed TS:
By Default a Standard TS includes a section to handle errors, this section should be enhanced adding a number of steps between "Copy Logs" and "Error in the Task Sequence"



Define the RunFailure Variable Set to YES in this section so the script will know it is handling a TS failure



For a Successful TS:

For a successful TS a very similar set of steps needs to be added someplace before your TS successfully exits, keeping in mind that the Toolkit package and Gather step must exist shortly before the Steps run to have all the required information available.  A good place to do this is just before the "Report Done" step, Add a toolkit package reference and Gather step then add the Success section.

Define the RunFailure Variable set to NO so the Script and TS will know to handle the reporting of status as a Successful TS Run.



Both Success and Failure require a connection to a network location by the SCCM NAA if the log file copying to a network share functionality will be used, this step is "Connect to Network SCCM Log Folder" it couldn't be exported without including valid account details for a NAA so, I will instead detail how to set it up:

  1. A "Connect to Network Folder" step to the Task Sequence and Name it "Connect to Network SCCM Log Folder"
  2. Create a share on a server in your environment which will receive the files, under that share create a "Failure" and "Success" subfolder
    ex:
    \\logsserver\logsdir$
    \\logsserver\logsdir$\Success
    \\logsserver\logsdir$\Failure
  3. The SCCM NAA Account should have at least "Modify" NTFS Permissions and Read/Write on the Share
  4. In the TS Step Input the path to logsdir$ on the server
  5. make it the "L:" drive
  6. In the Account property add the SCCM NAA and the valid password when prompted
SharePoint Integration:

If the TS Variable OSDPostToSharePoint is set to YES, then the functionality to post to SharePoint will be triggered.

A SharePoint List must be setup to receive the posting information through WEBDAV.  The account the TS is running as needs to have modify permissions to the SharePoint list.

I have included a screenshot of the Sharepoint list settings.  I'll enhance this section of documentation as needed and as I find time. 


Basically a SharePoint list needs to be created with the above field names.  The SCCM NAA or MDT Deployment Account needs to have permissions to the list. 

The URL to the list needs to be added to the ZTIOSDNotify.ps1 script at line 31:
Replacing the information in the "<>"
$TeamSiteURL = "http://<site>.<Replace Domain>.com/sites/<Sharepoint Sub Site>"

Update the List name at line 33:

$List = "OSD Results"


Other Notes:

In the ZTIOSDNotify.ps1 Script there are lines of code which picks up the MODEL TS Variable,  If you are using the ModelAliasExit script in your environment to get more sane and friendly Model Names especially for Lenovo models.  I advise replacing the lines of code in the script with a reference to the MODELALIAS TS variable created by that script to get a sane model name. 

Line 67:
Write-Progress -Activity "Sending email" -Status "Got Machine Model $TSEnv:Model" -PercentComplete 55 -Id 1
Becomes:
Write-Progress -Activity "Sending email" -Status "Got Machine Model $TSEnv:ModelAlias" -PercentComplete 55 -Id 1

Line 104:
Model: $TSEnv:Model<br/>
Becomes:
Model: $TSEnv:ModelAlias<br/>

Line 124:
Model: $TSEnv:Model<br/>
Becomes:
Model: $TSEnv:ModelAlias<br/>


No comments: