I've been trying to write a program to access properties and methods of a 3rd party OLE DLL.
Below code runs fine.
[System.__ComObject].InvokeMember("AppName", [System.Reflection.BindingFlags]::GetProperty, $null, $appObj, $null)
As the invocation is going to be repetitive, I want to call a wrapper like below.
function Get-Property {
param(
$objOLE,
[String] $propertyName
)
[System.__ComObject].InvokeMember($propertyName,[System.Reflection.BindingFlags]::GetProperty,$null,$objOLE,$null)
}
When the script runs
Get-Property($appObj, "AppName")
I got this error:
Exception calling "InvokeMember" with "5" argument(s): "Method 'System.__ComObject.ToString' not found."
At F:\Scripts\test.ps1:21 char:36
+ [System.__ComObject].InvokeMember <<<< ($propertyName,[System.Reflection.BindingFlags]::GetProperty,$null,$objOLE,$null)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
This is confusing. Anybody has an insight? Thank you in advance.
Remember your function is a PowerShell function/command and not a .NET method i.e. don't use parens and don't comma separate args:
Get-Property $appObj AppName
The way you had it, your function gets one argument that is an array with two elements.
Related
Everyone.
I'm not sure why this is happening, but in my PowerShell script I am converting a string (server name entered by the end user) to uppercase. My trouble is that when I run my script it throws the error "You cannot call a method on a null-valued expression." on the open/close parentheses of the *.ToUpper(). As far as I know using ToUpper it in this case should just shift the case.
Here's an example:
$Server = $Server.ToUpper()
And the output:
You cannot call a method on a null-valued expression.
At C:\TEMP\Servers.ps1:705 char:2
+ $Server = $Server.ToUpper()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Now, I have tried to just convert to uppercase on whatever action I am calling, for instance:
$GP = Get-printer -ComputerName $Server.ToUpper()
The above also fails on the "()".
But it doesn't seem to be happening all the time. I am converting in numerous places and it's working as expected, but sporadically in the script it's throwing errors.
I've tried $Server = $Server.ToUpper() and $Server = $Server.ToUpper -- and I get mixed results of success and errors.
I'm betting I'm missing something small with this. Any advice on how to resolve?
Thanks,
Dale
I have a script as below
[string]$newValue = Get-ConfigurationByType $setting.Value
After this line, the value of $newValue is
"http://ST-$($partner.Alias).vardiaforsakring.se/CardPaymentAgreementCallback.aspx"
In the loop, I call ExpandString
foreach ($partner in $partners)
{
$partnerSpecificValue =$ExecutionContext.InvokeCommand.ExpandString($newValue)
}
It throws exception
Exception calling "ExpandString" with "1" argument(s): "Object reference not set to an instance of an object."
At C:\Programs\Drops\Hydra_SE_v1.28\HydraDeploymentFunctions.ps1:342 char:5
+ $partnerSpecificValue = $ExecutionContext.InvokeCommand.ExpandString($newVal ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException
But when I try to input hard-code string, it returns expected result without exception
$partnerSpecificValue =$ExecutionContext.InvokeCommand.ExpandString("http://ST-$($partner.Alias).vardiaforsakring.se/CardPaymentAgreementCallback.aspx")
The value of $partnerSpecificValue is
http://ST-secure.vardiaforsakring.se/CardPaymentAgreementCallback.aspx
Does anyone know a workaround to resolve this bug? Thank you very much. I am running PowerShell v4.0 on Windows Server 2012 R2.
ExpandString is problematic and doesn't work correctly in general case. I use this method instead:
function render() {
[CmdletBinding()]
param ( [parameter(ValueFromPipeline = $true)] [string] $str)
"#`"`n$str`n`"#" | iex
}
Example:
$x=#{test='test'}
'Hashtable x contains value $($x.test)' | render
I have a workaround for this bug.
https://social.technet.microsoft.com/Forums/en-US/5efc52d2-619c-4063-9a47-bc646c45e882/executioncontextinvokecommandexpandstring-throws-exception-in-powershell-40?forum=winserverpowershell
When defining a function, how can you reference a custom enum?
Here's what I'm trying:
Add-Type -TypeDefinition #"
namespace JB
{
public enum InternetZones
{
Computer
,LocalIntranet
,TrustedSites
,Internet
,RestrictedSites
}
}
"# -Language CSharpVersion3
function Get-InternetZoneLogonMode
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[JB.InterfaceZones]$zone
)
[string]$regpath = ("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\{0}" -f [int]$zone)
$regpath
#...
#Get-PropertyValue
}
Get-InternetZoneLogonMode -zone [JB.InternetZones]::TrustedSites
But this gives the error:
Get-ZoneLogonMode : Unable to find type [JB.InterfaceZones]. Make sure that the assembly that contains this type is loaded.
At line:29 char:1
+ Get-ZoneLogonMode -zone [JB.InternetZones]::TrustedSites
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (JB.InterfaceZones:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
NB: I'm aware I could use ValidateSet for similar functionality; however that has the disadvantage of only having a name value; as opposed to allowing me to program using friendly names which are then mapped to ints in the background (I could write code for that; but enum seems more appropriate if possible).
I'm using Powershell v4, but ideally I'd like a PowerShell v2 compatible solution as most users are on that version by default.
Update
I've corrected the typo (thanks PetSerAl; well spotted).
[JB.InterfaceZones]$zone now changed to [JB.InternetZones]$zone.
Now I'm seeing error:
Get-InternetZoneLogonMode : Cannot process argument transformation on parameter 'zone'. Cannot convert value "[JB.InternetZones]::TrustedSites" to type
"JB.InternetZones". Error: "Unable to match the identifier name [JB.InternetZones]::TrustedSites to a valid enumerator name. Specify one of the following
enumerator names and try again: Computer, LocalIntranet, TrustedSites, Internet, RestrictedSites"
At line:80 char:33
+ Get-InternetZoneLogonMode -zone [JB.InternetZones]::TrustedSites
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-InternetZoneLogonMode], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-InternetZoneLogonMode
The ISE gave this one away to me but your attempted syntax was not completely incorrect. I was able to do this and get it to work.
Get-InternetZoneLogonMode -Zone ([JB.InternetZones]::TrustedSites)
Again, If you look at the highlighting you will see how I came to that conclusion.
Per comments by PetSerAl & CB:
Corrected typo in function definition
from [JB.InterfaceZones]$zone
to [JB.InternetZones]$zone
Changed function call
from Get-InternetZoneLogonMode -zone [JB.InternetZones]::TrustedSites
to Get-InternetZoneLogonMode -zone TrustedSites
I am writing a simple PS script with Psake and I have a problem when I try to include another ps1 file.
PS C:\CI> Include .\EnvSettings.ps1
I have this exception
Exception calling "Peek" with "0" argument(s): "Stack empty."
At C:\Users\Julien\Documents\WindowsPowerShell\Modules\psake\psake.psm1:227 char:2
+ $psake.context.Peek().includes.Enqueue(($pa));
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
I had a look at psake.psm1 line 227 to see what's going on around
# .ExternalHelp psake.psm1-help.xml
function Include {
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string]$fileNamePathToInclude
)
Assert (test-path $fileNamePathToInclude -pathType Leaf) ($msgs.error_invalid_include_path -f $fileNamePathToInclude)
$psake.context.Peek().includes.Enqueue((Resolve-Path $fileNamePathToInclude));
}
The code succesfully pass the Assert line.
The problem comes from the Resolve-Path $fileNamePathToInclude, it returns nothing...
If I try it from the command line it works fine.
Anyone experienced this problem before?
The way to include a file is not Include .\EnvSettings.ps1 , but just "dot source" the file :
. .\EnvSettings.ps1
I am not very familiar with powershell scripting and I'm stuck on this problem -
I need to make some operations on object retrieved like this:
$object = [ADSI]'LDAP://CN=Test User,OU=Dept,OU=Users,DC=example,DC=org'
...
$object.Commit()
this works fine, but I have to use distinguished name stored in variable - my test script looks like this, but its not working:
$object = [ADSI]'LDAP://$variable'
...
$object.Commit()
the first call to [ADSI] itself doesn't cause error, but any following operation crashes with message:
The following exception occurred while retrieving member "commit": "The server is not operational.
"
At line:1 char:10
+ $object.commit <<<< ()
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBaseGetMember
I'm pretty sure, that the parameter is sent in some wrong way, but I don't know, how to fix it, can anybody help?
tahnks
Try:
$object = [ADSI]"LDAP://$variable"
Single quotes don't expand variables.