get NativeCommandError when running powershell script - powershell

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?

Related

Execute a file remotely with 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 }

Powershell posh-ssh and cisco

I am having some issues getting command to work with posh-ssh and Cisco gear.
Commands are pretty simple;
New-SSHSession -ComputerName 10.10.101.240 -Port 22
Will connect fine, and will give me an ID.
I then try
Invoke-SSHCommand -SessionId 0 -Command "show version"
but I always get
Exception calling "EndExecute" with "1" argument(s): "An established connection was aborted by the server."
At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\2.0.2\Posh-SSH.psm1:266 char:25
+ $Output = $_.cmd.EndExecute($_.Async)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SshConnectionException
I have tried different hosts and different PCs.
I can, however, use $sshsession.write() and $sshsession.read() and I can talk to the switch fine.
Can anyone point out what I am doing wrong here?

Azure PowerShell will not connect

I have downloaded and installed the Azure Powershell by following the MS instructions:
https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/
Using the Windows Powershell ISE I run the command:
Login-AzureRmAccount
which returns the the error:
Login-AzureRmAccount : The 'Login-AzureRmAccount' command was found in the module 'AzureRM.Profile', but the module could not be loaded. For more information, run 'Import-Module AzureRM.Profile'. At line:1 char:1
+ Login-AzureRmAccount
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Login-AzureRmAccount:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
Import-Module AzureRM.Profile
Returns this error:
Import-Module : File C:\Program Files (x86)\Microsoft
SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager\AzureRM.Profile\CheckVersions.ps1 cannot be loaded
because running scripts is disabled on this system. For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ Import-Module AzureRM.Profile
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
Why is it so difficult to connect to Azure, what could be wrong?
From the command prompt run:
powershell Set-ExecutionPolicy RemoteSigned

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

Execute batch file on remote computer

I am on host_computer and attempting to execute batch file on remote_computer (part of WORKGROUP) to enable psremoting and use winrm to add trustedhosts.
Based on http://social.technet.microsoft.com/Forums/scriptcenter/en-US/732495c0-114c-4a1e-bbe1-5a7a3b84012d/execute-a-batch-file-on-a-remote-pc?forum=ITCG
I tried creating a process, i.e.
$server = "remote_computer"
$process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process"
But I get error
Cannot convert value "\\remote_computer\ROOT\CIMV2:win32_process" to type "System.Management.ManagementClass". Error: "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" At line:1 char:1
+ $process = [WMICLASS]"\\$server\ROOT\CIMV2:win32_process"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastToWMIClass
Then I tried using psexec
PsExec.exe \\remote_computer -u username -p password 'C:\share\enable.bat'
And get error
D:\pstools\PsExec.exe : At line:1 char:1
+ D:\pstools\PsExec.exe \\remote_computer -u username -p password 'C:\share\ena ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError PsExec v2.0 - Execute processes remotely Copyright (C) 2001-2013 Mark Russinovich Sysinternals - www.sysinternals.com The handle is invalid. Connecting to remote_computer...Couldn't access remote_computer: Connecting to remote_computer...
And this batch script is to enable psremoting and add trusted hosts using winrm
#echo off
C:\share\remote.ps1
winrm set winrm/config/client/auth #{Basic="true"}
winrm set winrm/config/client #{AllowUnencrypted="true"}
winrm set winrm/config/client #{TrustedHosts="host_computer"}