Hi there. I hope you can help me.
First off, I cannot use the RDM Cisco AnyConnect plugin as the particular systems I connect to have additional custom prompts that need to be answered. Instead I have been launching the AnyConnect powershell script utilizing RDM's embedded script function for the past 2 years and it had worked great up until about a month ago. Now I get the following:
Cannot convert argument "hWnd", with value: "System.Object[]", for "SetForegroundWindow" to type "System.IntPtr": "Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
At C:\Users\myusername\AppData\Local\Temp\RDM\72c727ad-4540-4980-828d-7015efc73512.ps1:46 char:10
+ else{[void] [Win]::SetForegroundWindow($h)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
The section of the script causing the issue (line 46 in bold):
{
Start-Process -FilePath $vpncliAbsolutePath -ArgumentList "connect $CiscoVPNHost"
$counter = 0; $h = 0;
while($counter++ -lt 1000 -and $h -eq 0)
{
sleep -m 10
$h = (Get-Process vpncli).MainWindowHandle
}
#if it takes more than 10 seconds then display message
if($h -eq 0){echo "Could not start VPNUI it takes too long."}
else{[void] [Win]::SetForegroundWindow($h)}
}
Running the script manually or running "C:\Users\myusername\AppData\Local\Temp\RDM\72c727ad-4540-4980-828d-7015efc73512.ps1" manually works without error, it is only when invoking via RDM's embedded script session where I see this error.
Any ideas to get this working again?
Thanks!
-Rico
Hi,
I'm not a PowerShell expert but maybe RDM does not use the same version of PowerShell than in external. Could you try to execute the script with a Command Line action instead of a powershell?
David Hervieux
Hi David,
Thanks for the response. Same error with command line action. Come to think of it, this might have very well started to occur after upgrading powershell on my PCs from v2.0 to v3.0. I will try to confirm this sometime next week and develop a workaround.
Cheers!
-Rico
Okay, pretty much confirmed as I can get my script to run via command line action by forcing powershell v2.0:
powershell.exe -version 2.0 "C:\Path\To\MyScript.ps1"
But that doesn't help with embedding the script in the datastore. I don't suppose you'll be implementing a "Force Powershell v2.0" feature in the embedded script action anytime soon, so I'll assume that I'm out of luck with this one. Off to workaroundland for me. :)
-Rico
Just in case anyone else should have this issue in the future, this is what I did to fix it. I know little about PowerShell, but I can Google.
In my embedded script, I added the following to the very start of the script:
If ($host.version -ne "2.0") {
$params = ($PSBoundParameters.GetEnumerator() | % {"-{0} {1}" -f $_.Key,$_.Value}) -join " "
Start-Process -File PowerShell.exe -Argument "-Version 2 -noprofile -noexit -file $($myinvocation.mycommand.definition) $params"
Break
}
This results in the script running without error, however the PowerShell console refused to exit once the connection was completed, so at the very end of the script I added:
stop-process -Id $PID
Now my embedded AnyConnect PowerShell scripts run exactly as they did before I upgraded PowerShell to v3.0.
Big thanks to David for suggesting PowerShell version as a possible culprit!
-Rico
I'm glad you've found a workaround. I will try to add the switch "-version 2.0 " to force the PowerShell version.
David Hervieux
Hello,
We'll have a "Force PowerShell 2.0" option in the next version of RDM. It will be alongside the other options in the powershell entry (both the session type and the macro type).
Regards,
Hubert Mireault
Excellent. Thank you Hubert!
Cheers,
-Rico