Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner Assistanmce

Status
Not open for further replies.

BlackWOlf23

Vendor
Aug 21, 2012
1
AU
Hi Guys,

I've begun learning powershell and have encountered a problem that I'm trying to solve.


Basically I'm working on developing a script that will find the most recent backup file and then run a check on the integrity of this file using a proprietary command that's run from the cmd prompt.The problem I'm having is when I run the code below I encounter the following error.

cmd.exe : ' C : \ P r o g r a m ' i s n o t r e c o g n i z e d a s a n i n t e r n a l o r e x t e r n a l c o m m a n d ,
At C:\Users\user\Desktop\Scripts\VerifyBackup.ps1:27 char:16
+ CMD.exe <<<< /U /C $cmddir v $Md5Name > C:\Temp\Result.txt
+ CategoryInfo : NotSpecified: (' C : \ P r o g... o m m a n d , :String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
o p e r a b l e p r o g r a m o r b a t c h f i l e .


Code:
$CmdDir = "C:\Program Files (x86)\StorageCraft\ShadowProtect\image.exe"

$File_Location = Get-ChildItem E: | Where {$_.extension -eq '.md5'}  | Sort-object {$_.LastWriteTime} -Descending | Select-object -First 1 Fullname 

CMD.exe  /U /C $cmddir v $File_Location > C:\Temp\Result.txt

I believe that the problem is that I'm passing variables that contains values to the cmd prompt and these variables need to be surrounded in specific quotes. However,I'm unsure which types of quotes/where to place them.

Any help would be appreciated.

Thanks.
 
I don't see a problem with quotes. For example I tried here a script with several ways how to execute "rexx -v" i.e. print the version number of the program and everything seems to work
Code:
cmd /c C:\"Program Files"\Regina\rexx.exe -v
cmd /c C:\'Program Files'\Regina\rexx.exe -v
&"C:\Program Files\Regina\rexx.exe" -v
&'C:\Program Files\Regina\rexx.exe' -v

$cmd_dir = "C:\Program Files\Regina\rexx.exe"
echo "cmd_dir = '$cmd_dir'"
cmd /c $cmd_dir -v

$cmd_dir = 'C:\Program Files\Regina\rexx.exe'
echo "cmd_dir = '$cmd_dir'"
cmd /c $cmd_dir -v

Output:
Code:
C:\_mikrom\Work\PowerShell>powershell -ExecutionPolicy RemoteSigned .\exec_cmd.p
s1
REXX-Regina_3.5 5.00 31 Dec 2009
REXX-Regina_3.5 5.00 31 Dec 2009
REXX-Regina_3.5 5.00 31 Dec 2009
REXX-Regina_3.5 5.00 31 Dec 2009
cmd_dir = 'C:\Program Files\Regina\rexx.exe'
REXX-Regina_3.5 5.00 31 Dec 2009
cmd_dir = 'C:\Program Files\Regina\rexx.exe'
REXX-Regina_3.5 5.00 31 Dec 2009

IMO the error is in your command line. You try to execute this command:
$cmddir v $File_Location
Is that right, shouldn't it be this?:
$cmddir -v $File_Location

Check if the program C:\Program Files (x86)\StorageCraft\ShadowProtect\image.exe really exists on your machine.
Check if in the variable $File_Location has proper value.

Hovever, I have here Windows XP. Isn't it Windows 7 specific error?
 
Really, there is no reason to use cmd.exe. You can use Start-Process, Invoke-Command, Invoke-Expression or any of those variations to run a command. This allows for seamless passing of variables, errors, and objects.

Do you have your Tek-Tips.com Swag? I've got mine!

Stop by the new Tek-Tips group at LinkedIn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top