System.Web.HttpServerUtility.MapPath(String path) - server.mappath

I am using System.IO.File.Exists(Server.MapPath(path)) in my code. Some time when a character (:) colon is coming then Server.MapPath is throwing an exception that System.Web.HttpException: '/MyWeb/pages/javascript:void(0).aspx' is not a valid virtual path.
How do i suppress this error and still call Server.MapPath()
Regards...

Related

Casting Error In Powershell(System.Int32)

I have a problem.
Firstly, There is a MSSQL Database and I sent to query with using PowerShell code. Then I take numbers from these tables (like U056258). After that, I want to mail the people have these numbers. But Powershell sends me an error. Like;
**Cannot convert value "U056258" to type "System.INT32". Error: "Input string was not in a correct format."**
**+CategoryInfo :InvalidArgument: (:) [], RuntimeException**
**+FullyQualifiedErorId :InvalidCastFromStringToInteger**
What is the "U" at the beginning ? Is it an unsigned number ? Remove it.
Ah, your ID isalphanumeric. Guy, don't cast into int32. Or remove the "U" before.

You cannot call a method on a null-valued expression - general

You create a script, it works for some time, then out of the blue it starts crashing with "You cannot call a method on a null-valued expression" or "The property 'property name' cannot be found on this object. Verify that the property exists and can be set.". What does this mean?
This is a Powershell version of "null pointer exception". This exception arises every time you attempt to query a variable that appears to be null. To determine what variable is null and where, you need to read the stack trace and the line/symbol numbers of the line in question. An example:
You cannot call a method on a null-valued expression.
At E:\temp\testsest.ps1:35 char:12
+ If($Search.value() -contains $SearchString)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Let's parse the error message. First, there is a wording that is in the title of this question. If you are about to ask a question with this wording, you will get a set of similar questions proposed by StackOverflow. But there is more in the error description. Second line shows script, line and character number of the first character of an expression that generates this exception. Here, a request is made to $Search.value() querying if it -contains $SearchString. The wavy underline separates the expression in full, although the proper way would be underlining only $Search.value(). Next, there is a CategoryInfo and FullyQualifiedErrorId, the latter saying "Invoke method on null", omitting "pointer" or "variable".
Now, let's debug the message. Here, the only method that's about to be called is value(), this means $Search is equal to null. Therefore, we need to get upwards from the line 35 of the script and find a place where a value is last assigned to the variable in question. This particular script had a query to Range.Find() which returns null if there's no match to the searched string. An excerpt:
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $true
$ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath)
$ExcelWorkSheet = $Excel.WorkSheets.item("$location")
$Range = $ExcelWorkSheet.Range("A1").EntireColumn
$Search = $Range.find($user) # <<< here we get null
If($Search.value() -contains $user)
So, we have found where do we receive a null.
Remedies vary, but all include checks against $null. In this case it's enough to check $Search for null, and return "Nothing found" if it is indeed null. It might be not as simple, there might be more structures that can be null, like in $a.b.c.someMethod() - here either $a, $a.b or $a.b.c is null, so you need to check all of the outcomes. There are also situations where a complex structure is returned, and is expected to have a value in a certain field, but the field is not populated, therefore trying to use that field's value will produce an exception.
The moral is: If you receive an exception speaking about "null-valued", you have not expected something to return null, and you have to add checks for null (or, in fact, any unexpected) values before attempting to use the result.

Querying Active Directory user information using Powershell - seemingly equivalent syntax, different results?

I have a simple Powershell function to perform an Active Directory LDAP lookup based on the SID of a user:
function SidToAdUser($sid) {[adsi]("LDAP://<SID=" + $sid + ">")}
If I wish to read an attribute from the returned User object, accessing it via an intermediary variable works fine:
$ad = SidToAdUser("S-1-5-21-968173855-142910291-87512543-670313")
$ad.department
However, attempting to access it directly from the return value of the function, like this:
SidToAdUser("S-1-5-21-968173855-142910291-87512543-670313").department
elicits an error:
format-default : The following exception occurred while retrieving member "distinguishedName": "An invalid dn syntax has been specified.
"
+ CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
Can anyone advise why exactly this would be the case, and how to correct it?
Thank you.
Your function call syntax is wrong.
(SidToAdUser S-1-5-21-968173855-142910291-87512543-670313).department
In powershell, function arguments are specified as space-separated values after the function name, not enclosed in parens.

Decode HResult = -2147467259

Can someone help me decode this HResult? What does it mean? I know the negative stands for a failure. How about the rest of the 10 bits?
I referenced MSDN HResult article here, but I am not sure how to determine what my facility and code bits are.
More info:
_message: "External component has thrown an exception."
Data: {System.Collections.ListDictionaryInternal}
I'll show you how to do it. Paste the negative number into Calculator (Windows) in programmer mode "Dec" setting. Then convert to "Hex" setting. You get the number: FFFFFFFF80004005. The error is 80004005 which is:
0x80004005
E_FAIL
Unspecified
Unfortunately the provider of the function that gave you this error did not categorize the error.
Useful links:
MSDN - HRESULT Format
MSDN - HRESULT Error List
Another way to do it is as follows. An HRESULT should contain a System Error Code in its first 32 bits. Using an AND operation will retrieve the error code from the HRESULT:
int result = (-2147467259 & 0xFFFF)
result is 16389, which is not a part of the System Error Codes list, and as a result, is unspecified.
Print it as an hexadecimal number, then, use for instance, VisualStudio ErrorLookup, to get the message.
-2147467259 in decimal is 80004005 in hexadecimal (usually rendered as 0x80004005). That's "E_FAIL (Unspecified error)" in Win32.
Not a very helpful error code, but maybe it'll get you a half-step closer to a solution.

How to get some content from xml file by powershell?

Variable $returnedxml is a sql query result forming in xml. I need to get content 'releasepath'\\sharespace\test1\\10.0.1212.00from it
<ReleasePath>\\sharespace\test1\\10.0.1212.00</ReleasePath>
Here are my code:
$xmldoc= new-object xml.xmldocument
$xmldoc.load($Returnedxml)
$xmldoc.releasepath
Here are the returned error alarm:
Exception calling "Load" with "1" argument(s): "Could not find file 'C:\Users\admin\System.Xml.XmlDocument'."
At D:\connecttods3andinvoke.ps1:47 char:14
+ $xmldoc.load <<<< ($Returnedxml)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
I thought xml.xmldocument is a .net class, seems that I was wrong. So what can I do then?
Since the handling of XML data is so integral to so many management tasks, Powershell has an XML Type Accelerator. So this would work as well:
[xml]$xmldoc = $returnedxml
$xmldoc.releasepath
I just read it into a string ...
$file = [IO.File]::ReadAllText($filename)
then use xpath to get values out of it ...
$releasePath = $file | SelectXml "//ReleasePath"
XPath is really powerful for pulling things out of an XML file, much simpler (coding wise) than using xmldoc
Your variable $Returnedxml must be a file name with absolute path. But currently it is an object of class System.Xml.XmlDocument.
So change your variable and then you can read the file.
Or on the other hand if you already have an object of XmlDocument in $Returnedxml then you do not have to read it into $xmldoc. Both are from the same class. Just use $Returnedxml
You are using the wrong load; that one is for files. Use LoadXML instead:
$xmldoc.LoadXml($Returnedxml)