I'm new to PowerShell
I want to send 3 different responses to 3 different user prompts that I get when running a PowerShell script using another PowerShell script in Jenkins
I tried
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("y")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("y")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("dummytext")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
$command = "D:\testing\source\sample.ps1" + " -param1 'dummyParam'"
Invoke-Expression $command
but I keep getting this error
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At D:\testing\powershell\testing.ps1:6 char:1
+ [System.Windows.Forms.SendKeys]::SendWait("y")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : Win32Exception
Build step 'PowerShell' marked build as failure
Finished: FAILURE
Does anyone how to fix this issue
Related
I execute two Powershell scripts during a installation process from a desktop application under Windows 10 IoT Enterprise.
%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Bypass -File ".\KeyboardFilter.ps1"
%WINDIR%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Bypass -File ".\ShellLauncher.ps1"
But the execution of the Powershell scripts is not successful. I get the following errors:
Get-WMIObject : Provider load failure
At C:\Program Files\Application\KeyboardFilter.ps1:31 char:19
+ ... $predefined = Get-WMIObject -class WEKF_PredefinedKey #CommonParams |
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Write-Error : A positional parameter cannot be found that accepts argument 'is'.
At C:\Program Files\Application\KeyboardFilter.ps1:41 char:9
+ Write-Error $Id is not a valid predefined key
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Write-Error], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteErrorCommand
enable-windowsoptionalfeature : An attempt was made to load a program with an incorrect format.
At C:\Program Files\Application\ShellLauncher.ps1:4 char:1
+ enable-windowsoptionalfeature -online -featureName Client-EmbeddedShe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Enable-WindowsOptionalFeature], COMException
+ FullyQualifiedErrorId : Microsoft.Dism.Commands.EnableWindowsOptionalFeatureCommand
The installation process starts with administration permissions.
The first script adds key combinations to the Keyboard Filter (Windows 10 IoT feature).
The second scripts enable and configure the Shell Launcher (also Windows 10 IoT feature).
KeyboardFilter.ps1:
param (
[String] $ComputerName
)
$CommonParams = #{"namespace"="root\standardcimv2\embedded"}
$CommonParams += $PSBoundParameters
function Enable-Predefined-Key($Id) {
$predefined = Get-WMIObject -class WEKF_PredefinedKey #CommonParams |
where {
$_.Id -eq "$Id"
};
if ($predefined) {
$predefined.Enabled = 1;
$predefined.Put() | Out-Null;
Write-Host Enabled $Id
} else {
Write-Error $Id is not a valid predefined key
}
}
If I execute the Powershell scripts in a batchfile or on Powershell console, everything works fine. I also tried to execute the Powershell scripts during the installation process with Powershell x86 and x64, same errors in both cases.
Any hints, tips or solution for this problem?
I'm trying to create a PowerShell script (based on two examples found on blogs) that will take a list of servers in a file passed in as an argument and then script out the SQL Server Agent jobs into T-SQL.
I'm getting this error on the last line:
$scrp.Script($job)
OBJECT DEFINITIONS
$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($ServerName)
$job in $s.JobServer.Jobs
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
ERROR:
Exception calling "Script" with "1" argument(s): "Script failed for Job 'AX_Project_Contract_Monitor'. " At C:\Users\045810dl\Documents\PowerShell\Script Out SQL Agent Jobs\Script Out SQL Agent Jobs.ps1:101 char:6
+ $scrp.Script($job)
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FailedOperationException
The object $scrp seems to be correctly connected to the SMO.Scripter method based on debugging output.
Thanks in advance!
I've added the following line of Powershell codes in Jenkins and it's throwing some errors, although it's working fine when I'm trying to execute it via Powershell ISE on the same desktop/machine.
I have configured the Jenkins service logon and the user ID got correct permission.
Kindly advise.
servers = Get-Content "C:\tmp\script\updated\SessionHost.txt"
foreach ($server in $servers)
{
$FolderWindows = Start-Process -FilePath "\\$server\C$\Temp" -WindowStyle Maximized
}
Error Message on Jenkins:
Start-Process : This command cannot be run due to the error: Server execution
failed.At C:\tmp\script\updated\MainTaiwanUAT.ps1:385 char:22
+ $FolderWindows = Start-Process -FilePath "\\$server\C$\Temp"
-WindowS ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOp
erationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
ommands.StartProcessCommand
I've created sample cmdlet as a file.ps1 with logic to check my new inbox emails iterative on required time. But when OS Task scheduler (Windows 7) executed it I get these errors:
The interface is unknown. (Exception from HRESULT: 0x800706B5)At
C:\SCRIPTS\my-userAgent.ps1:7 char:7
+ while($ie.busy -eq $true)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
Method invocation failed because [System.__ComObject] does not contain a
method named 'Navigate'.At C:\SCRIPTS\my-userAgent.ps1:18 char:2
+ $ie.Navigate($url)
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Navigate:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : MethodNotFound
As far as I understand that they are caused by my COM_obj, But why when I execute the same script from PowerShell_ISE it passes and works without errors!?! I've tried to resolve it by:
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
hoping that is user_privileges related, but no success so far.
Any help will be appreciated.
I am trying to write a PowerShell script to perform steps on multiple Word docs. I have Word 2010 installed on my machine, but I can't seem to get the script to open the docs. Here is the script
$path = "C:\MyPath"
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -recurse
$objWord = New-Object -ComObject "word.application"
$objWord.visible = $false
foreach($wd in $wordFiles)
{
$doc = $objWord.documents.open($wd.fullname)
#InsertProcessingFunctionsHere
$doc.Save()
$objWord.Documents.Close()
}
$objWord.Quit()
I try and run this, and the error I get back from PowerShell is:
Exception calling "Open" with "1" argument(s): "Command failed"
At C:\Scripts\Process-WordDocs.ps1:10 char:31
+ $doc = $objWord.documents.open <<<< ($wd.fullname)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\Process-WordDocs.ps1:13 char:10
+ $doc.Save <<<< ()
+ CategoryInfo : InvalidOperation: (Save:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Close" with "0" argument(s): "This method or property is not available because a document window is not active."
At C:\Scripts\Process-WordDocs.ps1:14 char:25
+ $objWord.Documents.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
MSDN states that documents.open only requires 1 argument, and the rest are optional. However, a C# example I have seen on the net, showed passing a "ReadOnly: False" parameter to documents.open. Stepping through the script in the ISE Debugger, I can see $wd.fullname is there and points to a valid file, so I am completely unclear why it is not opening. At first, I thought this was because I was using a 64-bit version of the OS (32-bit version of Office), but attempting the script from a 32-bit PowerShell Session resulted in the same error. Anyone have any insight here as to why this may be happening, and how I can fix it? I would prefer all the processing to happen invisible to the user. Any help would be greatly appreciated. Thank you in advance for your time.
I think you want to close the document using $doc.close() instead of $objWord.Documents.Close()