2

I have created a powershell module (.psm1) file that includes a few other powershell scripts. We use it for sharepoint.

So basically, here's what happens:

  1. I have a deploy script that retrieves the module location from the registry
  2. It loads the module using the Import-Module cmdlet (using -force switch)
  3. This module in turn loads the Sharepoint 2010 snap in and a few other scripts that I created
  4. It runs runs a deployment script that references functions from the included scripts
  5. It also runs a command line application and sends the output directly to the screen

The script will usually work the first time. However, after a few number of tries the commandline tool will stop working and sending output to the screen altogether. And if I try to run a commandline tool (not a cmd-let) after running my script, it don't worky anymore: no output, nothing is done. Its just the same as hitting enter on a blank prompt. anything powershell specific or running GUI applications will work fine but running any console application will not produce any concievable results. the only solution to this, is to just close my powershell and open it again. it will work for usually once and I will have to close it again. our users certainly wont be happy about that..

The most 'notable' things on the script:

  • scriptblocks are used extensively (for logging), a script block is sent to a handler that executes it using invokecommand and logs the step
  • its manipulating sharepoint objects
  • all objects are properly disposed of
  • no static variables are created nor changed
  • There are a few global variables shared across all scripts

What I have tried: I striped my code to a bare minimum: loading an xml file, and restaring a few windows services but I'm still getting this intermittently. I have no idea which part of the code could cause this. I would love to post the code, but our company policy forbids me to. so my aplogies..

Update as per the comment below:

here's roughly how I use codeblocks. I have this function below that is used everytime I want to make the user aware of a task that I'm executing and what it outcome is.

function DoTask($someString, $scriptBlock, $param)
{
   try
   {
      OutputTaskDescription $someString
      InvokeCommand $scriptBlock -ArgumentList $param
      OutputResultOK
   }
   catch
   {
      OutputResultError $_.tostring()
   }
}

it could then be used like this:

$stringVar = "something"
$SpSite = new-spsite
deploySomething 'Deploying something' -param $spsite -ScriptBlock {
   dosomethingToObject $stringvar
   dosomethingToObject $spSite.Name
}

it would then output something like:

Deploying Something ------------- OK
Deploying Something ------------- ERROR

Also notice that I pass the $spsite in the argument list and I just use the string directly. I still don't understand how this works but it seems like I can access all primitive typed variables even without passing them as arguments but I have to pass more complex objects are params, else they dont have any value.

Update: after much searching and days of pain. I have found others with the same pain. My code exhibits the same exact symptoms as described here: http://connect.microsoft.com/PowerShell/feedback/details/496326/stability-problem-any-application-run-fails-with-lastexitcode-1073741502

I guess there is no solution yet to this problem.

Mel
  • 3,058
  • 4
  • 26
  • 40
  • Why do you use `Import-Module` with `-Force`? – Roman Kuzmin Nov 25 '11 at 12:22
  • No particular reason, (it was already there when I touched it). however, I tried removing it but didn't get any difference. But I cant rule out the possibility that it might be causing the problem in junction with something else that i've done? – Mel Nov 28 '11 at 06:58
  • I asked mostly out of curiosity. I do not use this switch in my scenarios and I am interested in scenarios where it is needed. As for your issue, I've never seen something like this. But script modules do have unexpected behaviour and issues. If it is not too difficult to try, I would recommend a script library instead of a script module (I solved some problems by converting modules to scripts). This is a silly advice perhaps but there is nothing else, as I see. – Roman Kuzmin Nov 28 '11 at 07:34
  • BTW, do not "lose hope on powershell". There are usually much more than one way of solving a task in PowerShell. The first implemented is often not the best (talking for myself :)). – Roman Kuzmin Nov 28 '11 at 07:42
  • 1
    Just in case: https://connect.microsoft.com/PowerShell/feedback/details/496326/stability-problem-any-application-run-fails-with-lastexitcode-1073741502 – Roman Kuzmin Nov 28 '11 at 16:21
  • That almost sounds like something machine specific. Have you tried it on more than one system? You might also use Task Manager to verify it is using a reasonable amount of memory, threads and most importantly handles. – Eric Nicholson Nov 29 '11 at 13:55
  • Yeah, actually, I've run this off 5 different machines in two different geographic locations. Performance is not an issue, all machines are are in 16/32GB multi processor (forgot how many) configurations and its just running sharepoint and my script, nothing else. – Mel Nov 29 '11 at 13:59
  • Mel, you use scriptblocks extensivly you say. Keep in mind that modules have their own variable scope tree. I don't know how you use your code but you might need to use GetNewClosure() to handle scriptblocks properly. Please see my answer to this other question which is closure related. http://stackoverflow.com/questions/6264374/variable-used-in-function-from-a-module-is-empty-powershell/6452452#6452452 – CosmosKey Dec 02 '11 at 15:03
  • @CosmosKey Hi Cosmos, I've updated my question to reflect how script blocks are used. it maybe related to that.. but honestly, I'm not sure. – Mel Dec 03 '11 at 05:16
  • @RomanKuzmin my bad. I didn't see the link you posted. Its seems its the same exact thing. now... what to do next..... – Mel Dec 13 '11 at 11:51
  • @Mel, can you post the code for your InvokeCommand function? (Or did you just leave the dash out of Invoke-Command?) Also, try doing a "gci variable:" after your execution and see if there's anything that needs to be cleaned up. Do you have anything in your profile that logs your transaction or command history that could be doing something silly? – Kevin Buchan Dec 13 '11 at 12:22
  • @KevinBuchan yeah, that was just a typo. what exactly is a gci variale? do you mean gci == getchilditems? – Mel Dec 14 '11 at 05:52
  • Yeah, GCI is an alias for Get-ChildItems. By calling it on "variable:", you will see what variables are defined. There may be something useful/revealing in that and you can use Remove-Variable to clean them up. It's just a thought. – Kevin Buchan Dec 14 '11 at 12:33
  • Pretty sure the problem is with the external command. I'm having a ton of trouble finding this documented online, but there's a pretty well-known bug/design issue with PS v1/2 where it starts acting wonky after too many writes to the console from external applications. "Too many" is relative to your system. I believe this is fixed in V3. – Daniel Richnak Feb 20 '12 at 03:27

1 Answers1

1

After a little while I've noticed that if I've ran some very memory intensive functions, I too have gotten that behavior where everything you try to execute just goes to the prompt again. I'd recommend setting Set-PsDebug -Trace 2 to see what those functions are actually doing. I fixed my issue by doing this and figuring out how to make my functions more efficient.

Adam Bertram
  • 3,858
  • 4
  • 22
  • 28