Using Powershell to download and run exe file - powershell

I am trying to download Malwarebytes Anti-Rootkit Beta but my PowerShell script throws the error below. How do I fix this?
My Script:
$url = "https://data-cdn.mbamupdates.com/web/mbar-1.10.3.1001.exe"
$outpath = "$PSScriptRoot/mbar-1.10.3.1001.exe"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $outpath)
$args = #("Comma","Separated","Arguments")
Start-Process -Filepath "$PSScriptRoot/mbar-1.10.3.1001.exe" -ArgumentList $args
The Error:
Start-Process : This command cannot be run due to the error: The system cannot find the file specified.
At line:8 char:1
+ Start-Process -Filepath "$PSScriptRoot/mbar-1.10.3.1001.exe" -Argumen ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Related

How to use SSISDeploy command in powershell script

I'm running SSISDeploy command (documentation is here) in my CMD:
SSISDeploy.exe -s:"download\Integration Services.ispac" -d:catalog;/SSISDB/TEST/DEVOPS;"TEST03,1234" -at:win
all working good, and now I need to run it thought powershell script (against windows server 2019 slave), so I tried this syntax:
$SSISDeploy = Start-Process -FilePath SSISDeploy.exe -ArgumentList '/source:"download\Integration Services.ispac"',"/destination:catalog;${Target};"${Env}"" -at:win -wait -PassThru -Credential $cred -RedirectStandardOutput ssisstdout.txt -RedirectStandardError ssisstderr.txt
but it fails with exception:
Start-Process : A positional parameter cannot be found that accepts argument 'TEST03,1234'.
+ ... SISDeploy = Start-Process -FilePath SSISDeploy.exe -ArgumentList '/so ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
Can you suggest what's wrong with the syntax?
$StartProcessProps = #{
FilePath = 'SSISDeploy.exe'
ArgumentList = '-s:"download\Integration Services.ispac" -d:catalog;{0};{1} -at:win' -f $Target, $Env
Wait = $true
PassThru = $true
Credential = $cred
RedirectStandardOutput = 'ssisstdout.txt'
RedirectStandardError = 'ssisstderr.txt'
}
$SSISDeploy = Start-Process #StartProcessProp

Invoke-WebRequest is not recognized as the name of a cmdlet Windows 7 Powershell

How can I use this script on Windows 7 PowerShell?
$IE = new-object -com internetexplorer.application
$go = (Invoke-WebRequest –Uri ‘c:\link.html’).Links.href
$IE.navigate($go)
$IE.visible=$true
start-sleep 5
$word=$go = (Invoke-WebRequest –Uri ‘c:\word.html’).Links.href
$Link = $IE.Document.getElementsByTagName("span") | ? {$_.InnerHTML -eq "$word"}
$word2=$go = (Invoke-WebRequest –Uri ‘c:\word2.html’).Links.href
$ie.Document.getElementsByTagName("$word2").item(0).click()
After I run this script I get this error:
The term 'Invoke-WebRequest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:7 char:29
+ $go = (Invoke-WebRequest <<<< –Uri ‘http://lapfix.ir/link.html’).Links.href
+ CategoryInfo : ObjectNotFound: (Invoke-WebRequest:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The term 'Invoke-WebRequest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t
hat the path is correct and try again.
At line:12 char:31
+ $word=$go = (Invoke-WebRequest <<<< –Uri ‘http://lapfix.ir/word.html’).Links.href
+ CategoryInfo : ObjectNotFound: (Invoke-WebRequest:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Cannot find an overload for "getElementsByTagName" and the argument count: "1".
At line:13 char:42
+ $Link = $IE.Document.getElementsByTagName <<<< ("span") | ? {$_.InnerHTML -eq "$word"}
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
You cannot call a method on a null-valued expression.
At line:14 char:12
+ $Link.click <<<< ()
+ CategoryInfo : InvalidOperation: (click:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I think the error is saying can't use Invoke-WebRequest on Windows 7. Why would that be?
By default Windows 7 comes with PowerShell version 2.0 installed. The Invoke-WebRequest cmdlet was introduced in PowerShell version 3.0.
The simplest solution is to upgrade your version of PowerShell to 3 or greater (I recommend just installing the latest version: 5.1). You can do that by downloading the Windows Management Framework:
https://www.microsoft.com/en-us/download/details.aspx?id=54616
i found this and working with powershell version 2
$req = [System.Net.WebRequest]::Create("http://sample.com/link.html")
$resp = $req.GetResponse()
$reqstream = $resp.GetResponseStream()
$stream = new-object System.IO.StreamReader $reqstream
$result = $stream.ReadToEnd()
This is for test result : #Write-Host -Object $result
do you know any other command to do this for powershell version 2 ?
$Link = $IE.Document.getElementsByTagName("span") | ? {$_.InnerHTML -eq "https://sample.com/"}
$Link.click()
this is not working with powershell version 2 !

How do I open an image and then close it automatically in PowerShell?

I want to open an image in Windows Photo Viewer and then close it automatically using PowerShell. How would I achieve this?
I tried using the Start-Process cmdlet to open an image with the default application and have it return the associated process ID. The image is opened, but I don't get the process ID. This is probably because the Windows Photo Viewer-process is packed inside dllhost.exe.
PS C:\Users\Public\Pictures> $process = Start-Process -FilePath
.\Wiki-error.jpg -PassThru Start-Process
: This command cannot be run completely because the system cannot find
all the information required. At line:1 char:12
+ $process = Start-Process -FilePath .\Wiki-error.jpg -PassThru + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException +
FullyQualifiedErrorId :
InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
PS C:\Users\Public\Pictures> $process.ID
PS C:\Users\Public\Pictures> Stop-Process $process.ID
Stop-Process : Cannot bind argument to parameter 'Id' because it is
null. At line:1 char:14
+ Stop-Process $process.ID + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Stop-Process], ParameterBindingValidationException +
FullyQualifiedErrorId :
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.StopProcess
Command
I run PowerShell 2.1 and Windows 7. Any suggestions?

Powershell codes throwing errors on Jenkins

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

PowerShell 3.0 behavior when getting external process exit code

I have been using this code from another post from this site:
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "notepad.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = ""
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
**$p.Start() | Out-Null**
#Do Other Stuff Here....
**$p.WaitForExit()**
$p.ExitCode
It has worked fine under PowerShell 2.0. The server was upgraded to PowerShell 3.0, and now the two bold like fail with:
Exception calling "Start" with "0" argument(s): "The system cannot find the
file specified"
At \\asdnsom3978\optim_windows\script_master\exportall.ps1:126 char:1
+ $p.Start() | Out-Null
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : Win32Exception
Exception calling "WaitForExit" with "0" argument(s): "No process is
associated with this object."
At \\asdnsom3978\optim_windows\script_master\exportall.ps1:128 char:1
+ $p.WaitForExit()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
Why did it broke and how do I correct it so it works for both version 2.0 and 3.0?
Hmm, I can't repro the error on my V3 system. But why are you going to all this trouble when you can just use the Start-Process command to do this e.g.:
$p = Start-Process Notepad -Wait -PassThru
$p.ExitCode
You might want to try providing a fully-qualified path to notepad.exe, like "C:\Windows\notepad.exe".