I my trying to pass a variable server name to use it with psexec, so I have a variable $hostname and i'd like to do
psexec \\$hostname
When I do this I am getting an error. What is the correct way to do this?
You need to give psexec something to run, e.g. cmd or powershell.
Are you setting the $hostname variable? if not then you have to use $env:COMPUTERNAME as Powershell is different to the standard CMD.
This works for me at an elevated prompt:
$myhost = 'localhost'
.\psexec.exe \\$myhost cmd /c dir
I have run into a similar issue passing $ variables into psexec cmds. You can use this in your case:
$hostname = Read-Host 'Enter host name' | psexec \\\\\$_ cmd
or for a list (and Powershell 7 - drop the parallel switch for 5.1 or 6)
$hostname = Get-Content "...list.txt" | Foreach-Object -Parallel { psexec \\\\$_ cmd }
Related
I want to run a PowerShell command inside itself, after opened the PowerShell console. For example as we create desktop shortcut by DOS command line by CMD. Similarly opened the PowerShell command line console, another PowerShell cmdlet would execute inside that.
Ex:
PS C:> Test-Connection -ComputerName 8.8.8.8 -Count 1000
I want to run a PowerShell command inside itself, after opened the PowerShell console. For example as we create desktop shortcut by DOS command line by CMD. Similarly opened the PowerShell command line console, another PowerShell cmdlet would execute inside that.
Ex:
C:> PowerShell && Test-Connection -ComputerName 8.8.8.8 -Count 1000
PS C:> Test-Connection -ComputerName 8.8.8.8 -Count 1000
powershell.exe, the Windows PowerShell CLI, allows you to:
pass a command to execute via the (positionally implied) -Command parameter.
keep the resulting session open with -NoExit.
Therefore:
PowerShell -NoExit -Command "Test-Connection -ComputerName 8.8.8.8 -Count 1000"
Execute the remote server parametrized batch file from PowerShell.
Doesn't throw an error nor executed command on remote machine.
$path = "D:\run\test-5.2.bat";
Invoke-Command -ComputerName testserver -Scriptblock { "$path" }
Script inside the bat file is msiexec with parameters, which shall execute through Command Prompt only.
Based on this msdn link, you can run a ps1 script file on remote computers. So if it is possible to "port" the content of the bat file in a ps1 it should work. Here is the msdn example:
Example 11: Run a script on all the computers listed in a text file
PS C:\> Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList Process, Service
This example uses the Invoke-Command cmdlet to run the Sample.ps1 script on all of the computers listed in the Servers.txt file. The command uses the FilePath parameter to specify the script file. This command lets you run the script on the remote computers, even if the script file is not accessible to the remote computers.
When you submit the command, the content of the Sample.ps1 file is copied into a script block and the script block is run on each of the remote computers. This procedure is equivalent to using the ScriptBlock parameter to submit the contents of the script.
Hope that helps
$path is a string. PowerShell simply echoes bare strings instead of executing them, unlike CMD or bash. Use the call operator (&):
& "$path"
or Start-Process:
Start-Process cmd.exe -ArgumentList '/c', $path -NoNewWindow -Wait
to have PowerShell execute a string as a command. Since you say you're running msiexec.exe from the batch script using the latter may be required.
On top of that you have a scope issue. The variable $path inside the scriptblock is not the same as the one in the global scope. You can mitigate that via the using: scope qualifier:
Invoke-Command -Computer testserver -Scriptblock { & "$using:path" }
or by passing $path as an argument to the scriptblock:
Invoke-Command -Computer testserver -Scriptblock { & "$($args[0])" } -ArgumentList $path
I found the Invoke-Command cmdlet in PowerShell that supposedly is invoking a command on another server but this doesn't quite work. Instead what I get is the print of what the bat/exe. My exe is a console app and the bat was a test I did that launches the exe with start "" "myexe.exe".
This is the command I'm trying to do:
Invoke-Command -ComputerName 10.10.10.10 -ScriptBlock {
'C:\Program Files\program.exe'
}
or
Invoke-Command -ComputerName 10.10.10.10 -ScriptBlock {
'C:\Program Files\batch.bat'
}
In both cases instead of the command getting invoked on the other server I get the print on the server I call from.
Did I miss an argument somewhere? I want to launch the exe/bat on the remote server, not on the server I'm on.
EDIT
I made it work with this:
$command = "PathtoExe.exe"
$process = [WMICLASS]"\\10.10.10.10\ROOT\CIMV2:win32_process"
$result = $process.Create($command)
But now the exe is not displayed, it's like running in the background.
Your problem is how you're trying to call the executable. In PowerShell, everything is an object. What you're doing is printing the String to the console instead of executing. To invoke the string, you need to use the call operator: &
Invoke-Command -ComputerName 10.10.10.10 -ScriptBlock {
& 'C:\Program Files\batch.bat'
}
This will cause it to use the default program for that extension.
I had the same pain trying to run "netsh.exe" on a remote host. Eventually got it working with the following code:
Invoke-Command -ComputerName 10.10.10.10 -ScriptBlock {Invoke-Expression "C:\Program Files\program.exe"}
New-SshSession -ComputerName cagent01
$A= Invoke-SshCommand -InvokeOnAll -Quiet -Command '~/.bash_profile'
$B= Invoke-SshCommand -InvokeOnAll -Quiet -Command '~/.bashrc'
$Result = Invoke-SshCommand -InvokeOnAll -Quiet -Command './start'
$Ora = Invoke-SshCommand -InvokeOnAll -Quiet -Command 'echo $ORACLE_HOME'
write-host "Oracle home is $Ora"
Remove-SshSession -RemoveAll
i use PowerShell v3. This script isn't sourcing my .profile.
I don't see $Ora printing any value.
When I manually log into the server to do echo $PATH, it includes the ORACLE path
echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/data/app/oracle/product/11.2.0/dbhome_1/bin:/home/HEALTHLANGUAGE/scmbuild/bin:/home/db2inst1/sqllib/bin:/home/db2inst1/sqllib/adm:/home/db2inst1/sqllib/misc
The .bash_profile does export the PATH variable.
export PATH
When I run a bamboo plan on this agent, it says ORACLE_HOME is not set. Manually loggin into the server sources the path correctly, but i want to do it using the script.
Can someone please help me with a work around? Or is it not possible to do this using PowerShell for Linux servers?
I sourced the /etc/profile in my .bashrc and incorporated that change to the new snapshot. It works like a charm now. Thank you so much for all the valuable inputs
I am looking as the title says to kill a process (for example name.exe) on multiple remote machines. I can do it individually using pskill or taskkill using (for example):
pskill -t \ -u -p name.exe
but this becomes impractical with 50+ machines.
Is there a way to make it read a text file of IP Addresses like psexec does using the #C:\name.txt or in powershell or something similar?
All devices are on the same domain.
Thank you in advance for your help.
If you have a text file with a list of machines you could do it trivially with:
get-content serverlist.txt | Foreach-object {& pskill -t \\$_ -u -p name.exe}
There are many methods to do this in Powershell like WMI, Invoke-command etc.
Here is an example to do it using WMI:
$cred = get-credential
It will pop-up for Credential. This way you can make sure that credential is not visible in the script.
(Get-Content 'c:\Temp\Computers.txt') | ForEach-Object {
Get-WmiObject -computer $_ -class win32_process -filter "name = 'name.exe'" -credential $cred| %{$_.terminate()} | out-null
}