Scheuled Task PowerShell script that relies on PSCX

Topics: User Forum
Apr 19, 2007 at 2:31 PM

I am setting up a scheduled task of a PowerShell script that relies on some of the PSCX cmdlets. I am trying to figure out how to make sure PSCX is loaded when the script runs.

I assume if I choose to run the scheduled task as the account that I installed PSCX, PowerShell will most likely read the profile for that user account and everything would work fine.

If I choose to use a different domain account though, no PowerShell profile will exist. I am thinking that maybe I should use some kind of a loader which would basically be a two line PowerShell script. The first line would load the PSCX profile and the second would execute my script.

Any other thoughts on how to do this?

Thank you,
Apr 19, 2007 at 6:48 PM
You could check to see if the PSCX snapin is loaded early in your script and if it isn't, add it using "Add-PSSnapin PSCX". You could also load PSCX via the global profile (egp).
Apr 19, 2007 at 10:23 PM
This appears to work to load the PSCX snapin. Would it be better to load the entire defult profile script instead of just the snapin if we detect that the snapin is not loaded?

#load Pscx if it is not already loaded
$snapin = get-pssnapin  | select-string "pscx"
if ($snapin -eq $null)
	Add-PSSnapin PSCX

The alternative would be something like this:

#load Pscx if it is not already loaded
$snapin = get-pssnapin  | select-string "pscx"
if ($snapin -eq $null)
	& 'pscx_profile.ps1'

Any other suggestions or better ways to check to see if the snapin is already loaded or not?

Thank You,
Apr 27, 2007 at 2:48 PM
Edited Apr 27, 2007 at 2:48 PM
It's not always about the pipelines ;-)

if ((get-pssnapin pscx) -eq $null) { add-pssnapin pscx }
Apr 27, 2007 at 5:03 PM
That is definately a lot nicer and cleaner, but...

When I do that from within PowerShell, I get three (for the lack of a better term) error lines:

Get-PSSnapin : No Windows PowerShell snap-in matches criteria pscx.
At line:1 char:18
+ if ((get-pssnapin  <<<< pscx) -eq $null) { add-pssnapin pscx  }

The code still works, but how do I get rid of these lines? I used the pipeline to avoid the errors outputs.
Apr 27, 2007 at 5:09 PM
I figured it out. If anyone is reading this, here you go :)

This will check to see if pscx is loaded, if not it will load it.
if ((get-pssnapin -name pscx -ErrorAction SilentlyContinue) -eq $null) { add-pssnapin pscx }

Also to check if PSCX is installed on a computer
if ((get-pssnapin -name pscx -registered -ErrorAction SilentlyContinue) -eq $null) { break }

**note the break will stop your PowerShell script if your script relies on PSCX and it is not installed.

Now, if the PSCX guys can come up with one line to download and install PSCX if it is not installed :)
Apr 27, 2007 at 5:12 PM
Or do it all in one line, first check to ensure PSCX is installed, then load the snapin if it is not loaded.

if ((get-pssnapin -name pscx -registered -ErrorAction SilentlyContinue) -eq $null) { break } else { if ((get-pssnapin -name pscx -ErrorAction SilentlyContinue) -eq $null) { add-pssnapin pscx } }
May 5, 2007 at 8:38 PM
cheers! I'm reading this...