windows powershell open website and login exits with NULL-VALUED - windows

I want to login to a website with powershell commands in windows with following code:
$username = "hansmueller#test.com"
$password = "test_21"
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate("http://testsite.test")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementById('name')
$usernamefield.value = $username
$passwordfield = $ie.document.getElementById('pw')
$passwordfield.value = $password
It opens the website but doesnt login and gives me the error:
You cannot call a method on a null-valued expression.
At line:1 char:1
+ $ie.Document.getElementById("name").value = $username;$ie.D ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:1 char:65
+ ... = $username;$ie.Document.getElementById("pw").value=$pass ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Related

unpin apps from taskbar and start menu using powershell

In Windows 11, using PowerShell, I am trying to unpin Microsoft Store from the taskbar and ESPN & Spotify from the start menu.
function unpin_taskbar([string]$appname) {
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() |
Where-Object{$_.Name -eq $appname}).Verbs() | Where-Object{$_.Name.replace('&','') -match 'Unpin from taskbar'} | ForEach-Object{$_.DoIt()}
}
function unpin_startmenu([string]$appname) {
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() |
Where-Object{$_.Name -eq $appname}).Verbs() | Where-Object{$_.Name.replace('&','') -match 'Unpin from Start'} | ForEach-Object{$_.DoIt()}
}
foreach ($taskbarapp in 'Microsoft Store') {
Write-Host unpinning $taskbarapp
unpin_taskbar("$taskbarapp")
}
foreach ($startmenuapp in 'ESPN', 'Spotify') {
Write-Host unpinning $startmenuapp
unpin_startmenu("$startmenuapp")
}
Microsoft Store is unpinned successfully, but the next two fail. This is the output.
unpinning Microsoft Store
unpinning ESPN
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\repo\general\rundeck\windows\unpin_windows_apps.ps1:7 char:5
+ ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
unpinning Spotify
You cannot call a method on a null-valued expression.
At C:\Users\Administrator\Desktop\repo\general\rundeck\windows\unpin_windows_apps.ps1:7 char:5
+ ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
What I'm I doing wrong here?
are you looking at something like this by any change?
Install-Module -Name Microsoft.PowerShell.StartMenu
Import-Module Microsoft.PowerShell.StartMenu
Remove-StartAppsTaskbarPinned -AppName "Microsoft Store"
Remove-StartAppsStartMenuPinned -AppName "ESPN"
Remove-StartAppsStartMenuPinned -AppName "Spotify"
You will need to install the module "Microsoft.PowerShell.StartMenu", this is mandatory to run those commands.
You can look at the powershell gallery to find out more what this is about and how it works.

How do I download a file using windows command line without having to input a proxy password?

After looking at various stackoverflow questions, I found several ways to download a file from a command line without interaction from the user.
The only one that worked for me also works only on Windows 10 natively :
curl -sko %TEMP%\file.txt "https://some.hostname/file.txt"
But installing an external tool like wget/curl is what I want to avoid.
What didn't work for me because of proxy errors :
Command:
bitsadmin.exe /transfer "dljob" "https://some.hostname/file.txt" %TEMP%\file.txt
Error:
DISPLAY: 'dljob' TYPE: DOWNLOAD STATE: ERROR
PRIORITY: NORMAL FILES: 0 / 1 BYTES: 0 / UNKNOWN
Unable to complete transfer.
ERROR FILE: https://some.hostname/file.txt -> E:\Users\xxx\AppData\Local\Temp\file.txt
ERROR CODE: 0x80190197
ERROR CONTEXT: 0x00000005
Command:
powershell -Command "(New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', '%TEMP%\file.txt')"
Error:
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."
At line:1 char:1
+ (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Command:
powershell -Command "Invoke-WebRequest 'https://some.hostname/file.txt' -OutFile %TEMP%\file.txt
Error:
Invoke-WebRequest :
Authentication required
You must be authenticated to access this URL.
...
Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) WindowsPowerShell/5.1.17763.1007
At line:1 char:1
+ Invoke-WebRequest 'https://some.hostname/file.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
This didn't work either :
powershell -Command "$client.Credentials = Get-Credential; $browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', 'file.txt')"
Error :
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Get-Credential : Cannot process command because of one or more missing mandatory parameters: Credential.
At line:1 char:23
+ $client.Credentials = Get-Credential; $browser.Proxy.Credentials =[Sy ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Credential], ParameterBindingException
+ FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.PowerShell.Commands.GetCredentialCommand
The property 'Credentials' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:39
+ ... Credential; $browser.Proxy.Credentials =[System.Net.CredentialCache]: ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (407) Proxy
Authentication Required."
At line:1 char:124
+ ... redentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Refer to this question Access web using Powershell and Proxy
You can try something like that in Powershell and suppose that you have already created a folder named as C:\Test:
$url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
$file = "C:\Test\" + $url.Split("/")[-1]
$wb = New-Object System.Net.WebClient
$wb.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wb.DownloadFile($url,$file)
EDIT : 14/08/2020 #17:08
I tried this on Windows Powershell ISE and it works 5/5 :
cls
$start_time = Get-Date
$url = "https://cdn2.unrealengine.com/Fortnite%2FBoogieDown_GIF-1f2be97208316867da7d3cf5217c2486da3c2fe6.gif"
$Folder = "$Env:Temp\DownloadFolder"
# We create a SubFolder Named "DownloadFolder" in the temporary file %Temp% if it doesn't exists yet !
If ((Test-Path -Path $Folder) -eq 0) { New-Item -Path $Folder -ItemType Directory | Out-Null }
# We can get the name of the file to be downloaded from the variable $url
# $url = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
# In our case the FileName will be = "googlelogo_color_272x92dp.png" or
# Fortnite%2FBoogieDown_GIF-1f2be97208316867da7d3cf5217c2486da3c2fe6.gif
$file = $Folder+ "\" + $url.Split("/")[-1]
Try
{
$wb = New-Object System.Net.WebClient
$wb.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials
$wb.DownloadFile($url,$file)
# better use Invoke-Item $Folder instead of ii
Invoke-Item $Folder
Write-Output "Running Script Time taken is : $((Get-Date).Subtract($start_time).Milliseconds) millisecond(s)"
}
Catch
{
Write-Host "Error from $url" `n"Message: [$($_.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
}
This worked for me :
powershell -Command "[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy(); [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials; (New-Object Net.WebClient).DownloadFile('https://some.hostname/file.txt', 'file.txt')"

Trying to automate filling input fields of web page a with powershell. But stuck with errors

I am trying to fill the input fields with powershell but facing these issues.
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate("https://mxtoolbox.com/blacklists.aspx");
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }
($ie.document.getElementsByName("ctl00$ContentPlaceHolder1$ucToolhandler$txtToolInput") |select -first 1).value ="99.99.99.999";
$ie.Document.getElementsByName("ctl00$ContentPlaceHolder1$ucToolhandler$btnAction").click()
while ($ie.Busy -eq $true) { Start-Sleep -Seconds 1; }
Output :
The property 'value' cannot be found on this object. Verify that the
property exists and can be set. At line:5 char:1
+ ($ie.document.getElementsByName("ctl00$ContentPlaceHolder1$ucToolhand
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound Method invocation failed because [System.__ComObject] does not contain a method named
'click'. At line:6 char:1
+ $ie.Document.getElementsByName("ctl00$ContentPlaceHolder1$ucToolhandl
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (click:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

"Unspecified Error" when calling getElementById()

I am getting this error when executing the below script. Output for the same is also mentioned. I did check for error but dint find any fix. I am using Windows 10 with PowerShell version: 5.0.10586.0. Request anyone's assistance in resolving through this error. I can only see the IE opening with the mentioned URL and the excutable file Notepad. Script unable to perform auto login.
Output:
PS C:\WINDOWS\system32> C:\Users\admin\Desktop\Test.ps1
Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:35 char:1
+ $IE.Document.getElementById(“Email”).value = $Username
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:36 char:1
+ $IE.Document.getElementById(“signIn”).Click()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:37 char:1
+ $IE.Document.getElementByID(“Passwd”).value=$Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Unspecified error.
At C:\Users\admin\Desktop\Test.ps1:38 char:1
+ $IE.Document.getElementById(“signIn”).Click()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Script:
# Edit this to be the URL or IP address of the site to launch on login
$Url = "www.gmail.com"
# Edit this to be the username
$Username= "xxxx#gmail.com"
# Edit this to the corresponding password
$Password= "xxxxx"
# Edit this to be the path to the executable. Include the executable
# file name as well.
$Executable = "c:\windows\system32\notepad.exe"
# Invoke Internet Explorer
$IE = New-Object -com internetexplorer.application;
$IE.Visible = $true;
$IE.Navigate($url);
# Wait a few seconds and then launch the executable.
while ($IE.Busy -eq $true) {
Start-Sleep -Milliseconds 5000;
}
# The following UsernameElement, PasswordElement, and LoginElement need
# to be modified first. See the notes at the top of the script for more
# details.
$IE.Document.getElementById("Email").value = $Username
$IE.Document.getElementById("signIn").Click()
$IE.Document.getElementByID("Passwd").value=$Password
$IE.Document.getElementById("signIn").Click()
while ($IE.Busy -eq $true) {
Start-Sleep -Milliseconds 5000;
}
Invoke-Item $Executable
If your mail address is not valid, you will never get the Password field, but an error message.
For me it is working.

powershell behavior when host file handle is closed

I'm looking for an explanation (preferably documentation) of the following madness.
Say I have the following simple Perl script:
use strict;
use warnings;
my $output = "C:\\Temp\\aout.txt";
my $outpute = "C:\\Temp\\aoute.txt";
my $command = "powershell C:\\test.ps1";
close STDOUT;
close STDERR;
if ( (my $pid = fork()) == 0 ) {
open (STDOUT,">>$output") or die "cannot open $output as stdout: $!";
open (STDERR,">>$outpute") or die "cannot open $outpute as stderr: $!";
exec $command or die "couldn't exec $command: $!";
} else {
my $ret = waitpid($pid,0);
}
And that the Powershell script contains:
write-output "yay1";
write-error "nay2";
write-output "yay3";
write-error "nay4";
write-output "yay5";
write-error "nay6";
write-output "yay7";
write-error "nay8";
write-host "done!";
If I run the Perl script, it produces two files, as expected, with the following output:
aout.txt:
yay1
yay3
yay5
yay7
done!
aoute.txt:
C:\test.ps1 : nay2
At line:1 char:41
+ C:\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
C:\test.ps1 : nay4
At line:1 char:41
+ C:\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
C:\test.ps1 : nay6
At line:1 char:41
+ C:\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
C:\test.ps1 : nay8
At line:1 char:41
+ C:\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
If I modify the Perl script to keep STDERR closed before executing Powershell:
use strict;
use warnings;
my $output = "C:\\Temp\\aout.txt";
my $command = "powershell C:\\test.ps1";
close STDOUT;
close STDERR;
if ( (my $pid = fork()) == 0 ) {
open (STDOUT,">>$output") or die "cannot open $output as stdout: $!";
exec $command or die "couldn't exec $command: $!";
} else {
my $ret = waitpid($pid,0);
}
Then it produces one STDOUT file with all of the output:
yay1
C:\\test.ps1 : nay2
At line:1 char:41
+ C:\\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
yay3
C:\\test.ps1 : nay4
At line:1 char:41
+ C:\\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
yay5
C:\\test.ps1 : nay6
At line:1 char:41
+ C:\\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
yay7
C:\\test.ps1 : nay8
At line:1 char:41
+ C:\\test.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
done!
Is Powershell really realizing that STDERR is closed, and is doing a clean redirect onto STDOUT? Or is it the command line console? Or is it Perl?
For reference, this is on Windows Server 2003 x64 SP2 with:
C:\>perl --version
This is perl, v5.8.8 built for MSWin32-x64-multi-thread
(with 33 registered patches, see perl -V for more detail)
Copyright 1987-2006, Larry Wall
Binary build 819 [267479] provided by ActiveState http://www.ActiveState.com
Built Aug 27 2006 22:13:23
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
C:\>powershell get-host
Name : ConsoleHost
Version : 2.0
InstanceId : 1ac67afa-f020-414c-b47b-031bdc8cc703
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
and this is part of an ongoing effort related to this and this.

Resources