VSTS release only logs the 1st line of a Powershell exception as an error. How can I log the full exception details without enabling debug? - powershell

If I run this code (containing a null reference) in Powershell ISE
$result = $nonExistentString.Substring(0,4)
An exception is thrown and the following is output to the console
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $result = $nonExistentString.Substring(0,4)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
However, if I run the same code in a VSTS release as a powershell inline script, the release log will only output the following :
2017-11-30T13:23:26.9471504Z ##[error]You cannot call a method on a null-valued expression.
This isn't enough detail to facilitate problem resolution in more complex scripts. I know I can run the scripts in debug mode and the full exception record is logged as debug entries, but this would mean we would need to run all scripts in debug at all times in order to get full exception details when they occur. Does anyone know if it possible to configure VSTS to log full exception details without having to enable debug?

Related

ADSI commands causing Access Denied errors

I'm attempting to set the description attribute in ADSI via PowerShell, but keep getting "access denied". It's executing from a non-privileged user, but the user does have Write All Properties permissions over this object. With those permissions, I don't see why I would need to be a DA to write to that attribute.
Example shown here:
$Computer = [ADSI]"LDAP://cn=TESTER,cn=Computers,dc=lab,dc=local"
$Computer.Put("description", "test")
$Computer.setinfo()
$Computer.setinfo() Exception calling "setinfo" with "0" argument(s): "Access is denied. " At line:1 char:1 + $Computer.setinfo() + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI`
When I don't include the parenthesis (eg: $Computer.setinfo), it sets the attribute locally but never pushes it to AD. Yes i'm running in an elevated PS session.
As #Lee_Dailey mentioned, you'd better include the error message in the original post.
About the issue, are you running the script in an elevated prompt? It does not matter whether user has permissions on AD object or not if the resource requires running in elevated prompt. You should explicitly run as administrator if it needs. Can you please check again to see if that is the case here?

Why am I getting an error when trying to remove a Microsoft.Insights/components resource?

I am trying to execute a PowerShell script that will sequentially remove Azure Resources from a resource group using a .ps1 file, and have isolated an error I am getting to whenever I try to remove the Microsoft.Insights/components resource using the command:
Remove-AzureRmResource -ResourceId "/subscriptions/e41d3122-bbd8-48dc-a212-0337139671cc/resourceGroups/TestRG/providers/Microsoft.Insights/components/WA-Stag-API-EMEA-zgqmgcwnigknu"
The error I'm getting after running this once is as follows:
Remove-AzureRmResource : {"code":"Message: {\"Errors\":[\"One of the
specified pre-condition is not met\"]}","message":"Message:
{\"Errors\":[\"One of the specified pre-condition is not
met\"]}\r\nActivityId: 845b19fa-b6b4-4952-9b62-75bfc6e98646, Request
URI:
/apps/1921ec42-1c88-4fdd-8d6c-78646cba4b15/services/a32b484a-6ee1-4ad7-ab43-4e9
d57bb0b81/partitions/fab8c193-3ec3-45e8-b7a6-0e21d1e071da/replicas/131441657816991616p","innererror":{"diagnosticcontext":"597ea546-7625-474d-b27f-560a1140a652","time":"2017-
07-18T19:41:25.3734216Z"}} At line:1 char:1
+ Remove-AzureRmResource -ResourceId "/subscriptions/e41d3122-bbd8-48dc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Remove-AzureRmResource], ErrorResponseMessageException
+ FullyQualifiedErrorId : Conflict,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.RemoveAzureResourceCmdlet
From what I can tell, there seems to be some kind of "pre-condition" that needs to be met before you can delete it... but the weirdest part is that if I execute the same command again, I don't get an error and the resource is successfully deleted...
Is there any reason why this would fail once, but then succeed in being deleted after executing again?
This is because the Insights is used by your Web APP. Please refer to this link. When you enable client side monitoring, you will get the error log. I test in my lab, I get the same result with you.
If you want to avoid this error log. Please refer to the following steps:
1.Delete APPINSIGHTS_JAVASCRIPT_ENABLED key.
2.Restart your app.
3.Delete Insights.

Powershell [system.reflection.assembly].loadfile() Exception Access is denied when run by Jenkins

I have a very simple Powershell script which I want to use as part of a Jenkins build to return Assembly information. The script contains the following:
Write-Host([system.reflection.assembly]::loadfile($args[0])).FullName
When I try and call this script as part of the Jenkins build I get the following errors:
.\GetAssemblyInfo.ps1
Exception calling "LoadFile" with "1" argument(s): "Access is denied.
(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
At C:\Jenkins\workspace\Tools\NuGetBuildSupport\GetAssemblyInfo.ps1:1 char:50
+ Write-Host([system.reflection.assembly]::loadfile <<<< ($args[0])).FullName
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
The command runs fine when it is not executing as part of the Jenkins build.
The DLL that is being passed to loadfile is not marked as Blocked
The execution policy I am using to invoke Powershell is Bypass
I recommend navigating to your servers "Services" select the Jenkins service and you can assign a specific user. As a test try giving Jenkins administrator access and if that works create another user and restrict access. The fact that it runs fine outside of the Jenkins infrastructure is an indicator for this issue.
Services: (Search for services in the start menu or the common location: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools)
Navigate to the Jenkins Service
Open the properties
Select the Log in tab and set it to a user with proper permissions (E.g: administrator)

"Method Not found" error when calling GetElementsbyClassName

I created a PowerShell automation script and I gave it to my friend when he run it, it said
Method invocation failed because [mshtml.HTMLBodyClass] does not contain a method
named 'getElementsByClassName'.
At C:\Users\עמית\Documents\asaf.ps1:22 char:3
+ $a=$docs.body.getElementsByClassName("FadeOut-Scroll")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
why is that happened?
We both have IE11, .net 4.5, Visual studio, but I have that function and he not.
And it looks like here in his computer the IE console have the function:
but PowerShell does not:
How to update PowerShell?
Found after alot of time:
the missing want microsoft core xml

ADAL in Azure Automation: Type not loading intermittantly

I'm working on an Azure Automation script where I need to retrieve an access token to call the AAD Graph API. I wanted to use ADAL to do this so I zipped up Microsoft.IdentityModel.Clients.ActiveDirectory.dll and uploaded it as a module. When I run from the test blade, it sometimes works, and sometimes fails with this error:
New-Object : Cannot find type [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]: verify that the
assembly containing this type is loaded.
At line:22 char:9
+ $cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredent ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
When it fails I just run it again and it works. Now I've published and scheduled this runbook as a daily job 3 days ago. So far it has failed every day with this same error.
Has any one else seen this? Any suggestions on next steps?
I fixed this by adding the following line to my script:
Add-Type -Path "C:\Modules\User\Microsoft.IdentityModel.Clients.ActiveDirectory\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
Still not sure why it was working intermittently before. I guess depending on what else was going on ADAL may or may not have been loaded?