I am trying to use PSExec:
.\PsExec.exe \\servername -u $user -p $password /accepteula cmd.exe /c "C:\Program Files\VMware\vCenter Server\bin\service-control.bat --stop --all"
But I receive the following error:
.\PsExec.exe : 'C:\Program' is not recognized as an internal or external command,
At line:1 char:1
+ .\PsExec.exe \\$vCenter -u $user -p $password /accepteula cmd.exe /c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
operable program or batch file.
cmd.exe exited on <Server FQDN> with error code 1.
Issue is with the String formation. You need to wrap that in the single quotes to take the effect , like:
.\PsExec.exe \\servername -u $user -p $password /accepteula cmd.exe /c '"C:\Program Files\VMware\vCenter Server\bin\service-control.bat --stop --all"'
try with
.\PsExec.exe \\servername -u $user -p $password /accepteula cmd.exe /c '"C:\Program Files\VMware\vCenter Server\bin\service-control.bat --stop --all"'
the single quotes around the cmd command should tell the powershell to take the command as a string.
Related
I am using PSEXEC to get registry key value. The below is my code.
PowerShell:
Set-Alias psexec "e:\test\psexec.exe"
psexec \\172.20.118.74 -i -d -s -u xsumrouadm -p Welkom01 /c "E:\test\DeviceHealthRegistry.bat"
Batch file:
#echo off &setlocal
reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0011 /v "IEEE11nmode"
#echo >> \\esessmw2395\c$\test\QueryDeviceHealth-Registry.csv
I am getting error as below:
psexec : At line:3 char:1
+ psexec \\in00121324 e:\test\DeviceHealthRegistry.bat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The system cannot find the path specified.
Connecting to in00121324...
Starting PSEXESVC service on in00121324...
Connecting with PsExec service on in00121324...
Starting e:\test\DeviceHealthRegistry.bat on in00121324...
PsExec could not start e:\test\DeviceHealthRegistry.bat on in00121324:
Computer name modified to IPaddress
psexec : At line:3 char:1
psexec \\172.20.118.74 -i -d -s -u xsumrouadm -p Welkom01 /c "E:\test\DeviceHeal ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
The file exists.
Connecting to 172.20.118.74...
Starting PSEXESVC service on 172.20.118.74...
Connecting with PsExec service on 172.20.118.74...
Copying E:\test\DeviceHealthRegistry.bat to 172.20.118.74...
Error copying E:\test\DeviceHealthRegistry.bat to remote system:
You have two different errors from two different commands there:
The system cannot find the path specified.
This error occurred, because you ran PsExec like this:
psexec \\in00121324 e:\test\DeviceHealthRegistry.bat
Without the option -c the local file E:\test\DeviceHealthRegistry.bat is not copied to the remote host, and there was no remote file E:\test\DeviceHealthRegistry.bat available for execution.
The file exists.
This error occurred when you ran PsExec with the options -c and -d (the command accepts both / and - notation for parameters):
psexec \\172.20.118.74 -i -d -s -u xsumrouadm -p Welkom01 /c "E:\test\DeviceHealthRegistry.bat"
When you instruct PsExec to immediately detach from the remote process (-d) the file copied to the remote host is not removed after execution, so you probably have an artifact from a previous attempt in C:\Windows on the remote host. Remove that file (and the option -d) and the command should run as expected.
psexec \\172.20.118.74 -i -s -u xsumrouadm -p Welkom01 -c "E:\test\DeviceHealthRegistry.bat"
If you know that the file is already present on the remote host, you could also omit copying it again and run the command like this:
psexec \\172.20.118.74 -i -d -s -u xsumrouadm -p Welkom01 DeviceHealthRegistry.bat
I am experimenting with the following script in Powershell ISE, but this returns an error when executed.
$computerName = Read-Host "Enter name of remote computer"
psexec \\"$computerName" cmd
The Read-Host part works fine, but when it moves to the psexec line it returns
Enter name of remote computer: Computer
psexec :
At line:2 char:1
+ psexec \\"$computerName" cmd
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
So it seems that the script is not passing the value of $computer. I have tried various " ' combinations to no avail.
Any help would be much appreciated, I'm rather novice at powershell scripting.
To expand on some of the other answers, a method a coworker showed me for more complex psexec strings is as follows:
#Use Psexec to Allow all remote connections and Enable PSRemoting
$psexec = "psexec -accepteula \\"+$targetIP+" -u "+$localadmin+" -p "+'"'+$str+'"'+' -h powershell.exe "&{"Set-Item wsman:localhost\client\trustedhosts -Value * -Force" ; "Enable-PSRemoting -SkipNetworkProfileCheck -Force"}"'
invoke-command -ScriptBlock {cmd /c $args[0]} -Argumentlist $psexec
While the above code actually calls psexec on the remote machine then delivers two powershell commands, the problem of authentication causes all sorts of crazy errors in PS.
Hope this helps someone or the OP.
updated:
right, I tried this, seems to work:
$computerName = Read-Host "Enter Name of computer"
$cred = Get-Credential
psexec \\$computerName -u $cred.UserName -p $cred.GetNetworkCredential().Password ipconfig 2> $null #hide errors
You don't need to quote anything. PowerShell will quote automatically if needed.
psexec \\$computerName cmd
I try to use PsLogList to import some eventlogs.
What is strage is that ever second time I run it, there is no error-message...
Here is the part where I have problems with:
$psloglist = "C:\Users\e-cedric.esch\Downloads\PSTools\PsLogList.exe"
$log = &$psloglist $($serverr) -u $($this.elogusr) -p $($this.elogpwd) -s -x -t \t $($logname)
Error-Message I get:
At C:\Users\systemCESCH\Desktop\test1.ps1:line:38 char:10
+ $log = &$ <<<< psloglist $($serverr) -u $($this.elogusr) -p $($this.elogpwd) -s -x -t \t $($logname)
PsLoglist v2.71 - local and remote event log viewer
Copyright (C) 2000-2009 Mark Russinovich
Sysinternals - www.sysinternals.com
At C:\Users\systemCESCH\Desktop\test1.ps1:line:38 char:10
+ $log = &$ <<<< psloglist $($serverr) -u $($this.elogusr) -p $($this.elogpwd) -s -x -t \t $($logname)
Error-Message I get when I debug:
Exception calling "getELog" with "2" argument(s): "Program 'PsLogList.exe' failed to execute: The WriteObject and WriteError methods cannot be called after the pipeline has been closed. Please cont
act Microsoft Support Services.
At C:\Users\systemCESCH\Desktop\test1.ps1:38 char:10
+ $log = & <<<< $psloglist $($serverr) -u $($this.elogusr) -p $($this.elogpwd) -s -x -t \t $($logname)."
At C:\Users\systemCESCH\Desktop\test1.ps1:78 char:34
+ write-output $ELogHandler.getELog <<<< ("PWLU0W987","Application")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ScriptMethodRuntimeException
Btw., I'm using PowerGUI Script Editor v. 3.8.0.129
Are you running your command in the ISE? Have you tested it in a console? I've run into issues with the PsTools before when using the ISE and have gotten them to work properly from the powershell console directly.
I just tested this simplified version:
&"C:\Temp\PSTools\PsLogList.exe" -accepteula \\somecomputer -u domain\myusername -p mypassword
It didn't work in the ISE but it worked in the console.
I would suggest trying to simplify your command and test it in the console as well (if you're using the ISE).
try this :
$prog="psloglist"
$a="\\$serverr -u $user -p $password -s -x"
Start-Process $prog $a -noNewWindow
it's worth reading that post How to run an EXE file in PowerShell with parameters with spaces and quotes
Powershell script executes commandline on local machine
$j = "remote_machine"
$comp = "\\"+$j
$command = 'D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powershell.exe c:\share\script.ps1'
Invoke-Expression "& $command"
This works, but it outputs following
PsExec.exe : At line:1 char:1
+ & D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powersh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError PsExec v2.0 - Execute processes remotely Copyright (C) 2001-2013 Mark Russinovich
Sysinternals - www.sysinternals.com Connecting to
remote_machine...Starting PSEXESVC service on
remote_machine...Connecting with PsExec service on
remote_machine...Starting powershell.exe on remote_machine...
powershell.exe exited on remote_machine with error code 0.
How to fix?
After re-reading your question I see that this is the standard PSExec vs PowerShell issue that has been seen and discussed before. This is due to the fact that PSExec outputs its header lines to the stderr (Standard Error) stream for some of its text. The execution works fine, and it does show an exit code of 0 indicating that there is not actually an error.
This issue is only evident in PowerShell ISE, not the standard PowerShell console (unless you redirect StdErr to StdOut with PSExec <command & args> 2>&1 or something similar). To work around this, if you are going to run the script in the ISE, you can use Start-Process's -RedirectStandardError argument, or redirect StdErr through other means.
I have a string in powershell, which contains a native sqlcmd command. The command itself can be executed successfully in cmd.exe. I have difficulty in executing them in powershell. Anyone can help? Thanks.
This is sql.sql
select ##servername
go
select ##servicename
This is the result when I execute the sqlcmd command from cmd.exe
C:\Users\test>sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
--------------------------------------------------
thesimpsons\INSTANCE1
(1 rows affected)
--------------------------------------------------
INSTANCE1
(1 rows affected)
C:\Users\test>
This is the powershell script to call the sqlcmd command.
$sql = #"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"#
Invoke-Command $sql
When I execute this powershell script, I got the following error.
PS C:\TEMP> $sql = #"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"#
Invoke-Command $sql
Invoke-Command : Parameter set cannot be resolved using the specified named parame
ters.
At line:5 char:15
+ Invoke-Command <<<< $sql
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBin
dingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands
.InvokeCommandCommand
To call a Win32 executable you want to use the call operator & like this:
& sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
You could also stop using the external 'SQLCMD.EXE' and use the Invoke-Sqlcmd cmdlet instead:
Invoke-Sqlcmd is a SQL Server cmdlet that runs scripts that contain statements from the languages (Transact-SQL and XQuery) and commands that are supported by the sqlcmd utility
Just open the 'sqlps' utility and run
Invoke-Sqlcmd -InputFile "C:\temp\sql.sql"
Please see Running SQL Server PowerShell
You can also load the SQL Server snap-ins manually in PowerShell before using 'Invoke-Sqlcmd';
for MS SQL Server 2012 you can do that by running
Import-Module SqlPs
This is how I build some externals command in my scripts
$scriptblock = {fullpath\sqlcmd -S `"(local)\instance1`" <# comment option -S #>`
-U a `
-P a `
-i `"c:\temp\sql.sql`" }
Invoke-Command -ScriptBlock $scriptBlock
You can then use $args variable inside it and even start it remotly.
$scriptblock = {fullpath\sqlcmd -S `"(local)\instance1`" <# comment option -S #>`
-U a `
-P a `
-i `"$($args[0])`" }
Invoke-Command -ScriptBlock $scriptBlock -argumentList "c:\temp\sql.sql" -computer "remote1"
Remark :
This allow to comment each param.
Be careful not to forget a "`" and no space after them where they are at the end of the line.
Use Invoke-Expression rather than Invoke-Command
The first positional parameter of invoke-command is -scriptblock, and it expects a script block argument. To take advantage of a here-string to build the command and then run it with invoke-command, you need to convert the here-string to a script block:
$sql = #"
sqlcmd -S "(local)\instance1" -U a -P a -i "c:\temp\sql.sql"
"#
Invoke-Command ([scriptblock]::create($sql))
Both instance name and username should be fully qualified
<domain_name>\Instanc_name and <domai_name>\Username. Only your instance name is correctly scripted.
This is what worked for me for using sqlcmd from within the powershell script using the & operator, see sample to generate a csv file:
& cmd /c "sqlcmd -S $sv -i $PROCFILE -s, -v varDB = $dbclean -o $filename"
$sv has server name like SERVERNAME\INSTANCE
$PROCFILE is like d:\TSTSQL\Sqlquery.SQL
$filename is d:\TSTSQL\Desiredoutfilename.CSV
$dbclean is a parameter passed to the sql file