Sendkeys emulating WIN+ALT+4 - batch-file

I'm trying with no success to emulate the WIN+ALT+4 in a batch file:
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('(^{ESCAPE})%{4}')
The command above was my last attempt.

Related

cmd agent job shows success but does nothing to the file

I have a powershell script that launches an Excel macro to refresh pivot tables and data sources, it works fine when run manually or from cmd.
when run from Sql Agent job it succeeds without performing the Excel refresh.
I call the script with this command in the job step:
powershell -command "&'C:\rapport_declaratif\REFRESH_3.ps1'"
I tried calling it with powershell, with cmd, with xp_cmdshell but nothing seems to work.
I even called it differently, for exp when I call it with this command:
cmd.exe /c "C:\rapport_declaratif\REFRESH_3.ps1"
it runs forever.
I also googled and found that I have to create this folder:
C:\Windows\SysWOW64\config\systemprofile\DeskTop
I did and it didn't solve the problem.
Below is my powershell script:
# Open Excel file
$excel = new-object -comobject excel.application
$filePath = "C:\rapport_declaratif\Rapport_Declaratif.xlsm"
$workbook = $excel.Workbooks.Open($FilePath)
$excel.Visible = $true
$excel.DisplayAlerts = $false
$worksheet = $workbook.worksheets.item(1)
#Write-Host "Running macro in excel to refresh data."#
$excel.Run("Refresh_ALL")
Start-Sleep -s 30
#Write-Host "data refreshed."#
$workbook.save()
# Write-Host "saved."#
$workbook.close()
$excel.quit()
#Write-Host "Closed Excel"#

Problem executing REMOTE powershell script using WinSCP via SQL Server Agent Job

On SQL Server 2016 I have setup a job that executes a powershell script that resides on a remote app server. When I execute the powershell script via the app server using the Powershell ISE app my script works without issue. When I had setup the job and enter this command:
powershell.exe -ExecutionPolicy Bypass -file "\\serverapp1\c$\coverageverifier_scripts\SFTP_CoverageVerifier.ps1" in Step 1.
When I look at the VIEW HISTORY I see the error below but I cannot figure out why the script now cannot load the file or assembly.
Any help/direction would be appreciated. Here is the error:
The job script encountered the following errors. These errors did not stop the script: A job step received an error at line 1 in a PowerShell script. The corresponding line is 'powershell.exe -ExecutionPolicy Bypass -File "\empqaapp1\c$\coverageverifier_scripts\SFTP_CoverageVerifier.ps1"'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Add-Type : Could not load file or assembly '
Here is my powershell script as well:
# Load WinSCP .NET assembly
#Add-Type -Path "WinSCPnet.dll"
Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
# Declare variables
$date = Get-Date
$dateStr = $date.ToString("yyyyMMdd")
# Define $filePath
$filePath = "C:\coverageverifier_scripts\TEST_cvgver.20190121.0101"
# Write-Output $filePath
# Set up session options for VERISK TEST/ACCEPTANCE SFTP SERVER
$sessionOptions = New-Object WinSCP.SessionOptions -Property #{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "secureftpa.iso.com"
UserName = "account"
Password = "pw"
SshHostKeyFingerprint = "ssh-rsa 1111 xxx/xxxxxxxxx+3wuWNIkMY5GGgRObJisCPM9c9l7yw="
}
$session = New-Object WinSCP.Session
$session.ExecutablePath = "C:\WinSCP\WinSCP.exe"
try
{
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.PutFiles($filePath,"/").Check()
}
finally
{
$session.Dispose()
Your Add-Type call does not include the path to the WinSCPnet.dll.
If you have the WinSCPnet.dll in the same folder as your PowerShell script, use this syntax:
Add-Type -Path (Join-Path $PSScriptRoot "WinSCPnet.dll")
Or use a full path.
See also Loading assembly section in WinSCP .NET assembly article on Using WinSCP .NET Assembly from PowerShell.

How to open the Run dialog box with a file already in the box

Crappy title, but the thing I want to do is this:
Open the windows dialog box from cmd
I already know how to do this
But I was wandering if I could open it and have a set file selected
E.g. powershell -c (New-Object -ComObject "Shell.Application").FileRun() opens the dialog box
But I want:
powershell -c (New-Object -ComObject "Shell.Application").FileRun("C:\<file_location>") opens the dialog box with "C:\" automatically inserted
This seems to work. I am by no means a powershell guy. I just Google searched.
powershell -c (New-Object -ComObject "Shell.Application").FileRun();$wshell = New-Object -ComObject wscript.shell;$wshell.SendKeys('C:\temp')

Powershell passing named parameters from file

i have a problem that is troubling my mind.
I want to automatically execute powershell scripts with named arguments but within another powershell script (that will act as a script deamon).
For example:
One of the scripts that get called has this parameters
param(
[int]$version,
[string]$user,
[string]$pass,
[string]$domain,
)
The powershell script deamon now loads the file and arguments like this
$argumentsFromScript = [System.IO.File]::ReadAllText("C:\params.txt") $job = Start-Job { & "ps1file" $arguments}
The params.txt contains the data like this
-versionInfo 2012 -user admin -pass admin -domain Workgrup
But when i try to execute this code obviously the whole $argumentsFromScript variable will be seen as parameter 1 (version) and i end up with an error, that "-versionInfo 2012 -user admin -pass admin -domain Workgrup" cannot be converted to Int32...
Do you guys have any idea how i can accomplish this task?
The Powershell deamon does not know anything about the parameters. He just needs to execute scripts with given named parameters. The params.txt is just an example. Any other file (csv,ps1,xml,etc) would be fine, i just want to automatically get the named parameters passed to the script.
Thank you in advance for any help or advice..
Try this:
#'
param ([string]$logname,[int]$newest)
get-eventlog -LogName $logname -Newest $newest
'# | sc c:\testfiles\testscript.ps1
'-logname:application -newest:10' | sc c:\testfiles\params.txt
$script = 'c:\testfiles\testscript.ps1'
$arguments = 'c:\testfiles\params.txt'
$sb = [scriptblock]::Create("$script $(get-content $argumentlist)")
Start-Job -ScriptBlock $sb
I guess you want this:
$ps1 = (Resolve-Path .\YourScript.ps1).ProviderPath
$parms = (Resolve-Path .\YourNamedParameters.txt).ProviderPath
$job = sajb -ScriptBlock {
param($ps1,$parms)
iex "$ps1 $parms"
} -ArgumentList #(
$ps1,
[string](gc $parms)
)
# if you wanna see the outcome
rcjb $job -Wait

Executing .bat file from powershell button click event

I was working with some powershell button that calls a .bat file. I have looked around but it only appears to work the other way around. I'm trying to figure out the syntax to call a .bat file from powershell button. This seems to work from command line, but I'm having issues with using it within my button. Any sugestions?
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(170, 450)
$StartButton.Size = New-Object System.Drawing.Size(125,43)
$StartButton.Font = New-Object System.Drawing.Font("Courier New", "10.5")
$StartButton.Text = "Start Tuner"
$StartButton.Add_Click(invoke-command{C:\temp\Map.bat})
$tunerForm.Controls.Add($StartButton)
Thanks anything is highly appreciated.
You can also use Start-Process and -wait
Start-Process myBatch.bat -wait
That way, your script does not do anything else while that script runs / completes.
Sorry guys.. I found the answer.. Invoke-Expression "cmd /c c:\path\to\batch\file.bat"
Sorry...

Resources