Execute a file remotely with powershell - powershell

I am trying to run a batch file remotely using Invoke-Command
PS C:\Users\ale> Invoke-Command -ComputerName SERVER -ScriptBlock { Invoke-Expression -Command:"cmd.exe /c 'C:\Test\Extract.bat'" }
The batch file is supposed to output 3 files in C:\Test\ and is running fine locally.
When I execute the command above, here's the output I receive in powershell
NotSpecified: (:String) [], RemoteException
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : SERVER
1_03102021.csv
2_03102021.csv
3_03102021.csv
As I check there was no file in the remote directory. Any advice please?

To call a bat file in powershell where the path has no spaces you can call it directly
PS C:\> c:\temp\bat.bat
If the path/name contains spaces, quote the path and use the command invoke operator &
PS C:\> & "c:\temp\bat with spaces.bat"
So for your command it would be just
Invoke-Command -ComputerName SERVER -ScriptBlock { C:\Test\Extract.bat }

Related

Invoke-Command is giving NativeCommandError

I am using Invoke-Command { & "powershell.exe" } -NoNewScope and getting error as belove.
powershell.exe : Loading personal and system profiles took 1761ms.
At line:1 char:18
+ Invoke-Command { & "powershell.exe" } -NoNewScope
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Loading persona...es took 1761ms.:String
) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Error Image
This is...
Invoke-Command { & "powershell.exe" } -NoNewScope
... is for the console host based on your use case.
If you are in the ISE,
... you must use New PowerShell Tab option, to get a new session and your profile will load there.
You can use the shortcut key of course. CRTL+T
Differences between the ISE and PowerShell console - PowerShell Team
Console Application (Non) Support in the ISE
$psUnsupportedConsoleApplications
# Results
<#
wmic
wmic.exe
cmd
cmd.exe
diskpart
diskpart.exe
edit.com
netsh
netsh.exe
nslookup
nslookup.exe
powershell
powershell.exe
ssh-keygen
ssh-keygen.exe
#>
PowerShell- Running Executables - TechNet Articles - United States (English) - TechNet Wiki

get NativeCommandError when running powershell script

I'm looking some help. I have batch file on remote PC like this (Path on remote PC = C:\temp\silentsophos.bat) :
\\svr-sophos\SophosUpdate\CIDs\S000\SAVSCFXP\Setup.exe -mng yes -updp \\svr-sophos\SophosUpdate\CIDs\S000\SAVSCFXP -ouser <USERNAME> -opwd <PASSWD> -s -ni
and I tryin to execute that with powershell, which is the script is :
invoke-command -computername PC01 -scriptblock {(&C:\windows\system32\cmd.exe /c C:\temp\silentsophos.bat)}
and the result i get is :
Access is denied.
+ CategoryInfo : NotSpecified: (Access is denied.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : PC01
already tried with "2> $null", the error message is not showing up but on the remote PC seems nothing being executed or started.
Am I doing it wrong?

How to run PowerShell Invoke-Command from Command Prompt when the script has a switch?

I have the need to run PowerShell scripts using the command prompt (calling powershell.exe with the -c parameter). I have run these for years but have never tried it with Invoke-Command in order to do remoting and when the script I want to execute has a switch I have to pass.
This piece of code works great from PowerShell, a simple script that has the LogRun switch:
icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}
However, when I run it via the Command Prompt with powershell.exe fails:
powershell -c "icm -cn MYCOMPUTER02 {C:\Temp\Write-to-File.ps1 -LogRun $Using:LogRun}"
I have tried many variations of this and nothing works, I am sure I missing some syntax detail or some quotes somewhere.
Please let me know if you can help me figure this out.
This is the error I get:
icm : The value of the using variable '$using:LogRun' cannot be retrieved
because it has not been set in the local session.
At line:1 char:1
+ icm -cn MYCOMPUTER02{D:\PatV2DU\PatV2DUService20180411120032\Sc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Command], RuntimeException
+ FullyQualifiedErrorId : UsingVariableIsUndefined,Microsoft.PowerShell.Commands.InvokeCommandCommand

Invoke-Command error when I try to run a script on a remote computer

I am trying to run a script which is located on my local computer on a remote computer and I have to problems:
first: it is trying to run the script on my local computer and I don't want it to do that
second: the script doesn't run on the remote computer
This is the command I run and the error message in question:
PS C:\Users\administrator> Invoke-Command -ComputerName UKBTH05TSV08 -FilePath "C:\TaskSchedulerEventViewerSetup.ps1"
The "ICSweep Script" source is already registered on the "localhost" computer.
+ CategoryInfo : InvalidOperation: (:) [New-EventLog], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.NewEventLogCommand
ERROR: The system cannot find the file specified.
+ CategoryInfo : NotSpecified: (ERROR: The syst...file specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
And this is the content of the C:\TaskSchedulerEventViewerSetup.ps1 script:
New-EventLog –LogName Application –Source “ICSweep Script” schtasks
/Create /XML "ICSweepscripttaskscheduler.xml" /TN ICSweepscript /RU
SYSTEM
this file ICSweepscripttaskscheduler.xml should exist on the remote computer.
also Please specify a path such as :
c:\temp\xmlfile
so that schtasks dosent look in the windows folder for that file.
The evntlog source is probably also registered on the remote computer

Powershell: Running a .msc applet as another user

I'm currently writing a powershell script that asks for a single set of admin credentials, and uses those to run relevant applications, pulled from a network-hosted CSV. When I try to run
Start-Process $tools[$userInput-1].path.toString() -credential $credential
(where $tools is returning "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc") I get the error below
Start-Process : This command cannot be executed because the input "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc" is an Invalid Application. Give a valid application and Run your command again.
At line:1 char:14
+ Start-Process <<<< "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc" -credential
Get-Credential
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
If I need to, I'll just write a .bat file and run that, but I'd rather avoid that whenever possible.
And the reason I'm not using Invoke-Item is because it can't take -Credential, even if the man file says otherwise.
.msc is a saved console file, the host of which is mmc, so to start this from powershell you could use syntax similar to the following:
$mmcPath = "C:\Windows\System32\mmc.exe"
$mscPath = "C:\Program Files\Microsoft\Exchange Server\V14\Bin\Exchange Management Console.msc"
Start-Process -FilePath $mmcPath -ArgumentList $mscPath