Here is the script:
$compnames = import-csv "$env:userprofile\Documents\list.csv"
$User = "username"
$Password = "password"
foreach ($computer in $compnames) {
cmdkey /generic:TERMSRV/$($computer.compname) /user:$User /pass:$Password
mstsc /v:$($computer.compname)
}
When I run the script, I get the following error:
cmdkey : The term 'cmdkey' 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 C:\Program Files\WindowsPowerShell\Modules\rdpsession\1.0.2\RdpSession.psm1:92 char:1
+ cmdkey /generic:Termsrv/$computer /user:$Username /pass:$rdp > $null
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (cmdkey:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
mstsc : The term 'mstsc' 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 C:\Program Files\WindowsPowerShell\Modules\rdpsession\1.0.2\RdpSession.psm1:94 char:1
+ mstsc /v:$computer
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (mstsc:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
It seems like powershell doesn't recognize cmdkey and mstsc. I've tried googling for hours with no solution. Hoping some one knows what the issue is here preventing the script from running.
Thanks!
Make sure that the actual executables are in your path.
e.g.
dir "$($env:systemroot)\system32\cmdkey.exe"
dir "$($env:systemroot)\system32\mstsc.exe"
The errors are specif.
cmdkey : The term 'cmdkey' is not recognized as the name of a cmdlet
mstsc : The term 'mstsc' is not recognized as the name of a cmdlet
Those two items are external executables of the Windows and ran via cmd.exe, not PowerShell. Running executables/external commands in PowerShell is a well-documented thing, directly from Microsoft.
PowerShell only runs, '.ps' files. Executables are executed via cmd.exe, even from a PowerShell script, which will call cmd.exe under the covers to run them.
If you want to see your call stack of your command, that is why the Trace-Command cmdlet exists.
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Trace-Command).Parameters
(Get-Command -Name Trace-Command).Parameters.Keys
Get-help -Name Trace-Command -Examples
Get-help -Name Trace-Command -Full
Get-help -Name Trace-Command -Online
PowerShell: Running Executables
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
As is PowerShell command precedence.
about_Command_Precedence
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7
1 - Alias
2 - Function
3 - Cmdlet
4 - External executable files (programs and non-PowerShell scripts)
With your use case, the call operator is most prudent.
The Call Operator &
Why: Used to treat a string as a SINGLE command. Useful for dealing with spaces.
In PowerShell V2.0, if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.
The PowerShell V3.0 parser does it now smarter, in this case, you don’t need the & anymore. >
Details: Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters
Example:
& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen
Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths!
With spaces, you have to nest Quotation marks and the result is not always clear!
In this case it is better to separate everything like so:
$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'
& $CMD $arg1 $arg2 $arg3 $arg4
or same like that:
$AllArgs = #('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs
If the executable is not in your System path, or $env:PSModulePath, then you must provide the full path to the executable, as noted by the answer provided by 'Dan'.
Related
I'm executing remotely some scripts to get information from a server, using Plink tool from putty. The trouble comes when I use a .ps1 file, because one '?' appears on the beginning, making the first line incorrect, but with .bat files works as desired.
For example, I want to print the content of a file:
GetDate.bat:
type C:/Data/DateOfCompilation.txt
And then:
PS C:/Users/MyUser> plink -ssh <User>#<IP> -i C:\Key.ppk -m C:\Scripts\GetDate.bat
10/09/2018 14:32:02,72
Everything okay
GetDate.ps1:
Get-Content -Path C:/Data/DateOfCompilation.txt
Execution:
PS C:/Users/MyUser> plink -ssh <User>#<IP> -i C:\Key.ppk -m C:\Scripts\GetDate.ps1
?Get-Content : The term '?Get-Content' 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:1 char:1
+ ?Get-Content -Path C:/Data/DateOfCompilation.txt
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (?Get-Content:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Also, if I add more code, the other lines work fine, it's just the first one which fails with that '?' added at the beginning.
(However, running locally the script works fine)
I have other ps1 scripts much more extended, so using only bat files is not the best option.
I have looked at the documentation, other forums and here, but I'm not able to find anything. Maybe I do not know anything about ps1 files.
Check if there's UTF-8 BOM at the beginning of GetDate.ps1 - If there is, remove it.
Though the root cause your problem may be your misunderstanding of that -m switch of Plink does. It makes Plink read the file and send its contents (and only the contents) to the server. The server never learns, what the file extension is. So it makes no sense to use .ps vs .bat. No matter, what the extension is, the file will be interpreted by the default shell of your Windows SSH server. What is PowerShell (according to the error message).
So even your type command in .bat file was executed by PowerShell, not by cmd.exe. In PowerShell, type is an alias to Get-Content.
The reason why your .bat file works is most probably that it has no BOM, while your .ps1 has BOM. Had you executed your .ps1 with PowerShell, it would handle the BOM correctly. But you are not executing .ps1 with PowerShell, you are executing its contents, and in that case the BOM probably causes problems.
The difference is basically like between:
powershell.exe -File GetDate.ps1
and
powershell.exe < GetDate.ps1
Both do basically the same, but the latter fails with BOM, while the first handles it correctly.
This is the exact error on the cmd window.
this is the command I typed to be executed in the cmd
F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows>install.bat
CNTK Binary Install Script
F:\Fast : The term 'F:\Fast' 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:1 char:1
+ F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows\ps\inst ...
+ ~~~~~~~
+ CategoryInfo : ObjectNotFound: (F:\Fast:String) [], CommandNotF
oundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error during install operation
I've tried running as admin, direct clicking, changing the path etc. Kindly tell me a way to run this file. It's a batch file for a series of installations for the Microsoft cognitive tool kit.
Looks like you need to enclose the path in quotes
"F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows\install.bat"
use "start" before the filename.
Example:
F:\Fast R-CNN\Cognitive tool kit\cntk\Scripts\install\windows>start install.bat
I'm attempting to write a batch file that makes it easier for me to make runas utility shortcuts on a computers desktop.
I have this mostly done, I have all the variables generated, I'm just having issues with the actual shortcut creation part of the script.
This is the code I am using to create the shortcut. With this code, I am using for variables: %shortcutName% as Internet Explorer, %computername% is my computers name, which doesn't include any special characters or spaces, %user% is a local user account which is an administrator (Again no special characters or spaces), and %UserInputpath% is equal to "C:\Program Files\Internet Explorer\iexplore.exe" (When you drag and drop a file into the command prompt window it generates this link, and wraps it in quotes if needed)
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%USERPROFILE%\Desktop\%shortcutName%.lnk');$s.TargetPath='runas /user:%computername%\%user% /savecred %UserInputPath%';$s.Save()"
I think that my problem stems from the quotes as I said earlier, but I'm not really sure how to handle the issue.
This is the error that I get:
Value does not fall within the expected range.
At line:1 char:98
+ ... lorer.lnk');$s.TargetPath='runas /user:iamgroot\admin /savecred C ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
Unable to save shortcut "C:\Users\user\Desktop\Internet Explorer.lnk".
At line:1 char:203
+ ... /savecred C:\Program Files\Internet Explorer\iexplore.exe';$s.Save()
+ ~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], DirectoryNotFoundException
+ FullyQualifiedErrorId : System.IO.DirectoryNotFoundException
My suggestion is you wrap your script with this...
#Run as Adminstrator
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
%Your Coder HERE%
}
This little section of code resolved all my not administrator errors.
Let me know how it works out for you,
The UnderDog
Powershell suddenly quit opening from both cmd and powershell prompts. I haven't installed anything new between when it did work and when it quit working.
When I try to start powershell.exe from a cmd window (both elevated and not elevated) with the following command
C:\Users\myuser>powershell.exe
I get a popup error from the OS that says:
This app can't run on your PC
Once I close that popup the cmd prompt I made the call from then prints:
Access is denied
To the screen (yes even when I do this in an elevated cmd prompt)
When I try to do it in powershell with the following command:
PS C:\Users\myuser> powershell.exe
I get:
Program 'powershell.exe' failed to run: The specified executable is not a valid application for this OS platform.
At line:1 char:1
+ powershell.exe
+ ~~~~~~~~~~~~~~.
At line:1 char:1
+ powershell.exe
+ ~~~~~~~~~~~~~~.
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorID : NativeCommandFailed
Apparently even powershell doesn't like powershell anymore.
I've tried restarting the computer and that didn't fix it, but I'm totally stymied as to what to do next.
#PetSerAl gave the crucial pointer in comments on the question.
The "This app can't run on your PC" pop-up error message on Windows 8 or above
indicates:
a corrupted file, such as a 0-byte *.exe file, esp. when followed by an "Access denied" error in the console.
or, increasingly less commonly, an attempt to run a 64-bit executable on a 32-bit edition of Windows.
Troubleshooting steps:
From a Command Prompt (cmd.exe console), run where.exe <executable-name>;
from PowerShell, run Get-Command -All <executable-name>, which shows you all executables by that name present in the directories listed in the $env:PATH environment variable in that order, by their full paths.
Note that where.exe, unlike Get-Command, also looks in the current directory, and looks there first.
Thus, the first path returned is the executable that is actually executed when only the executable name is specified.
Note that a match in the current directory, if found by where.exe, only matters when calling the executable from cmd.exe (from the Command Prompt or a batch file), because PowerShell by design doesn't allow invocation of executables from the current directory by mere name.
If you want to run where.exe from PowerShell, extension .exe is required, because the command name where by itself is a built-in alias for the Where-Object cmdlet.
In the output from where.exe / Get-Command, check:
if the executable you expect is listed first.
if its size is non-zero.
Remove unexpected (zero-byte) executables, or, if you expect them to be there as functioning executables, reinstall them.
Example:
Look for all all executables named powershell.exe in the current directory and in the directories listed in $env:PATH.
Note that the proper home of powershell.exe is C:\Windows\System32\WindowsPowerShell\v1.0, as reflected in $PSHOME.
From cmd.exe (regular Command Prompt):
where powershell.exe
Example output:
C:\Windows\System32\powershell.exe
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
From PowerShell:
Get-Command -All powershell.exe
If you also want to look in the current directory, use
Get-Command -All .\powershell.exe, powershell.exe
Example output:
CommandType Name Version Source
----------- ---- ------- ------
Application powershell.exe 0.0.0.0 C:\WINDOWS\system32\powershell.exe
Application powershell.exe 10.0.14... C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
If you want to include the file size in the output:
PS> where.exe powershell.exe | % { [system.io.fileinfo] $_ |
select fullname, length, #{ n = 'Version'; e = { $_.versioninfo.FileversionRaw } } }
FullName Length Version
-------- ------ -------
C:\Windows\System32\powershell.exe 0
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 446976 10.0.14393.206
Delete the powershell.exe (with 0KB) from location C:\Windows\System32
In my case Powershell working fine after deleting the powershell.exe (with 0KB) from system 32
The term 'jmeter' 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:1 char:1
jmeter -n -t D:\apache-jmeter-2.13\apache-jmeter-
2.13\bin\zzz\zzz ...
~~~~~~
CategoryInfo : ObjectNotFound: (jmeter:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
Use 'cmd' instead of the Powershell console. Also may sure that you have Jmeter(folder installation)\bin in your PATH environment variable.
If you are on Windows 10 and using Windows PowerShell make sure you are inside JMeter's bin folder and start your command with .\jmeter instead of only jmeter.
.\jmeter -n -t D:\apache-jmeter-2.13\apache-jmeter- 2.13\bin\zzz\zzz ...