Firefox automation using Selenium Powershell ISE, Windows 10 - windows

PS ISE 5.1
Windows 10
Has anyone had luck with Firefox automation using Selenium with PS ISE? Below, I have two different examples below where in each method I am unsuccessfully able to open the firefox browser, let alone navigate anywhere. I'll explain more under each method.
Method 1) Install of Selenium PS Module:
Source:
https://github.com/adamdriscoll/selenium-powershell
In the following method, I installed a module that is supposed to be a powershell wrapper for C# Selenium. The error is triggered from module itself. I have placed the error message in the comment block below. How would I find which "assembly" is needed, that contains this "type"?
cls
$website = "https://www.google.com/"
Import-Module "C:\Program Files\WindowsPowerShell\Modules\Selenium\3.0.1\Selenium.psm1" -Function *
$Driver = Start-SeFirefox
Enter-SeUrl $website -Driver $Driver
Cannot find the type for custom attribute 'ValidateURIAttribute'. Make sure that the assembly that contains this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\Selenium\3.0.1\Selenium.psm1:403
char:9
+ [ValidateURIAttribute()]
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ([ValidateURIAttribute()]:AttributeAst) [], RuntimeException
+ FullyQualifiedErrorId : CustomAttributeTypeNotFound
Cannot find the type for custom attribute 'ValidateURIAttribute'. Make sure that the assembly that contains this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\Selenium\3.0.1\Selenium.psm1:580
char:9
+ [ValidateURIAttribute()]
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ([ValidateURIAttribute()]:AttributeAst) [], RuntimeException
+ FullyQualifiedErrorId : CustomAttributeTypeNotFound
Method 2) Downloaded geckodriver.exe and WebDriver.dll
Sources:
https://www.reddit.com/.../getting_started_in_web.../
https://adamtheautomator.com/selenium-powershell/
https://www.selenium.dev/downloads/
In this example, I have downloaded geckodriver.exe and WebDriver.dll, placed them both in the same folder and added the folder to my system's environment variables. I've added a comment block below showing the error message. I commented out the Add-type call as it gave me different errors and it didn't load the webdriver, so I used the loadform call instead. Which file is the error referencing to? The code is using the DLL file; I know this because FireFoxOptions line of code works and the code opens the exe, so that clearly is found as well. There are only 2 files and both are working to some degree. The line that's not working is
$Firefoxdriver = New-Object OpenQA.Selenium.Firefox.Firefoxdriver($Firefoxoptions)
This would be a function from the dll file; can it not find the EXE at this point, even though the code had already executed the EXE file? Regarding the GAC = false, I read that not all DLL's can be registered with the system because some don't have all the necessary functions; I'm not sure if that's required or not.
cls
$PathToFolder = 'F:\Programs\Selenium\WorkingDirectory'
if ($env:Path -notcontains ";$PathToFolder" ) {
$env:Path += ";$PathToFolder"
}
[System.Reflection.Assembly]::LoadFrom("{0}\\WebDriver.dll" -f $PathToFolder)
#Add-Type -Path "$($PathToFolder)\WebDriver.dll"
$Firefoxoptions = New-Object OpenQA.Selenium.Firefox.Firefoxoptions
#$Firefoxoptions.AddArgument('-headless') ##### <----- Used to make the window not appear, or 'headless' - comment out to have a normal window show.
$Firefoxoptions.AcceptInsecureCertificates = $True
$Firefoxdriver = New-Object OpenQA.Selenium.Firefox.Firefoxdriver($Firefoxoptions)
pause
GAC Version Location
--- ------- --------
False v4.0.30319 F:\Programs\Selenium\WorkingDirectory\WebDriver.dll
New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0,
Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified."
At F:\Programs\PowerShell\SeleniumFireFox.ps1:15 char:18
+ ... foxdriver = New-Object OpenQA.Selenium.Firefox.Firefoxdriver($Firefox ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: 🙂) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Press Enter to continue...:
Here is a snippet of the Java code that works in Eclipse. I wonder if what I'm missing in the code above is whatever the System.setProperty is doing in Java?
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","F:\\Programs\\Selenium\\GeckoDriver\\geckodriver-v0.29.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "https://www.google.com/";
String expectedTitle = "Google";
String actualTitle = "";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
Additional notes (partly just venting): The reason I'm trying to do this in powershell is because I know powershell more; it's generally easier for me to write code. In my example, I am trying to combine two pieces of working code together; one of them is the Firefox automation in Java and the other is an api call in Powershell. My first attempt was to port the powershell code into Java, but I don't know Java well and the littlest things were frustrating me. In Java, I couldn't figure out how to make the API call which was so simple in powershell; simply using Invoke-RestMethod. The answers I found online to do that were saying that it's actually complex in Java because I would need to manage cookies and do all sorts of stuff. I couldn't even find consistent date functions; different answers were importing different date function modules and it made combining code difficult. So, I decided to try and port my Java code to Powershell. The code I'm using works for other people, at least according to the answers I've found online, but do not work for me.

It looks like you are not importing the full module, is this on purpose? If Selenium is in your modules path, you should be able to run:
Import-Module Selenium
If that's not possible, most modules have a .psd1 file that should be the import target, and may load other .psm1 files, nested modules, assemblies, etc. Your github link recommends the following:
Import-Module "{FullPath}\selenium-powershell\Selenium.psd1"
At the very least, you are missing a type definition - these are usually included in .dll or .xml files with the module, and can be seen with get-module once imported under ExportedTypeFiles:
(Get-Module ActiveDirectory).ExportedTypeFiles
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\ActiveDirectory\ActiveDirectory.Types.ps1xml

Related

Can't load executable from memory, VB.NET assembly

So basically, I'm trying to load exe file into memory and run its Main function.
It's coded in VB.NET and it has a Namespace called ConApp and a Module called MyModule
So far I've tried this in powershell:
$path = [path to exe]
$bytes = [IO.File]::ReadAllBytes($path)
[System.Reflection.Assembly]::Load($bytes)
And this is the output:
GAC Version Location
--- ------- --------
False v4.0.30319
So I assume it's loaded here? And then when I try to call it like this:
[ConApp.MyModule]::Main()
I get this error:
Unable to find type [ConApp.MyModule].
At line:1 char:1
+ [ConApp.MyModule]::Main()
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (ConApp.MyModule:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
Any ideas on how could I achieve the execution from memory? Thanks in advance.
The answer was actually simple, I wasn't calling the assemblyname first.
So it should've been:
[ConApp.ConApp.MyModule]::Main()
instead of:
[ConApp.MyModule]::Main()

How install Oracle package for Oracle.ManagedDataAccess.dll in VS Code

I'm trying to install the above package in VS Code. For some reason when I put this at the top of my script I get an error message.
using namespace system.collections.generic
Add-Type -AssemblyName System.Data.OracleClient
Add-Type -Path "C:\Users\me\OneDrive - company\Documents\2021\temp endToEnd\oracle.ManagedDataAccess.Core\oracle.manageddataaccess.core.3.21.50\lib\netstandard2.1\Oracle.ManagedDataAccess.dll"
Error:
Add-Type : Missing an argument for parameter 'AssemblyName'. Specify a parameter of type 'System.String[]' and try again.
I also tried Add-type -Path "C:\Users\me\OneDrive - company\Documents\2021\temp endToEnd\oracle.ManagedDataAccess.Core\oracle.manageddataaccess.core.3.21.50\lib\netstandard2.1\Oracle.ManagedDataAccess.dll"
And it had this error:
Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
My question is, how do I load this, so I can do the following:
$connectionString = "Data Source=$dataSource;User Id=$username;Password=$password;"
$con = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)
I am trying to install this package because I have this error when I try to execute the last code line above:
New-Object : Cannot find type [Oracle.ManagedDataAccess.Client.OracleConnection]: verify that the assembly containing this type is loaded.
I tried doing it through nuget manager as well, with ctrl shift P, nuget manager, but it's not coming up in the list that I can see (odp.net, oracle managed..., etc).
I had download this from the oracle website:
oracle.manageddataaccess.core.3.21.50.nupkg
Then I used 7-zip to unzip it to the location I'm Add-Type from.
I've been looking at these links:
New-object Oracle.ManagedDataAccess.Client.OracleConnection
oracle-developer-tools-vs-code
install nuget package in vs code
I can't seem to get this installed so the command works in the script. Any help would be appreciated.
You're basically treating powershell like a client application, you'll want: the Oracle Data Application Client(ODAC) driver, powershell is a managed memory model so you'll want the managed one, and most likely the 64-bit one unless for some reason you're running 32-bit powershell... Beyond that it'll likely depend on which version works best for your Oracle database.
For example 12cR1:
Download ODP.NET_Managed_ODAC122cR1.zip
extract odp.net\managed\common\Oracle.ManagedDataAccess.dll
PS C:\working> add-type -path (ls .\Oracle.ManagedDataAccess.dll).FullName
PS C:\working> $OraEntry = '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(Host=database.example.com)(Port=1234)))(CONNECT_DATA=(service_name=BigData)))'
PS C:\working> $con = [Oracle.ManagedDataAccess.Client.OracleConnection]::new()
PS C:\working> $con.ConnectionString = "Data Source=$OraEntry;User Id=$username;Password=$password"
PS C:\working> $con.Open()
If you don't know what the OraEntry should be you can likely copy it from your tnsnames.ora and/or check with your database admin
Have a look at How the Runtime Locates Assemblies
Most common .NET assemblies are loaded either from current directory or from Global Assembly Cache (GAC). The GAC takes precedence over locally stored files.
Check files in your downloaded package, there should be the OraProvCfg.exe. Use it for adding the dll into GAC and doing the configuration:
OraProvCfg.exe /action:config /product:odpm /frameworkversion:v4.0.30319 /providerpath:"C:\Users\me\OneDrive - company\Documents\2021\temp endToEnd\oracle.ManagedDataAccess.Core\oracle.manageddataaccess.core.3.21.50\lib\netstandard2.1\Oracle.ManagedDataAccess.dll"

Corrupt PackageManager? Unable to find module providers (PowerShellGet)

This is a common error that is not solved by the common solutions I have found on the internet.
Unable to run Get-PsRepository, Install-Module, and related (OneGet?) commands without them throwing the "Unable to find module providers (PowerShellGet)" error. This appears to be an issue with the NuGet packagemanager module but I do not know how to recover. The package provider installed appears valid
Symptom patterns:
C:> [Net.ServicePointManager]::SecurityProtocol
Tls12
Get-PSRepository
PackageManagement\Get-PackageSource : Unable to find module providers (PowerShellGet).
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4496 char:31
+ ... ckageSources = PackageManagement\Get-PackageSource #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power...etPackageSource:GetPackageSource) [Get-PackageSource
], Exception
+ FullyQualifiedErrorId : UnknownProviders,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackageSource
Register-PSRepository -Default -Verbose
VERBOSE: PowerShell meta provider initialization failed.
VERBOSE: No match was found for the specified search criteria and provider name 'PowerShellGet'. Try
'Get-PackageProvider -ListAvailable' to see if the provider exists on the system.
PackageManagement\Register-PackageSource : Unable to find module providers (PowerShellGet).
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:4211 char:17
+ ... $null = PackageManagement\Register-PackageSource #PSBoundParamete ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power...erPackageSource:RegisterPackageSource) [Register-Pac
kageSource], Exception
+ FullyQualifiedErrorId : UnknownProviders,Microsoft.PowerShell.PackageManagement.Cmdlets.RegisterPackageSource
Install-Module PowerShellGet -Force
PackageManagement\Install-Package : Unable to find module providers (PowerShellGet).
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ... $null = PackageManagement\Install-Package #PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : UnknownProviders,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage
Facts:
Windows 10 (1903)
Powershell 5.1.18362.1171
Running PowerShell as Administrator
Not behind a proxy
Have access to https://www.powershellgallery.com/api/v2
Have access to nuget.org
`[Net.ServicePointManager]::SecurityProtocol = Tls12
FIPS is disabled (HKLM\System\CurrentControlSet\Control\Lsa\FIPSAlgorithmPolicy\Enabled DWORD=0)
Get-PackageProvider shows only NuGet 2.8.5.208
PowerShellGet version is 1.0.0.1 ; when trying force a sxs install with Install-Module PowerShellGet -Force I get the same: PackageManagement\Install-Package : Unable to find module providers (PowerShellGet).
We encountered the same problem. Found this thread without a solution. But I dug a bit further and on the computer that had this problem we found version 1.4.7 of PackageManagement installed.
After removing this folder everything started working normally. I have not found how this thing got installed. Seems like a legit powershell module from Microsoft but it's interacting with the 'normal' PowershellGet module and seems to break it.
For us it was located in the %USERPROFILE%/documents/WindowsPowershell/Modules.
Also worth nothing that this %USERPROFILE% was a corporate OneDrive.
Edit: Microsoft have fixed this issue: https://github.com/PowerShell/vscode-powershell/issues/3432
Not enough Rep to comment.
I ended up in the same situation. A Windows 10 1909 machine, with
%USERPROFILE%/document being a corporate OneDrive. Moving
PackageManagement out of /WindowsPowerShell/ changed
Get-PSRepository's output from nothing to PSGallery. Thanks! – tmcg
Mar 1 at 21:35 This was exactly the same issue. I suspect this is
because PowerShell 7 dumped it into this location (possibly Visual
Studio Code when it prompt to update?) – aolszowka Mar 1 at 22:09
I encountered the same issue with packagemanagement in %USERPROFILE%/documents on my personal profile. The file dates matched when I installed VScode and the Powershell Extensions on this new machine.
Removing the folder from modules corrected the issue for the time being.
In addition to removing %USERPROFILE%/Documents/WindowsPowershell/Modules folder, I also found that putting the package manger locally helped powershell.exe -NoLogo -NoProfile -Command 'Save-Module PackageManagement -MinimumVersion 1.4.7 -LiteralPath ""C:\Program Files\WindowsPowerShell\Modules\""' thanks to itsho here https://github.com/PowerShell/vscode-powershell/issues/2824#issuecomment-750902181
The cause is due to the %USERPROFILE%/Documents folder being on a OneDrive and module-install keeps installing the PackageManagement module to user documents by default ignoring PSModulePath, even had the OneDrive location removed/changed.
This my module path from running PS inside vscode identifying info removed:
$env:PSModulePath -split ';'
C:\Users\{myUsername}\OneDrive - {myCompanyName}\{company}\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
There are more things that can be done to fix this from happening again discussed here https://github.com/PowerShell/PowerShell/issues/15552#issuecomment-1002708799
Like setting the documents folder to local and off OneDrive like shown here https://support.microsoft.com/en-us/topic/configuration-of-the-my-documents-folder-dfd9a90d-8f80-18d6-e7cc-f1566fc3b10b
I followed these steps for now.
In explorer to the left under quick access right click Documents, and then click Properties.
Change to the location tab in the configuration box and type the new path to the Documents folder, I used the local one
Click No so the existing files do not move and still can be accessed by going into OneDrive. I also added another quick access folder for my OneDrive location, but that is up to you.
Note: This effects other programs as well like the default save location for PowerPoint. You'll need to be mindful that you don't save something local thinking it will be backed up to OneDrive.
My module paths after the change:
$env:PSModulePath -split ';'
C:\Users\{myUsername}\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
Another workaround could be to have OneDrive keep the files local.
Navigate to %USERPROFILE%/Documents
Right click WindowsPowershell and select "Always keep on this device".
I haven't been able to test this one, but it might be the least impactful way to address this.

Need help understanding error "manifest is not in the package root" while registering Windows 10 package

I am trying to register the Windows 10 Edge package with Windows Server 2016. I have successfully installed the store thanks to a forum I found and it opens but remains untested. I am trying to do the same with Edge. I've copied the app package from the installer iso to C:\Windows\systemapps and ran the power shell script:
Add-AppxPackage -register "C:\windows\systemapps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\Appxmanifest.xml" -DisableDevelopmentMode
And get the following error:
Add-AppxPackage : Deployment failed with HRESULT: 0x80073CF9, Install failed. Please contact your software vendor.
(Exception from HRESULT: 0x80073CF9)
Rejecting a request to register from Appxmanifest.xml because the manifest is not in the package root.
NOTE: For additional information, look for [ActivityId] a80e1223-2787-0000-dc3e-12a98727d301 in the Event Log or use
the command line Get-AppxLog -ActivityID a80e1223-2787-0000-dc3e-12a98727d301
At line:1 char:1
+ Add-AppxPackage -register "C:\windows\systemapps\Microsoft.MicrosoftE ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\windows\syst...ppxmanifest.xml:String) [Add-AppxPackage], IOException
+ FullyQualifiedErrorId : DeploymentError,Microsoft.Windows.Appx.PackageManager.Commands.AddAppxPackageCommand
I've verified the manifest is in the package root so other than that I can't figure out what it means. Can someone help me understand what it's looking for?
Reference for store install: http://virtualcustoms.net/showthread.php/72904-Install-Microsoft-Store-and-Apps-on-Windows-10-LTSB-2016
I was just trying to do the same thing and I have found if I copy the app to C:\Program Files\WindowsApps then it installs without complaining about not being in the root and then the Edge icon also appears on the start menu.
The command to run is:
Add-AppxPackage -DisableDevelopmentMode -Register "C:\Program Files\WindowsApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AppxManifest.xml"
Whilst this allows the app to install and I am able to launch it from the start menu it closes after about 2 seconds and looking in the log (Application and Service Logs\Microsoft\Windows\Apps\Microsoft-Windows-TWinUI/Operational it shows this error so it looks like there is still something else that needs to be done to get this working:
ActivateApplicationForContractByAppIdAsUserWithHost of the app Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge for the Windows.Launch contract failed with The app didn't start..
Robin
I found it difficult to add to the win apps folder but figured out as long as the location of the apps you want to add are located in a folder named C:\Program Files\WindowsApps* it will work. I use a folder named “WindowsApps-Import” in the programs folder, works well!
One answer suggests adding the -DisableDevelopmentMode flag to the command you've ran, which works, but leaves an important point out.
Once the app installs, it won't run and crashes soon after. This is because the ALL APPLICATION PACKAGES group doesn't have access to the folder of the app. Giving ALL APPLICATION PACKAGES full control of the app folder will allow it to run.
Move the folder inside: "C:\Users$USERNAME\AppData\Local\Packages" then it works. Obv developer mode on windows needs to be ON.
for eg:
Add-AppxPackage -Register "C:\Users\Amit\AppData\Local\Packages\WsaPackage_1.7.32815.0_x64_Release-Nightly\AppxManifest.xml"

Getting "Unable to find type [System.Net.ServicePointManager]." in PowerShell for OS X

I'm trying to debug a script that makes an https call using Invoke-WebRequest. Since it won't allow the request due to certificate errors, I need to run the following command to disable SSL validation:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
However, when I attempt to run this command I get the following error:
Unable to find type [System.Net.ServicePointManager].
At line:1 char:1
+ [System.Net.ServicePointManager]::ServerCertificateValidationCallback ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.ServicePointManager:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
OS: macOS Sierra
To replicate the issue:
Download PowerShell for OS X from https://github.com/PowerShell/PowerShell
Install the .pkg
Type 'powershell' in a terminal
Attempt to run the command above.
Not really familiar with this, but... the PowerShell for OS X link you provide appears to use .NET Core. However, System.Net.ServicePointManager isn't supported by .NET Core.
Digging around, I'm able to find this StackOverflow question, which lead to this issue, and then this issue, but honestly I'm a bit lost on what's going on because the version numbers refer to so many different things. I can't tell what's current and what's not.
Bottom line is, as far as I can tell, the proper method for .Net Core is supposed to be Http​Client​Handler.​Server​Certificate​Custom​Validation​Callback in System.Net.Http, but I'm not exactly sure how you refer to that with PowerShell.
On Windows PowerShell up until version 5, the workaround you are attempting to use was required because the Invoke-WebRequest cmdlet did not have a convenient way to ignore certificate checks.
In PowerShell 6+, because of the prevalence of self signed certificates in the Open Source world, Invoke-WebRequest was upgraded to include a lot of new switch parameters to deal with them. In particular take a look at the -SkipCertificateCheck parameter in the documentation for the cmdlet.
That parameter will allow you to accept an unknown certificate to encrypt your connection as you are attempting to do here.

Resources