Jump to content


lucastee

Powershell and Status Filter Rules

Recommended Posts

Hi All,

 

I've been having some issues executing powershell scripts using the status filter rules. I am able to run VB scripts without issue, but powershell scripts do not seem to execute.

 

I have a VBscript that runs under the same conditions which executes without issue.

 

The PS script is a basic script to send an email to the SCCM administrators upon successful completion of a task sequence.

It will work perfectly when run manually, under both administrator and system accounts context.

 

Powershell Execution Policy is set to unrestricted.

 

The command line for the script is 'powershell -file \\SCCM\d$\packages\scripts\complete.ps1 %msgsys'

I have also tried directly pointing to D:\Packages\...etc.

 

Any ideas?

Share this post


Link to post
Share on other sites

I don't see why you are using the %msgsys variable at all. You can simply use %computername% inside of your script.

Also, create a package for your script and don't run it from the distribution point.

Always run powershell comand lines with the switch "-executionpolicy bypass".

Share this post


Link to post
Share on other sites

Hi. Thanks for the reply.

 

I was using the %msgsys variable because technet lists it as a status filter rule variable. (http://technet.micro...y/bb693758.aspx)

It allows me to pass properties of the status message to the program it runs, or in this case, my powershell script.

 

Ideally, I would like to have the script run as a status filter rule, instead of in a package, as it would allow me to run the same script no matter which task sequence runs. i.e I can pass the PC name and task sequence name to the script and have it report back to me. This eliminates overheads such as adding the script to each new task sequence. Admittedly, there won't be all that many, but I'd still like to do it this - if possible.

 

Since posting, I have tried running the script with the switch -executionpolicy unrestricted, to no avail. Would setting it to bypass do any more?

 

Thanks for all the help.

 

Luke

Share this post


Link to post
Share on other sites

Hi Luke,

 

the bypass switch is just telling powershell to ignore the local policy and to run the script in any case.

Also make sure that the Network Access Account has access to the share you are referring to in your command line. Linking it directly to the D$ is probably not a good idea. Also pointing to the local drive of the server "D:\..." will not work.

If you run an external command inside of your script which requires administrative rights, make sure that you start them with the "-nonewwindow" switch. Otherwise it will trigger the UAC and your script will just hang until timeout.

Share this post


Link to post
Share on other sites

Thanks Peter33,

 

I ended up getting this working after quite a bit of fiddling around.

 

I was able to create a Status Filter Rule that simply ran my script whenever the return code matched the action.

e.g 11143 is returned by the SMS Agent whenever a task sequence is complete.

Add the action to 'Run program field' and I was good to go.

 

Thanks for the help.

Share this post


Link to post
Share on other sites

I hope that someone is still monitoring this thread. I'm experiencing the exact same issue as 'lucastee' but there is no actual resolution mentioned so I'm at a loss as to how to fix this issue.

I have tested using the same method. Created vbscript to send email and it worked fine but I'd like to use powershell. Seems like there is some sort of permission or something that will not allow powershell to be run from the status filter action field.

 

Here is the syntax I've been trying: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -file "D:\OSP\Scripts\Powershell\OSDFailed.ps1" -argumentlist "%msgsys, OSD - Install Corporate SOE Test'"

The script itself requires two variables which I've defined on the command line as %msgsys and "OSD - Install Corporate SOE Test

 

I'm using Michael Niehaus' custom powershell module that is being called using a default profile in the C:\Windows\system32\WindowsPowerShell\v1.0 directory so that it applies to all profiles and this is where I'm not sure if there is some issue with the system account or what ever account runs the status filter action is not loading the module.

 

According to my statmgr.log file the process runs and exits cleanly, but no email is sent:

 

Created child process 2408 for program "C:\Windows\System32\WINDOW~1\v1.0\powershell.exe" and command line arguments "-file "D:\OSP\Scripts\Powershell\OSDFailed.ps1" -argumentlist "BGCB62S, OSD - Install Corporate SOE Test"

 

The process exited with exit code 0.

 

Here is the code of my powershell script (*server names changed):

 

param([string]$strComputerName, $strSequenceName)

 

$strSCCM = Connect-SCCMServer -HostName SERVER -siteCode A00 #-credential $cred

$strPCinfo = Get-SCCMComputer -SccmServer $strSCCM -NetbiosName $strComputerName

$strSite = $strPCinfo.ADSiteName

$strOSType = $strPCinfo.operatingSystem.Caption

# Check that value exists for $strSite

If(!$strSite) {$strSite = "Could not determine site"}

# Asia sites

ElseIf($strSite -eq "Shanghai") {$strSLShare = "SERVERNAME"}

ElseIf($strSite -eq "Mongolia-UHG") {$strSLShare = "SERVERNAME"}

ElseIf($strSite -eq "Ulaanbaatar") {$strSLShare = "SERVERNAME"}

ElseIf($strSite -eq "Beijing") {$strSLShare = "SERVERNAME"}

# South America sites

ElseIf($strSite -eq "Santiago-Chile") {$strSLShare = "SERVERNAME"}

# South Africa sites

ElseIf($strSite -eq "Johannesburg") {$strSLShare = "SERVERNAME"}

ElseIf($strSite -eq "Boseto") {$strSLShare = "SERVERNAME"}

# Australian sites

ElseIf($strSite -eq "Perth-170") {$strSLShare = "SERVERNAME"}

Else {$strSLShare = "SERVERNAME"}

#Set Report ID

If($strSequenceName -eq "OSD - Install Corporate SOE") {$strReportID = "A0000032"}

If($strSequenceName -eq "OSD - Install Corporate SOE Test") {$strReportID = "A0000031"}

Send-MailMessage `

-SmtpServer "SERVERNAME" `

-to "SCCM Mail <EMAILADDRESS>" `

-From "SCCM OSD <EMAILADDRESS>" `

-Subject $strComputerName": OS Deployment Failed" `

-body @"

<style type="text/css">

/* Makeshift CSS Reset */

{

margin: 0;

padding: 0;

}

/* Tell the browser to render HTML 5 elements as block */

header, footer, aside, nav, article, div {

display: block;

}

body {

text-align: center;

margin: auto;

width: 800px;

font: 13px/22px Arial, Helvetica, sans-serif;

}

h1 {

font-size: 28px;

}

h2 {

font-size: 22px;

}

strong {

color: #003366;

}

p {

margin-bottom: 5px;

}

td {

text-align: left;

}

table {

margin: auto;

}

#intro {

margin-bottom: 20px;

padding: 20px;

background-color: #232135;

background-size: 100%;

border-radius: 22px;

height: 50px;

}

#blogpost {

font-size: 16px;

margin-bottom: 20px;

padding: 20px;

background-color: #EBEBEB;

background-size: 100%;

border-radius: 22px;

color: #333;

}

#resultcolor {

color: #FF0000;

}

#colorchange {

color: #4F81BD;

}

#footersub {

color: #808080;

}

</style>

<html xmlns="http://www.w3.org/1999/xhtml">

<body>

<header>

<h1>CORPORATE OSD RESULTS</h1>

</header>

<div id="intro">

<h2 id="resultcolor">DEPLOYMENT ERROR</h2>

</div>

<div id="blogpost">

<p>The Operating System Deployment Task Sequence <br/><strong>$strSequenceName</strong><br/> has encountered a problem installing Windows<sup style="font-size:11px">©</sup><br/><br/>

Computer Name: <strong>$strComputerName</strong>  |   Site: <strong>$strSite</strong>   |   Time: <strong>$strTime</strong></p>

</div>

<div>

<p>Please use the links below for troubleshooting issues:</p>

<table>

<tr>

<td><b>STATUS REPORT:</b></td>

<td> </td>

<td> <a href="https://SERVERNAME/Reports_SCCM2012R2/Pages/Report.aspx?ItemPath=%2fConfigMgr_A00%2fSoftware+Distribution+-+Package+and+Program+Deployment+Status%2fAll+system+resources+for+a+specified+package+and+program+deployment+in+a+specified+state">Status Report Page</a></td>

</tr>

<tr>

<td><b>BUILD LOGS:</b></td>

<td> </td>

<td> <a href="file://\\$strSLShare\BuildLogs\$strComputerName">$strComputerName Log Directory</a></td>

</tr>

</table>

</div>

<div>

<!-- Footer -->

<hr/>

</div>

</body>

"@ `

-BodyAsHtml

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.