How do I determine if Anonymous Access is enabled in IIS 6 using PowerShell? - iis-6

I hve several IIS 6 web sites that I need to survey. One of the items that I need to check is AllowAnonymous. I need to know if it is True or False. I found this bit of code:
$a = gwmi -Namespace root\MicrosoftIISv2 -Class IIsWebServerSetting -ComputerName
$ServerName -Impersonation Impersonate -Authentication PacketPrivacy |
Select-object AllowAnonymous.AllowAnonymous
That gives me this:
PS> $a | gm
TypeName: Selected.System.Management.ManagementObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AllowAnonymous.AllowAnonymous NoteProperty AllowAnonymous.AllowAnonymous=null
How do I get the NoteProperty into a variable for my report?

Okay, I'm going to infer that you already know how to read the value of a property of an object by adding a . and the property name, but you're running into difficulties with this oddly-named property because if you assign $a.AllowAnonymous.AllowAnonymous to a variable, the value is always null.
The reason is that the property name contains a dot, which is the property/method operator. So, PowerShell doesn't interpret $a.AllowAnonymous.AllowAnonymous as "read the AllowAnonymous.AllowAnonymous property of the object $a; it interprets it as "read the AllowAnonymous property of the object contained in the AllowAnonymous property of $a. In other words, it first evaluates $a.AllowAnonymous (which is null, because $a doesn't have a property named AllowAnonymous), and then attempts to read the AllowAnonymous property of the null result, which is of course also null.
The solution is simple. Quote the property name:
$SomeVariable = $a.'AllowAnonymous.AllowAnonymous'

Related

What are the PowerShell “special” variables and characters

This question is to help clear up some confusion I have with variables in PowerShell. I'm familiar with the concept of creating a variable and assigning it a value.. for example, in SharePoint, you would do this: $spsite = http://site
Yet, there are a lot of variables that come from PowerShell that can be used in various ways.
For example, if I set a site collection in a variable $spsite (like I did above), I can do a foreach loop on it and iterate through each web site..
foreach ($web in $spsite)
In another example, I see something like this
$a = 5
$b = 6
$c = 7
$d = $a,$b,$c
Foreach ($i in $d)
{ $i + 5}
In both cases, I have not assigned a value to $i or $web.. yet they have some kind of meaning or value. I get that $web is each website in a site collection. But how does PowerShell know this? It just does?
Can someone explain this and/or send me a link to where this is explained?
Also is there a list of all these special variables and special characters (shortcuts) in PowerShell.. for example, I believe % is the foreach-object cmdlet
$i and $web aren't special variables. They are just popular names that people reuse. The name of the current object in a foreach-loop is defined by you.
Ex:
#Creating a simple array for the demonstration
$MyArrayOfAwesomeObjects = 1,2,3
foreach ($ThisVariableIsAwesome in $MyArrayOfAwesomeObjects) {
#Here, $ThisVariableIsAwesome is the current object, and we can call methods on it.
$ThisVariableIsAwesome.GetType()
}
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
True True Int32 System.ValueType
True True Int32 System.ValueType
As for % -> Foreach-Object, that is just an alias. You can view all aliases with Get-Alias

Get a name of a method parameter using Javassist

I have a CtMethod instance, but I don't know how to get names of parameters (not types) from it. I tried getParameterTypes, but it seems it returns only types.
I'm assuming it's possible, because libraries I'm using don't have sources, just class files and I can see names of method parameters in IDE.
It is indeed possible to retrieve arguments' names, but only if the code has been compiled with debug symbols otherwise you won't be able to do it.
To retrieve this information you have to access the method's local variable table. For further information about this data structure I suggest you to check section 4.7.13. The LocalVariableTable Attribute of the jvm spec. As I usually say, JVM spec may look bulky but it's an invaluable friend when you're working at this level!
Accessing the local variable table attribute of your ctmethod
CtMethod method = .....;
MethodInfo methodInfo = method.getMethodInfo();
LocalVariableAttribute table = methodInfo.getCodeAttribute().getAttribute(javassist.bytecode.LocalVariableAttribute.tag);
You now have the the local variable attribute selected in table variable.
Detecting the number of localVariables
int numberOfLocalVariables = table.tableLenght();
Now keep in mind two things regarding the number in numberOfLocalVariables:
1st: local variables defined inside your method's body will also be accounted in tableLength();
2nd: if you're in a non static method so will be this variable.
The order of your local variable table will be something like:
|this (if non static) | arg1 | arg2 | ... | argN | var1 | ... | varN|
Retriving the argument name
Now if you want to retrieve, for example, the arg2's name from the previous example, it's the 3rd position in the array. Hence you do the following:
// remember it's an array so it starts in 0, meaning if you want position 3 => use index 2
int frameWithNameAtConstantPool = table.nameIndex(2);
String variableName = methodInfo.getConstPool().getUtf8Info(frameAtConstantPool)
You now have your variable's name in variableName.
Side Note: I've taken you through the scenic route so you could learn a bit more about Java (and javassists) internals. But there are already tools that do this kind of operations for you, I can remember at least one by name called paranamer. You might want to give a look at that too.
Hope it helped!
If you don't actually want the names of the parameters, but just want to be able to access them, you can use "$1, $2, ..." as seen in this tutorial.
It works with Javaassist 3.18.2 (and later, at least up to 3.19 anyway) if you cast, like so:
LocalVariableAttribute nameTable = (LocalVariableAttribute)methodInfo.getCodeAttribute().getAttribute(LocalVariableAttribute.tag);

Getting a method parameters in Smalltalk

How to print the parameters of a specific method?
Parameters don't have types so you can't print those. It seems that you want to print the method header which provides the name of the method and the names of the parameters. You already have the name of the method so you need to get the names of the parameters. The only way to get the names of the parameters is from the original source string. Here's a nice way to extract the parameter names from the source string. In this example, I'm getting the name of the parameter of the printOn: method in class Object.
(Parser new parse: (Object sourceCodeAt: #printOn:) class: Object) arguments
collect: [:each | each name]
In Pharo3 it is even easier:
(Object>>#printOn:) argumentNames

How can i use powershell embedded parameters?

$b = "a00000"
Now due to a complicated application, i want to access $a00000.ID
The application stores a return object inside a variablename (which is a string value of another)
When i try "$b" it shows the value, but $"$b".ID is an error
${$b}.ID is also an error
$'"$b"'.ID is also an error
How do i access value of $a00000.ID given that previously $b=a00000 ?
(one variable name is assigned by a previous string value)
Give this a try:
(Get-Variable $b -ValueOnly).Id

ConstructorArguments without magic strings

If I want to specify a constructor argument I need to specify the argument name as string. Unfortunately, this is not very refactoring friendly. Is there any way to get around this limitation?
See http://www.planetgeek.ch/2011/05/28/ninject-constructor-selection-preview/ . The next release of Ninject will support to type safely define constructor arguments.
Do:
string s = "my string"
kernel.Bind<IMyInterface>().ToConstructor(x => new MyObject(s));
where MyObject implements IMyInterface.