I thought substring is easy until I tried:
# extract from a webpage with regex into $mmdd the value like 08/09
Write-Host $mmdd
08/09
$mmdd.Substring(0,2)
Then I get:
Method invocation failed because [System.Double] does not contain a method named 'Substring'.
At line:1 char:1
+ $test.Substring(0,2)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
> $test.Substring(3)
Method invocation failed because [System.Double] does not contain a method named 'Substring'.
At line:1 char:1
+ $test.Substring(3)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
How do I cast the variable $mmdd to string so I can use Substring() on it?
I suspect I have to do something to the following:
$page | Select-String -Pattern $mmddyyyyRgx -AllMatches |
% { $_.Matches } |
% { $_.Value } |
Select-String -Pattern $mmddRgx -AllMatches |
% { $_.Matches } |
% { $_.Value } -Outvariable mmdd
Since $mmdd gave me:
0.888888888888889
it's a double balue, casting like below will not work as expected:
[string]$mmdd.Substring(0,2)
it will give me
0.
instead of
08
Just call the ToString() method on $mmdd so you can use SubString():
$mmdd.ToString().SubString(0,2)
However, maybe you tell us about your actual problem since this could be a XY Problem.
Related
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
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
I need to replace a line in a .ini file.
Currently my script looks like this...
clear
$file= "D:\sqr.ini"
$path="c:\bin\TestPWProv_VC;"
$newpath="c:\myprod\sqlrs\test;"
get-content $file |
Foreach-Object { $_ -replace $path,$newpath } | Set-Content D:\result.ini
But this is giving me an error of
Invalid regular expression pattern: c:\bin\TestPWProv_VC;.
At line:7 char:38
+ Foreach-Object { $_ -replace <<<< $path,$newpath } | Set-Content D:\result.ini
+ CategoryInfo : InvalidOperation: (c:\bin\TestPWProv_VC;:String) [], RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression
Invalid regular expression pattern: c:\bin\TestPWProv_VC;.
At line:7 char:38
+ Foreach-Object { $_ -replace <<<< $path,$newpath } | Set-Content D:\result.ini
+ CategoryInfo : InvalidOperation: (c:\bin\TestPWProv_VC;:String) [], RuntimeException
+ FullyQualifiedErrorId : InvalidRegularExpression
Could you offer some advice please as to why this isn't working, any help is greatly appreciated.
This is the .ini file
[INFO] PROVIDER=;
OUTPUT=c:\bin\TestPWProv_VC.ini;
TABLENAME=;
OUTPUT=c:\bin\TestPWProv_VC.ini;
DEFAULTQUERY=;
VERSION=1,50,3518,0;
TABLE=Privlib;
OUTPUT=c:\bin\TestPWProv_VC.ini;
PROVIDER=TestPWProv_VC;
TABLENAME=Privlib;
In Future please try researching before just simply asking a question, i'm sure others won't be as straightforward in the future.
$file= "D:\sqr.ini"
$newpath="c:\myprod\sqlrs\test;"
(Get-Content $path) -replace 'OUTPUT=.+', ("OUTPUT=$newpath") | Set-Content $path
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.
I have a simple script that assigns a drive letter to any unlettered partition, like the following:
function GetNextAvailableLetter
{
#returns an unused char for drive letter assignment, or $null if none are available
}
foreach ($disk in ( get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null } ) )
{
$letter = GetNextAvailableLetter
if ( $letter -ne $null )
{
$disk.DriveLetter = $letter + ":"
$disk.Put()
}
}
Oddly, sometimes it'll work, and sometimes Put() throws an exception:
Exception calling "Put" with "0" argument(s): "Not supported"
I have no idea why Put() would throw.
I made a couple of empty, driveletterless drives on my computer and was able to recreate this and one other error that I think you might have neglected to mention.
Property 'DriveLetter' cannot be found on this object; make sure it exists and is settable.
At line:2 char:1
+ $disk.DriveLetter = "Q:"
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Exception calling "Put" with "0" argument(s): "Access is denied.
"
At line:3 char:1
+ $disk.Put()
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
The way to solve this would be run the variable $disk though for each loop or something along those lines. Another approach would be to check the $disk.Count ahead of time.
$disk = get-wmiobject -class win32_volume | where-object { $_.DriveLetter -eq $null }
If (($disk) -and ($disk.Count -eq 1)){
$disk.DriveLetter = "Q:"
$disk.Put()
}
The If should in theory protect you from errors when $disk is empty or returns more that one object.
According to the Scripting Guys:
The reason for this error is that the Windows PowerShell prompt is not running with Administrator rights. Unfortunately, the error that bubbles back up from WMI does not tell us that the problem is related to rights.