I need to run a BAT file on some servers
Because of the amount of many servers I avoid copying the file on any server
I have the file on my computer and I try to access it remotely
$CMDCOMMAND = "\\Mycomp\c$\file.bat"
Invoke-Command -ComputerName $Hostname -Credential $Cred -ScriptBlock {
Start-Process cmd.exe "/c $CMDCOMMAND
}
Invoke-Command -Computer $Hostname -Credential $Cred -FilePath $CMDCOMMAND
should work as #Ansgar Wiechers mentioned.
I doubt you have set $CMDCOMMAND = "\\Mycomp\c$\file.bat" and tried. It should be local path when you pass it to -FilePath
$CMDCOMMAND = "c:\file.bat"
Invoke-Command does not work with a variable defined outside the ScriptBlock. You need to use the ArgumentList parameter. Something like this should work, although I have not tested it:
$CMDCOMMAND = "\\Mycomp\c$\file.bat"
Invoke-Command -ComputerName $Hostname -Credential $Cred -ScriptBlock {
param($ARGLIST) Start-Process cmd.exe "/c $ARGLIST"
} -ArgumentList $CMDCOMMAND
Related
I try to execute my simple vbscript on the remote session using PowerShell. The script does not have any error and run successfully, but I notified the code not executed in remote session. is there any way how to solve this?
$session=New-PSSession -ComputerName "192.XXX.XX.XX" -Credential (New-Object System.Management.Automation.PSCredential("UserName",(ConvertTo-SecureString "Password" -AsPlainText -Force)))
$status= Invoke-Command -Session $session -ScriptBlock{
& cscript "C:\Data\test.vbs"
}
Use Start-Process to execute cscript from PowerShell using cscript with the C:\Data\test.vbs parameter as in the answer example below. You can optionally use the -wait parameter of Start-Process in case it helps in your particular case.
PowerShell
$session = New-PSSession -ComputerName "192.XXX.XX.XX" -Credential (New-Object System.Management.Automation.PSCredential("UserName",(ConvertTo-SecureString "Password" -AsPlainText -Force)))
$status = Invoke-Command -Session $session -ScriptBlock {
Start-Process C:\Windows\System32\cscript.exe "C:\Data\test.vbs" -Wait;
};
Supporting Resource
Start-Process
I am trying to write a Powershell script which will deploy software on a collection of WS2016 servers. I am a local administrator on all these servers. Here's what I have so far:
$Cred = Get-Credential
$Computer = 'myserver.contoso.com'
$SplunkMSI = '\\mylocalbox\C$\Splunk.msi'
$InstallDir = 'C:\Apps\Splunk\'
$sb = {
param($installer, $dir)
Start-Process -FilePath 'c:\windows\system32\msiexec.exe' -ArgumentList "$installer INSTALLDIR=$dir AGREETOLICENSE=Yes /qn /norestart /L*v C:\temp\splunkInstall.log" -Wait -NoNewWindow
}
Write-Host "Deploying Splunk to host $Computer"
Invoke-Command -Computer $Computer -Credential $Cred -ScriptBlock $sb -ArgumentList $SplunkMSI, $InstallDir -ErrorAction Stop
When I run this script, I get prompted for credentials, and then I see the output of the Write-Host, but then... nothing. I have to manually terminate the script.
I logged onto the remote host, but see no evidence that the MSI was executed or failed to execute.
Anyone see a smoking gun?
ya, it looks like it's looking for $installer and $dir in the script block but they're not specified
Is it possible to use Invoke-Command in PowerShell to run a script on a remote machine?
I have tried :
Invoke-Command -ComputerName $MyPC -Credential $mycreds -ScriptBlock {
& "C:\Users\MyPC\Desktop\scripts\Script1.ps1"
}
which returns
script1.ps1 is not recognized as the name of a cmdlet
The scenario here is, I have some scripts on a remote folder, and I need to use Invoke-Command or some other ways to run the script on a remote machine.
Also, how to write if I want to pass some parameters for script1.ps1? Thanks in advance!
Instead of this:
Invoke-Command -ComputerName $MyPC -Credential $mycreds -ScriptBlock {& "C:\Users\MyPC\Desktop\scripts\Script1.ps1"}
Try this:
Invoke-Command -ComputerName $MyPC -Credential $mycreds -FilePath C:\Users\MyPC\Desktop\scripts\Script1.ps1
to avoid confusion with filepaths on local machines/remote machines; i always run stuff from smb-shares; you can enter UNC as filepath... eg:
-FilePath \server\Netscripts\script.ps1
Using Powershell, I'm trying to connect to a remote machine and install an exe file on that system. Unfortunately I'm getting an Access is denied error when running the file. What's truly odd about this error, is that other exe's located on the same path run fine, so I'm wondering if something more cryptic could be involved?
I'm currently using this command to connect to the remote machine, upon which I'm a local admin.
$InstallFile = "\\networkshare\folder\folder\setup.exe"
$InstallParameters = "SampleParameter1 = 5"
$Server = SERVERNAME.DOMAINNAME.COM
$cred = Get-Credential
invoke-command -Computername $Server -authentication credssp -credential $cred -ScriptBlock {
$CurrentProcess = Start-Process -FilePath $InstallFile -ArgumentList $InstallParameters -Wait -PassThru
$CurrentProcess | Wait-Process
}
I'm using CredSSP which seems to be working well since it fixed this issue for other files, but this one simply refuses. Any other thoughts? I ran into a similar issue with .NET 4 and was unable to resolve that install either.
I've noticed you are using Start-Process to run your executable. According to this:
http://technet.microsoft.com/en-us/library/dd347667.aspx
there's also -Credential parameter that can be passed to it. It very well might be the case that Start-Process is not executed by the user that called Invoke-Command. Try passing Credential to your Start-Process:
invoke-command -Computername $Server -authentication credssp -credential $cred -ScriptBlock {
$CurrentProcess = Start-Process -FilePath $InstallFile - Credential $cred -ArgumentList $InstallParameters -Wait -PassThru
$CurrentProcess | Wait-Process }
When I run the lines below from a script the file ends up being created on my local machine.
$cred = Get-Credential domain\DanTest
Enter-PSSession -computerName xsappb01 -credential $cred
New-Item -type file c:\temp\blahxsappk02.txt
exit-pssession
When I run each line individually from the powershell console the remote session is created correctly and the file is created on the remote machine. Any thoughts on why? Is it a timing issue is the script perhaps?
Not sure if it is a timing issue. I suspect it's more like Enter-PSSession is invoking something like a nested prompt and your subsequent commands are not executing within it. Anyway, I believe Enter/Exit-PSSession is meant for interactive use - not scripting use. For scripts use New-PSSession and pass that session instance into Invoke-Command e.g.:
$cred = Get-Credential domain\DanTest
$s = New-PSSession -computerName xsappb01 -credential $cred
Invoke-Command -Session $s -Scriptblock {New-Item -type file c:\temp\blah.txt}
Remove-PSSession $s