How do I use FlaUI to set the value in a specific field after clicking the "Save As" button in Paint?
I'm trying to use FlaUI to automate filling a field in Paint after clicking "Save As". The field in question is named "Name:". Here is the code I have so far:
Add-Type -Path C:\Users\sergi\assemblies\bin\Release\net48\publish\FlaUI.UIA3.dll
$windowTitle = 'Untitled - Paint'
$control = 'Name:'
$automation = [FlaUI.UIA3.UIA3Automation]::new()
$process = get-process | Where-Object {$_.MainWindowTitle -match $windowTitle}
$app = [FlaUI.Core.Application]::Attach( $process )
foreach( $wnd in $app.GetAllTopLevelWindows( $automation ) ) {
$myinput = $wnd.FindAllDescendants() | Where-Object { $_.Name -eq $control }
$myinput[0].SetValue('Value Test')
}
After running the code it gives this error:
Method invocation failed because [FlaUI.Core.AutomationElements.AutomationElement] does not contain a method named 'SetValue'.
On line: 11 character: 5
+ $saveButton[0].SetValue('Value Test')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
However, I'm having a hard time finding a way to access this particular field and set its value using FlaUI. Any suggestions on how I can do this?
Related
I want to simply add some numbers to an array and then sort them via powershell, however, the following code seems to be wrong
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
$myArray.Add($Tokens[$Tokens.Count-1])
}
Write-Host($myArray | Sort-Object)
The error is
+ $myArray.Add($Tokens[$Tokens.Count-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
How can I fix that?
The variable $Name is something like 101.u18.uab.14 or 103.win10.template or 102.win7.pink.18 and so on. Each $Name has some . symbols and I want to tokenize them and get the last element for each of them. So, in this example, I want to see a sorted 14 18 template.
UPDATE:
The provided methods seems to be incorrect.
1- This method by Steven
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
[Void]$myArray.Add($Tokens[-1])
}
shows this error
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At C:\Users\user\Desktop\get_ip_list.ps1:20 char:5
+ [Void]$myArray.Add($Tokens[-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
2- This method by Santiago
$myArray = [Collections.Generic.List[string]]::new()
Foreach ($Name in $VMName) {
[Void]$myArray.Add($Name.Split(".")[-1])
}
Shows the following error
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At C:\Users\user\Desktop\get_ip_list.ps1:19 char:5
+ [Void]$myArray.Add($Name.Split(".")[-1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NotSupportedException
If I have missed your point in the above codes, please let me know.
I think you are missing the first line from the error. However it looks like you are simply trying to add the last elements from the $Tokens array. In that case you don't need to reference the index like that, below should work:
$myArray = New-Object System.Collections.ArrayList
Foreach ($Name in $VMName) {
$Tokens = $Name.Split(".")
[Void]$myArray.Add($Tokens[-1])
}
Notice the addition of [Void] this will stop the .Add() method from returning the index number it just added to.
Also note you can create array list objects using casting like:
$myArray = [Collections.ArrayList]#()
Update to Address Continued Errors:
The only thing I can think of to cause the error "Collection was of a fixed size." is if you've previously type constrained the variable.
Example:
[String[]]$myArray = #()
# Posibly a whole bunch of other things happening maybe in the console or IDE.
$myArray = [Collections.ArrayList]#()
$myArray.Add('something')
Results:
Exception calling "Add" with "1" argument(s): "Collection was of a fixed size."
At line:1 char:1
+ $myArray.Add('something')...
In this case they type of the $myArray will not change to [Collections.ArrayList]. The problem will be transparent up until you try to use the .Add() method that won't work. This is because an array list is easily and therefore silently cast back to a [String[]] or [Object[]].
Note: If you were to run $myArray.IsFixedSize it would return "True".
My guess as to what's happening; at some point while developing your code or perhaps in the larger script, $myArray got type constrained, and stuck in the scope. This can definitely happen especially given the scope overlap in IDE's like PowerShell's ISE, and I think it happens in VSCode as well. If this is part of a larger script look for instances of $myArray to see if it's indeed type constrained and make corrections as needed. Otherwise a simply restarting your session might do the trick.
Honestly, not sure how could you be getting that error unless the array we're looping through is actually something different. Steven's answer should work fine, I'll put this code below just to show that the results we get are the ones you expect:
$col = [Collections.Generic.List[String]]::new()
$vmName = #(
'101.u18.uab.14'
'103.win10.template'
'102.win7.pink.18'
)
ForEach($name in $vmName)
{
$col.Add($name.Split('.')[-1])
}
if you want absolutly use array you can simply do it :
$Array=#()
$VMName | %{
$Value=($_.Split('.'))[-1]
$Array+= $Value
}
$Array| sort
Otherwise you can simply do it :
$VMName | %{($_.Split('.'))[-1]} | sort
I am facing a problem how to add folder to existing ZIP file.
This zip file is created by PowerShell also.
I can only use system classes provided by Powershell 5. I cannot use any of user packages or plugins (7zip included).
Here is my code:
function addFileToArchiveTest ($filePathToAdd, $archivePathToUpdate) {
if ([System.IO.File]::Exists($filePathToAdd) -or (Test-Path $filePathToAdd)) {
$file = [System.IO.Path]::GetFileName($filePathToAdd);
Write-Host $filePathToAdd.Name;
Write-Host $filePathToAdd;
Write-Host $archivePathToUpdate;
$archive = [System.IO.Compression.ZipFile]::Open($archivePathToUpdate, [System.IO.Compression.ZipArchiveMode]::Update);
$compressionLevel = [System.IO.Compression.CompressionLevel]::NoCompression;
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($archive, $filePathToAdd, $file, "$compressionLevel");
$archive.Dispose();
} else {
Write-Host "[ERROR#function] <AddFileToArchive>: <filePathToAdd> does not exist!";
Write-Host "[ERROR#function] <Variable<filePathToAdd>>: $filePathToAdd";
Write-Host "[ERROR#function] <Variable<archivePathToUpdate>>: $archivePathToUpdate";
}
}
I am thinking about variable $file - there might be a problem, because folder doesn't have an extension.
I run script like this:
PS> addFileToArchiveTest "C:\TestFolder\FolderToArchive" "C:\TestFolder\thereIsAlreadyZipFile.zip"
It returns with error:
Exception calling "CreateEntryFromFile" with "4" argument(s): "Access to the
path 'C:\TestFolder\FolderToArchive' is denied."
At C:\Users\user\Desktop\testfolder.ps1:196 char:13
+ [System.IO.Compression.ZipFileExtensions]::CreateEntryFro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException
Noted I also try allow script and I am launching with admin rights.
Perhaps surprisingly, CreateEntryFromFile() is for adding files, not folders. You need to add each file individually:
Get-ChildItem $filePathToAdd | ForEach-Object {
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($archive, $_.FullName, $_.Name, "$compressionLevel")
}
As user #guiwhatsthat answered: PowerShell 5 does support Compress-Archive. It does exactly what you want.
That is working as I want.
I am trying to follow this example in order to attach images to an email with powershell. Here is the part of the code that behaves strange:
if ($DirectoryInfo) {
foreach ($element in $DirectoryInfo) {
$failedTest = $element| Select-Object -Expand name
$failedTests += $failedTest
$failedTestLog = "$PathLog\$failedTest.log"
$logContent = [IO.File]::ReadAllText($failedTestLog)
$imageDir = "$PathLog\$element\Firefox\*"
$imageSearch = Get-ChildItem -Path $imageDir -Include *.png -Recurse -Force
$imageFullname = $imageSearch | select FullName | Select-Object -Expand Fullname
$imageFilename = $imageSearch | Select-Object -Expand name
$imageFilename
$imageFullname
# *** THE FOLLOWING LINE CAUSES THE ERROR ***
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $imageFullname.ToString() # *** CAUSING ERROR ***
#$attachment.ContentDisposition.Inline = $True
#$attachment.ContentDisposition.DispositionType = "Inline"
#$attachment.ContentType.MediaType = "image/jpg"
#$attachment.ContentId = '$imageFilename'
#$msg.Attachments.Add($attachment)
$outputLog += "
********************************************
$failedTest
********************************************
$logContent
"
}
} else {
$outputLog = '** No failed tests **'
}
# Create the Overview report
$outputSummary = ""
foreach ($element in $scenarioInfo) {
if (CheckTest $failedTests $element) {
$outputSummary += "
$element : FAILED" # *** ERROR LINE ***
} Else {
$outputSummary += "
$element : Passed"
}
}
If I comment out the line which defines the attachment, the code works fine. If I use the code as it is, I get the following error:
Unexpected token ':' in expression or statement.
At D:\Testing\Data\Powershell\LoadRunner\LRmain.ps1:112 char:11
+ $element : <<<< FAILED"
+ CategoryInfo : ParserError: (::String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
which refers to the line at the bottom of the script where it says "ERROR LINE". What the heck is going on? The behavior look completely illogical to me! I don't understand how a statement, which has no effect at all, can cause an error elsewhere! What is the problem and how to fix it...?
Also it does not matter if I use $imageFullname or $imageFullname.ToString() in the offending line.
Try to replace "$element : FAILED" by
"$element` : FAILED"
The reverse quote will escape the semicolon; which has a specific meaning in PowerShell. (It allows to output subproperty : $env:username for example)
Define the $outputSummary as Array:
$outputSummary = #()
instead of
$outputSummary = ""
I need help with my code.
I first tried to create a new wiki page in SharePoint 2013 and that worked perfectly.
Now I'm trying to delete a wiki page and that doesn't work very well.
My code:
Add-Type –Path "C:\Users\Benutzername\AppData\Local\Apps\OfficeDevPnP.PowerShell.V15.Commands\Modules\OfficeDevPnP.PowerShell.V15.Commands\Microsoft.SharePoint.Client.dll"
Add-Type –Path "C:\Users\Benutzername\AppData\Local\Apps\OfficeDevPnP.PowerShell.V15.Commands\Modules\OfficeDevPnP.PowerShell.V15.Commands\Microsoft.SharePoint.Client.Runtime.dll"
Function Delete-WikiPage ([Microsoft.SharePoint.Client.ClientContext]$Context, [string]$WikiLibraryTitle,[string]$PageName)
{
$wikiLibrary = $context.Web.Lists.GetByTitle($wikiLibraryTitle)
$context.Load($wikiLibrary.RootFolder.Files)
$context.ExecuteQuery()
$wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
$context.Load($wikiPage)
$context.DeleteObject()
}
$Url = "hhtps://sharepoint.com"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$pageName = "Testlauf.aspx"
Delete-WikiPage -Context $context -WikiLibraryTitle "Testwiki" -PageName $pageName
I got an error message about the DeleteObject() method but I didn't find anything how I can fix that:
Error when calling the method [Microsoft.SharePoint.Client.ClientContext] no method found with the name "DeleteObject".
In C:\Users\Benutzername\Desktop\Projektarbeit_Wiki\PowerShell\Delete_WikiPage.ps1:12 Zeichen:5
+ $context.DeleteObject()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I can only work with methods from the Add-Types. Can anyone help me with this?
You use the CSOM in PowerShell. (you load the CSOM DLL ).
So you should search information with SharPoint CSOM.
To answer you question, you use : DeleteObject() on you object : context. This methode doesn't exist on the Context Object. But the methode exist on the File.
Si you should try something like :
$wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
$context.Load($wikiPage)
$context.ExecuteQuery() //Get the item, but not sure this is needed
$wikiPage.DeleteObject()
$context.ExecuteQuery()
personal Note :
You should use CAML query to retrieve your WikiPage, not this: $wikiPage = $wikiLibrary.RootFolder.Files | Where {$_.Name -eq $pageName}
With this kind of query you have bad performance, and you can raise exception list threshold
$timeout = new-timespan -Minutes 1
$sw = [diagnostics.stopwatch]::StartNew()
$path = "d:\powershell\test.csv"
"Processor Load, Available Memory(MB), Max Memory(Bytes)" >> $path
while ($sw.elapsed -lt $timeout)
{
$a = gwmi -query "Select * from win32_processor"
$b = gwmi -query "Select * from win32_perfrawdata_perfOS_memory"
$c = gwmi -query "Select * from win32_physicalmemory"
$date = Get-Date -format s
$a.loadpercentage + "," + $b.availableMbytes + "," + $c.capacity >> $path
start-sleep -seconds 5
}
So I'm just looking to get a minute long snapshot of what's going on. I'm not just opening this in perfmon for reasons. Basically I'd expect to get a comma-delimited output in the CSV file mentioned. It works for a single variable, but when I try to add a second variable, or text I get the following error.
Cannot convert value ", test" to type "System.Int32". Error: "Input string was not in a
correct format."
At D:\powershell\VDIPerfMon.ps1:14 char:21
+ $a.loadpercentage + <<<< ", test" >> $path
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
How can I fix this problem?
When you use the + operator PowerShell looks on the left hand side to determine the resulting type of the expression. It is seeing an int on the left of the + and a string (that can't be converted to an int) on the right. Try it this way:
"$($a.loadpercentage), $($b.availableMbytes), $($c.capacity)" >> $path
Also where you write your headers, you might not want to append i.e. in order to overwrite old attempts:
"Processor Load, Available Memory(MB), Max Memory(Bytes)" > $path
The error is because $a.loadpercentage is an int. You are then trying to add an int and a string.
One workaround is to explicitly call .ToString()
$a.loadpercentage.ToString() + "," + $b.availableMbytes.ToString() + "," + $c.capacity.ToString() >> $path
Another way is the PowerShell array join operator. It is quick easy, and types do not matter:
($a.loadpercentage, $b.availableMbytes, $c.capacity) -join "," |
Add-Content $path
Yet another way is with a string formatter. This will easily let you control the precision and display of each value:
'{0},{1},{2}' -f $a.loadpercentage, $b.availableMbytes, $c.capacity