Scheduled task running powershell script with SendKeys and getting Access is denied - powershell

I have a scheduled task that runs a power shell console window with script (script is always runned from machine that has that scheduled task, so there is no remote task calling) that contains:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('~');
Problem is when I'm logged (remotely or locally) it runs smoothly (power shell console opens, procedure runs, key is pressed and procedure completes), but when I log off script is throwing exception in power shell console window:
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\Testing\Powershell\PageFeature.ps1:4 char:2
+ [System.Windows.Forms.SendKeys]::SendWait('~');
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Win32Exception
I set task to run with my credentials even when I log off, and still getting messages in the console window.
What could be the root problem of this?

maybe just try:
$obj = new-object -ComObject WScript.Shell
$obj.SendKeys("~")

Related

powershell Command Works When run alone, but note when scripted

I am working on a deployment script for some software, and my script is working on probably 90% of the machines it runs on, but on the other 10%, I get a failure in the download portion. Here is what I have:
$tls = "Tls";
$Token = "#ZorusKey#";
[System.Net.ServicePointManager]::SecurityProtocol = $tls;
$source = <Source>
$destination = "C:\installers\ZorusInstaller.exe";
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($source, $destination)
I have created the C:\installers folder on all target machines, so I don't think that's the problem. When run as a script, I get the following exception:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At C:\Windows\system32\config\systemprofile\AppData\Local\f1e96243cb9c4ef0b0336a152ed8827f.ps1:8 char:1
+ $WebClient.DownloadFile($source, $destination)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Which then causes the following steps to fail, as the downloaded file doesn't exist.
I have also taken the step on several of the failing machines of running each line of the script above one at a time (copied / pasted from the same Powershell ISE window) and the download works without issue. looking for a possible reason / resolution.
Thanks in advance.

Powershell won't send keys once locked

I'm attempting to create an autoclicker that can run in the background, or while other users use the computer and the account that the autoclicker is on is not logged off of (I'm using Windows 10)
I've tried running the script with administrative privileges. However, this still returns the same issue. I believe it may be an issue with Windows security and not powershell itself, however I do not know any other methods.
add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms
$wshell = New-Object -ComObject wscript.shell;
do {
[Microsoft.VisualBasic.Interaction]::AppActivate("Program")
[System.Windows.Forms.SendKeys]::SendWait("m");
start-sleep -Milliseconds 1050
} while ($true)
This is the output:
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\Users\me\Desktop\ASI.ps1:8 char:1
+ [System.Windows.Forms.SendKeys]::SendWait("m");
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Win32Exception
Is there any method that can simulate key input, even if the desktop is locked?
You will not be able to complete what you are attempting to do natively.
You will be able to create a Virtual Machine that may be able to continue running while the computer is locked. That way you can run all your scripts and game inside the virtual machine, lock the physical machine and away you go.
Even running it as a scheduled task with "Run whether user is logged on or not" checked, it will not give you the desired results.

Task Scheduler Powershell DCOM error

I am running a task to execute a powershell script.
The script runs fine if I run it manually through powershell, or through the task scheduler with "Run only when user is logged on" is selected.
If I change it to "Run whether user is logged on or not" (even with highest privledges checked) I get the following error when I catch the exception from my powershell script that tries to open up internet explorer:
New-Object : Retrieving the COM class factory for component with CLSID {0002DF0
1-0000-0000-C000-000000000046} failed due to the following error: 80080005.
At D:\PSA\EYReserve\EY_reserve.ps1:14 char:18
+ $ie = New-Object <<<< -com internetexplorer.application;
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMExcept
ion
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
ds.NewObjectCommand
I've tried steps provided in solutions for similar problems but they did not work. Namely modifying DCOM settings, adding "NETWORK SERVICE" with full permissions etc.
Anyone think of something I could have missed?
Thanks!

Error creating a powershell COM object in Jenkins

I want to create a COM object, internet explorer application.
The following code works fine in PowerShell ISE (32 bit):
$ie = New-Object -com InternetExplorer.Application
But, when I try running it on Jenkins Windows server (using either the powershell plugin or calling the PS1 with a batch file), I get the following error.
Building on master in workspace C:\Program Files\Jenkins\workspace\Jenkins Test
[Jenkins Test] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\WINDOWS\TEMP\hudson3148932824880446098.ps1'"
New-Object : Creating an instance of the COM component with CLSID
{0002DF01-0000-0000-C000-000000000046} from the IClassFactory failed due to the
following error: 80004005.
At C:\WINDOWS\TEMP\hudson3148932824880446098.ps1:1 char:17
+ $ie = New-Object <<<< -com InternetExplorer.Application
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
I'm using a VM at work (Server 2003). I'm not sure if this is an issue with my server permissions or if these are interactive commands being used with a non-interactive device. Can anyone help?

Powershell scripts will not work with Jenkins plugin on Windows

I'm writing some pretty simple scripts in Powershell 32-bit and they are working fine.
For instance, I want to open internet explorer to a webpage and start typing in keys. The following code works fine when I run it in powershell ISE.
Add-Type –AssemblyName System.Windows.Forms
$url = "http://WebAddress"
$ie = New-Object -com "InternetExplorer.Application"
$ie.Navigate($url)
sleep 30
[System.Windows.Forms.SendKeys]::SendWait("{2}{tab}{H}{E}{L}{L}{O}")
I have downloaded a Windows PowerShell plugin and have executed the same code through it and received the following errors:
Building on master in workspace C:\Program Files\Jenkins\workspace\Jenkins Test
[Jenkins Test] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\WINDOWS\TEMP\hudson257018662776252417.ps1'"
New-Object : Creating an instance of the COM component with CLSID {0002DF01-000
0-0000-C000-000000000046} from the IClassFactory failed due to the following er
ror: 80004005.
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:4 char:17
+ $ie = New-Object <<<< -com "InternetExplorer.Application"
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMExcept
ion
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Comman
ds.NewObjectCommand
You cannot call a method on a null-valued expression.
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:5 char:13
+ $ie.Navigate <<<< ($url)
+ CategoryInfo : InvalidOperation: (Navigate:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\WINDOWS\TEMP\hudson257018662776252417.ps1:9 char:42
+ [System.Windows.Forms.SendKeys]::SendWait <<<< ("{2}{tab}{H}{E}{L}{L}{O}")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Is there a reason why my code will not work through Jenkins like it does in Powershell? I tried some simple echo scripts which worked fine, but any more complex and it throws an error.
You may need to set execution policy once: https://technet.microsoft.com/library/hh849812.aspx
Set-ExecutionPolicy Unrestricted
Run PowerShell as admin and run that command. There may be security concerns using Unrestricted. But if it works, read the link above and find the policy that works best in your context. Options are (copy/pasted from the link):
Restricted. Does not load configuration files or run scripts. Restricted is the default execution policy.
AllSigned. Requires that all scripts and configuration files be signed by a trusted publisher, including scripts that you write on
the local computer.
RemoteSigned. Requires that all scripts and configuration files downloaded from the Internet be signed by a trusted publisher.
Unrestricted. Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet,
you are prompted for permission before it runs.
Bypass. Nothing is blocked and there are no warnings or prompts.
Undefined. Removes the currently assigned execution policy from the current scope. This parameter will not remove an execution policy
that is set in a Group Policy scope.