Error when running powrshell at prompt with TaskScheduler info - 'is not recognized as the name of a cmdlet, function, script file, or operable ..' - powershell-4.0

I'm debugging why my script isn't running right in the TaskScheduler by running it at the command prompt in powershell ISE on the server like it's set up in TaskScheduler:
PS Microsoft.PowerShell.Core\FileSystem::\\w5server\ksupport\C_Adjust> c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -noninteractive -ExecutionPolicy bypass -Command "& .\\w5server\ksupport\C_Adjust\RunScan_MoveFiles_Admin.ps1"
I get this error:
c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe : & : The term '.\\w5server\ksupport\C_Adjust\RunScan_MoveFil
At line:1 char:1
+ c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -noninterac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (& : The term '....Scan_MoveFil:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
es_Admin.ps1' 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:3
+ &
.\\w5server\ksupport\C_Adjust\RunScan_MoveFiles_Admin.ps1
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (.\\w5server\k...Files_Admin.p
s1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I'm not sure why it's splitting the full command line info like that in the error message either; hopefully it's not part of the problem.
I tried removing the '&' before the .\w5server...scriptname.ps1 but got the same error message. I've never given a full path with this type of path before, but that's where the script is. Last time I did this it was in c:\Scripts\B... on that server.
This server is Windows Server 2012 R2. Previously, my server I was working on was 2008 R2, I'm not sure if that matters or not. Otherwise my syntax is the same, other than the script name and full path.
I saw this link is not recognized as the name of a cmdlet, but I think it's spelled correctly.

I was playing around with what I was using to run it at the command line, and this is working:
PS Microsoft.PowerShell.Core\FileSystem::\\w5server\ksupport\C_Adjust> c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -noninteractive -ExecutionPolicy bypass -Command \\w5server\ksupport\C_Adjust\RunScan_MoveFiles_Admin.ps1
So I got rid of the double quotes, '.' and '&' for the script location.

Related

I keep getting access to the path is denied when running a script on powershell

I know this question probably has been answered before.
I am running Powershell as admin. I am trying to output a command to a .txt file
Script: get-alias > C:\Users\xxx\Documents\Personal\Shell_Scripting\get_alias.txt
See error below: out-file : Could not find a part of the path 'C:\Users\xxx\Documents\Personal\Shell_Scripting\get_alias.txt'. At line:1 char:1
get-alias > C:\Users\xxx\Documents\Personal\Shell_Scripting\get_ali ...
CategoryInfo : OpenError: (:) [Out-File], DirectoryNotFoundException
FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

Start-Process : Parameter set cannot be resolved using the specified named parameters (run as admin another .ps1)

Good morning guys,
I am quite new with programming in PowerShell..
I have a problem since this morning and I don't understand why.. I am working on a script for my company, and it works perfectly. But the problem is executing it. The script ist named Script.ps1 and I run it through another one who run another PS window with admin proviledges.
$path = 'Script.ps1'
Start-Process powershell.exe -verb RunAs -ArgumentList "-file $path"
Since Friday was working perfectly, but this morning this command gives the following error.
Start-Process : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Start-Process powershell.exe -verb RunAs -ArgumentList "-file $path" -NoNewWindo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand
(In the error is written also a -NoNewWindow, I used it for understanding which error came out, or the new process disappears in a second)
I tried all.. Do someone can understand what's wrong?
Thanks all!

start telnet from powershell

After installed telnet server on win7, using powershell I try to start it:
sc config tlntsvr start= demand
(this because telnet server is disabled) and then:
sc config tlntsvr start= auto
I've this error:
Set-Content : A positional parameter cannot be found that accepts argument 'start='.
At line:1 char:3
+ sc <<<< config TlntSvr start= demand
+ CategoryInfo : InvalidArgument: (:) [Set-Content], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand
Use Windows command shell for this command, because PowerShell interprets sc as the Set-Content cmdlet, while it is not in cmd.

Powershell ConvertTo-SecureString ObjectNotFound

After upgrading to powershell 3.0 existing scripts stopped working with an error
ConvertTo-SecureString : The term 'ConvertTo-SecureString' 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
+ ConvertTo-SecureString
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ConvertTo-SecureString:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I found that ConvertTo-SecureString is supported on PS 3.0. Do I need to include it somehow?
The following does NOT work.
C:\contoso>powershell -command {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured;}
'ConvertTo-SecureString' is not recognized as an internal or external command,
operable program or batch file.
C:\contoso>
The following does work.
C:\contoso>copy con: tfile1.ps1
$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;
$secured;
^Z
1 file(s) copied.
C:\contoso>powershell -file tfile1.ps1
System.Security.SecureString
C:\contoso>
This also works.
C:\contoso>powershell "& {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured}"
System.Security.SecureString
C:\contoso>
I'll leave why it doesn't work as a -command to someone else as I'm only a powershell novice.
S.
Import-Module 'Microsoft.PowerShell.Security'
fixes the issue. I don't know why this module is not loaded by default.

Executing a simple powershell command on the command line

I try to run a simple powershell command by setting a variable and printing it.
This is what I want to do:
powershell -command "& {$name=\"hi\"; echo $name}"
But it fails with:
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
The invoke operator (&) works fine with commands such as:
powershell -command "& {&echo hi}"
I read about the invoking operator and how to execute commands with -command option and executing scripts with -File option etc. They are working as expected. But my attempts to do the same for setting a variable and printing it as above doesn't work. I suspect -command works with only commands. Any idea how to achieve what I do above?
from a DOS shell this works:
powershell -command "& {$name='hi'; echo $name}"
but also your code works.
From a Powershell console use this:
powershell -command {$name='hi'; echo $name}

Resources