How can I pass a powershell variable into a CMD command? - variables

I am trying to run the following command through powershell:
schtasks /query /s $compname /v /fo csv | ConvertFrom-Csv
$compname is set in the first part of the powershell script, but cannot be passed into the command like this. For example, when I try passing in $localhost, I get the following error:
ERROR: Invalid syntax. Value expected for '/s'.
Type "SCHTASKS /QUERY /?" for usage.
Is there a way to pass the variable into this command? I don't plan to change to powershell's native way to access scheduled tasks (through a COM object).

Working for me, tested in v2. Does it help if you enclose the variable in double quotes (e.g "$compname") ?
$compname = 'localhost'
schtasks /query /s $compname /v /fo csv | ConvertFrom-Csv

Try this:
Invoke-Expression -Command "schtasks /query /s $compname /v /fo csv" | ConvertFrom-Csv

Related

WMIC How to pass arguments/ parameters with exe

I want to call exe from remote system using WMIC direct command. for that I prepare one command here
WMIC /NODE:"RemoteSys" /USER:"domain\username" /PASSWORD:"XXXXXXXXX" PROCESS CREATE "C:\Program Files (x86)\Company\Product\productapp.exe"
But issue here is this productapp.exe is expecting an arguments/ parameters like " -p PlantA "
Question here is How to pass it ?
I tried many alternate ways but all fails like:
WMIC /NODE:"RemoteSys" /USER:"domain\username" /PASSWORD:"XXXXXXXXX" PROCESS CREATE "C:\Program Files (x86)\Company\Product\productapp.exe" -p PlantB
Output: Invalid Verb Switch
WMIC /NODE:"RemoteSys" /USER:"domain\username" /PASSWORD:"XXXXXXXXX" PROCESS CREATE "C:\Program Files (x86)\Company\Product\productapp.exe" "-p PlantB"
Output: Invalid format. Hint: = [, ].
Don't know what is paramlist & How to use that.
Can anyone help me here ?
I've had to do something similar with MATLAB. What you could try is opening a command prompt and executing the program with parameters from that command prompt.
WMIC /NODE:"RemoteSys" /USER:"username" /PASSWORD:"XXX" PROCESS CALL CREATE "cmd.exe /c cd C:/Program Files (x86)/Company/Product/ & productapp.exe -p PlantB"

How to execute SQL scripts in alphabetical order using a batch file

Is it possible to execute all SQL scripts in a folder in alphabetical order using a batch file ?
Currently I uses the following code but it executes the scripts in the order in which they are kept instead of executing it in alphabetical order
SET Database=<<DatabaseName>>
SET ScriptsPath=<<FolderPath>>
SET ServerName=<<ServerName>>
IF EXIST "%ScriptsPath%output_CCF.txt" del "%ScriptsPath%output_CCF.txt"
type NUL > "%ScriptsPath%output_CCF.txt"
FOR /R "%ScriptsPath%" %%G IN (*.sql *.up) DO (
sqlcmd -d %Database% -S %ServerName% -i "%%G" -o "%%G.txt"
echo .................................................
>> "%ScriptsPath%output_CCF.txt"
echo Executing: "%%G" >> "%ScriptsPath%output_CCF.txt"
echo ...................................>> "%ScriptsPath%output_CCF.txt"
copy "%ScriptsPath%output_CCF.txt"+"%%G.txt" "%ScriptsPath%output_CCF.txt"
del "%%G.txt"
)
If you use the /F option of the FOR command, then you can use another command to generate the set of data to be iterated over.
Use this FOR statement and I believe you'll find the result you're looking for.
FOR /F "usebackq" %%G IN (`dir /ON /B *.sql *.up`) DO ...
This uses the dir command to generate the set of files to use. The /ON argument orders the files by name and the /B argument provides a bare format (no heading information or summary) of the file names.
For more details on the arguments check the help for either of the commands from the command line dir /? and for /?

Running Command Line Arguments

I'm trying to run a command line argument through VB.NET using the Shell() command.
I'm trying to use this piece of code:
FOR /R %I in (*.pdf) DO #pdf2swf.exe "%~fI" -o "%~dpI%~nI.swf" -f -T 9
-t -G
Using this:
Shell("FOR /R %I in (*.pdf) DO #pdf2swf.exe "%~fI" -o "%~dpI%~nI.swf" -f -T 9
-t -G ")
However, the interpreter is giving me this error:
Character is not valid. (BC30037)
For the %~ part.
I also tried created a string and passing the argument to the Shell() command by using Shell(StringName) but I still get the same error in the string.
How could I fix this issue?
This is not proper use of the Shell Method:
Public Shared Function Shell (PathName As String, [...]) As Integer
Parameters
PathName
Type: System.String
Required. String. Name of the program to execute, together with any required arguments and command-line switches. PathName can also include the drive and the directory path or folder.
The first parameter is supposed to be the name of a program to execute. FOR is not a program, it's a built-in feature of the cmd.exe command line interpreter.
As far as I can see, you have the following options:
Option 1: Explicitly call cmd.exe and pass the string that you want to execute with the /c parameter:
Shell("cmd.exe /c for /R %I ...")
Don't for get to duplicate quotation marks (") to escape them.
Option 2: Create a batch file and call the batch file using Shell.
Option 3: Don't use FOR to find the files you need, but use the methods of the System.IO namespace, e.g. Directory.EnumerateFiles, instead.
Escape your internal quote marks like this.
Shell("FOR /R %I in (*.pdf) DO #pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
As I recall, in VB.Net you escape double quote marks by doubling them.
EDIT:
It might help if you do the iteration outside of the Shell. (Certainly to debug)
Dim sourceFolder As String = "c:\Your call"
Dim sourceFiles As String[] = Directory.GetFiles(sourceFolder, "*.pdf")
ForEach file As String In sourceFiles
Dim justName As String = Path.GetFileNameWithoutExtension(file)
Dim shellCall As String = _
String.Format("pdf2swf.exe ""{0}"" -o ""{1}.swf"" -f -T 9 -t -G", _
file, justName)
Shell(shellCall)
EndFor
You could also cosider using System.Diagnostics.Process instead of Shell
Try escaping the quotes (2 quote marks - "" - in VB.NET, IIRC):
Shell("FOR /R %I in (*.pdf) DO #pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")
If pdf2swf.exe is not in the same folder your program's executable is running from, that could be a reason you're getting the error.
Also, you'll have the same issue with the *.pdf files if they are in a different folder other than where your executable is. You can specify the drive and path to search:
Shell ("FOR /R C:\SomeFolder %I in (*.pdf) DO #pdf2swf.exe ""%~fI"" -o ""%~dpI%~nI.swf"" -f -T 9 -t -G ")

How to check if a particular command line is running in cmd propmt

I desperately need help to create a vb / dos code which will do the following:
Check if a command prompt window is running with the following command: mgms A1 (mgms is a custom command)
If it is running, exit.
If it is not running, start cmd prompt and run the command , exit
Thanks a lot for your help!
The Windows cmd.exe batch language is horrible, but you should be able to put this in a batch file and get it working:
tasklist /FI "IMAGENAME eq mgms.exe" 2>&1 | findstr /B "INFO: No tasks running" > tmp
for /F "delims=" %x in (tmp) do mgms A1
You may need to further check that the command-line arguments to mgms.exe match what you expect -- have a look at the help for tasklist.exe and findstr.exe. Both programs are both standard in WinXP Pro and up, I believe. If you don't have them, I'm sure you can find them or (near) equivalents on the web.

DOS command to execute all SQL script in a directory and subdirectories

I need a DOS command or a batch (.bat) file I can execute to run all the *.sql scripts in a directory and its subdirectories. What would the solution be?
The following will get you started
for /r %f in (*.sql) do echo %f
Run from the command line that will print the names of all the SQL files in the current directory and all sub directories.
Then substitute sqlcmd <connection args> -i%f for echo %f to execute the scripts.
Hope this helps.
Here you go. This batch file will execute all sql files in a directory and its subdirectories. It will also create an output.txt file with the results so you can see errors and whatnot. Some notes on batch file:
[YourDatabase] is the name of the database you want to execute the scripts against.
[YourPath] is the path of where you keep all the scripts.
[YourServerName\YourInstanceName] is the SQL server name and instance name, separated with a '\'
You'll want to replace the text after the '=' for each variable with whatever is appropriate for your server
Be sure NOT to put spaces around the '='
Do not put any quotes around [YourPath]
Make sure that [YourPath] has a '\' at the end
SET Database=[YourDatabase]
SET ScriptsPath=[YourPath]
SET ServerInstance=[YourServerName\YourInstanceName]
IF EXIST "%ScriptsPath%output.txt" del "%ScriptsPath%output.txt"
type NUL > "%ScriptsPath%output.txt"
FOR /R "%ScriptsPath%" %%G IN (*.sql) DO (
sqlcmd -d %Database% -S %ServerInstance% -i "%%G" -o "%%G.txt"
echo ..................................................................................... >> "%ScriptsPath%output.txt"
echo Executing: "%%G" >> "%ScriptsPath%output.txt"
echo ..................................................................................... >> "%ScriptsPath%output.txt"
copy "%ScriptsPath%output.txt"+"%%G.txt" "%ScriptsPath%output.txt"
del "%%G.txt"
)
for %f in ("c:\path\to\dir\*.sql") do sqlcmd -S [SERVER_NAME] -d [DATABASE_NAME] -i "%f" -b
Try a for loop. The options of this command have evolved and I'm not sure what version of DOS you are using, but assuming that DOS includes "cmd.exe from Windows XP", something like this could work:
for /r . %f in (*.sql) do #echo %f
Ok, this will only print the names of the files. I'm assuming you already have a program that you can run from the command line that will execute one SQL file, which you can use instead of echo.
For more information, try for /?.