Deep Down Troubleshooting/Debugging

I made a script with custom functions. One of the functions accepts a server name and returns the server details from a SharePoint list.

The surprising thing is that when I run the script it gives the server details of a particular server name, sometimes and and sometimes it doesn’t!

I have banged my head a lot, but it is quite surprising for me. Is there any trick for debugging the script in more details to get to the root of the problem.

I am not sure how the function returns the server details. Either it should not return the server details, everytime I run the script or it should return the details.

It’s tough to help people through the troubleshooting process when there’s nothing to see, and we have to make assumptions about what you mean. For instance, you said “server details” at least three times, but I’m still not sure what you mean. Can you include some code? Can you show us what server details look like? I get that your getting back results of some sort that are sometime present, and sometimes not present. Does this happen when you supply the same server name? So yeah, anymore you can add will likely be useful for those that would able and willing to help.

Hey mate,

we all find ourselves in this situation at some point. the best thing to do it slow down and run each line by line in the ISE. You can simply click on a line in the IE and press F8 just run that selection. another way that may help is to add some output to your script so you know exactly where it’s up to when it fails to return value. This could either be output to te verbose stream or if you’re not using an advanced function you can simple put some write-host in your script so you know exactly where its at.

You can also take a look at adding in a breakpoint

get-help Set-PSBreakpoint -examples

Or, post your code up here and we can help you out!
Cheers!

Function CheckEscalation($Server)
{
$mycredential = MyCred

Write-Host "Getting Escalation Information For " $Server -ForegroundColor Yellow

$ListContents=invoke-command -ComputerName kw1pespsa11 -Authentication CredSSP -Credential $mycredential {
    Add-PSSnapin Microsoft.SharePoint.Powershell
    $siteURL="http://siteaddress"
    $web=Get-SPWeb $siteURL
    $ListName = "Server Escalation"
    $List = $web.Lists[$ListName]
    $List.Items | select -Property Title, @{Label="DeviceName";Expression={$_["DeviceName"]}}, @{Label="Service";Expression={$_["Service"]}}, @{Label="Escalation";Expression={$_["EscalationInformation"]}}, @{Label="TechnicalOwner";Expression={$_["Technical Owner"]}}, @{Label="ServiceOwner";Expression={$_["Service Owner"]}}, @{Label="Function";Expression={$_["Function"]}}, @{Label="Instruction";Expression={$_["Special Instructions"]}}

}


$ListContents | where {$Server -eq $_.DeviceName}

}

(CheckEscalation “server1”).DeviceName

Write-Host “2nd Server”
(CheckEscalation “server2”).DeviceName
(CheckEscalation “server3”).DeviceName

Steps

  1. Open Powershell ISE
  2. Run the Code
  3. The Output is

Getting Escalation Information For Server1
Server1 (because the device name for server1 is server1)
2nd Server

(script ends here)

  1. Run the Code (again)
  2. Output is
    Getting Escalation Information For Server1
    Server1
    2nd Server
    Getting Escalation Information For Server2
    Server2
    Getting Escalation Information For Server3
    Server3

(script ends here)

  1. Close Powershell ISE
  2. Open it again
  3. Run the code
  4. Output is

Getting Escalation Information For Server1
Server1 (because the device name for server1 is server1)
2nd Server

(Script ends here)

These are the exact steps. I am not changing anything in the code. I am not changing the server names

The first time I run the code (after starting Powershell ISE) it just gives me the output for the first server. After the first server, it doesn’t even go inside the Check-Escalation Function (it seems). The second, third, fourth time etc I run the code, it gives me the correct output.

After closing and restarting Powershell ISE the same behavior is repeated!

Thanks Flynn. This code was part of a bigger script. I have tried everything you mentioned. And narrowed the problem to what I mentioned above.

 

Function CheckEscalation($Server)
{
$mycredential = MyCred

Write-Host "Getting Escalation Information For " $Server -ForegroundColor Yellow

$ListContents=invoke-command -ComputerName kw1pespsa11 -Authentication CredSSP -Credential $mycredential {
Add-PSSnapin Microsoft.SharePoint.Powershell
$siteURL="http://siteaddress"
$web=Get-SPWeb $siteURL
$ListName = "Server Escalation"
$List = $web.Lists[$ListName]
$List.Items | select -Property Title, @{Label="DeviceName";Expression={$_["DeviceName"]}}, @{Label="Service";Expression={$_["Service"]}}, @{Label="Escalation";Expression={$_["EscalationInformation"]}}, @{Label="TechnicalOwner";Expression={$_["Technical Owner"]}}, @{Label="ServiceOwner";Expression={$_["Service Owner"]}}, @{Label="Function";Expression={$_["Function"]}}, @{Label="Instruction";Expression={$_["Special Instructions"]}}

}

$ListContents | where {$Server -eq $_.DeviceName}

}

(CheckEscalation "server1").DeviceName

Write-Host "2nd Server"
(CheckEscalation "server2").DeviceName
(CheckEscalation "server3").DeviceName

I’m unable to reproduce this problem, so I’m tempted to believe it’s a product of your environment. Through visuals, I’ve included the shortened list I created, and the code I borrowed and modified for my environment. No matter when I run it (first time opening the ISE, or any time after), I still get the full results on the first run and all subsequent runs. There’s not much to suggest, as I would agree, the function is not being invoked on the second and following calls during your first run after opening the ISE. You might try other computers, running it on the SPCA server, restarting your machine, or digging into any SP logs. Perhaps something will give light as to why it’s working this way. If there’s anything different I should be trying, let me know and I’ll see if I can help.

SP List
http://tommymaynard.com/wp-content/uploads/2016/01/PowerShell.org-ForumVisual-SharePoint01-01.png

Code
http://tommymaynard.com/wp-content/uploads/2016/01/PowerShell.org-ForumVisual-SharePoint01-02.png