Execute remote powershell script from local system - vb.net

I am new to this powershell..Execuse me if anything wrong here.
Now coming straight to the question I have a powershell script stored in one of the drive say "D:\myfile.ps1" which is in remote system.
Now I need to execute this script from my local system and this is the way I have done
ExecuteCommand(Directory.GetCurrentDirectory & "\Psexec \\myservername -i -u username -p password powershell.exe Invoke-Command -ComputerName \\myservername -FilePath D:\myscript.ps1")
The result is showing me this way without any output giving me an error
"powershell exited on myservername with error code 1'
And If run this script to run .exe on the same server it's working fine
ExecuteCommand(Directory.GetCurrentDirectory & "\psexec \\myservername -u username -p password D:\myfile.exe")
So what's the correct way to execute my script to run on remote desktop ?
Update after suggestions:

Can you just try :
ExecuteCommand(Directory.GetCurrentDirectory & "\Psexec \\myservername -i -u username -p password powershell.exe -command "&{Invoke-Command -ComputerName \\myservername -FilePath D:\myscript.ps1}"

Related

Using Plink to run a command

I am trying to create a script to login to a remote server and run a command. The script is as follows
plink.exe UserName#ServerIP -pw PASSWORD -batch -m PATHTOFILEWITHSCRIPT.txt
PATHTOFILEWITHSCRIPT.txt has this line
debug software restart process user-id
The script when run logs into the server but does not run the command.
Any ideas?
The format that is required is
echo COMMAND | plink.exe USER#SERVER -pwd PASSWORD -batch
where COMMAND is the command that you want to run after you log in

SQL BCP within Powershell hangs

I am new to BCP. I am tying to launch bcp.exe utility from Powershell but it hangs. Same command works fine command prompt. I am using Invoke-Expression to launch bcp.exe.
I am able to launch SQLCMD.exe without any problems.
This is my powershell.
Set-Location -Path "C:\Program Files\Microsoft SQL Server\110\Tools\Binn"
SQLCMD.EXE -b -E -S CORPSYSSQLDEV -d CORPSYSDM -Q "select top 10 * from t_test"
$psCommand = "bcp.exe ""testDB.dbo.t_test"" in ""C:\temp\test\testFile20180919.txt"" -c -t""\t"" -T -S ""TESTSQLDEV"" -e c:\temp\NoahFolder\error.csv"
Write-Host $psCommand
Invoke-Expression $psCommand
This is the result of $psCommand.
bcp.exe "testDB.dbo.t_test" in "C:\temp\test\testFile20180919.txt" -c -t"\t" -T -S "TESTSQLDEV" -e c:\temp\test\error.csv
Which works fine from command prompt but when I run the powershell script its stuck.
I am able to launch SQLCMD.exe from powershell fine.
What am I doing wrong? Any ideas or pointers.
I should have searched more before asking here. This post helped me. I was able to run BCP from powershell using Start-Process command.
$bcp = 'c:\Program Files\Microsoft SQL Server\110\Tools\Binn\bcp.exe'
$arglist = #(
#add all arguments.
)
Start-Process -FilePath $bcp -ArgumentList $arglist

Run .sql file from batch file

I'm new to batch files and would like to execute sql script from the batch file. Please can someone assist.Thanks in advance.
You can use sqlcmd.exe from the DOS prompt. If you run sqlcmd -? from the DOS prompt the program will tell you the command line parameters. Here's an example
sqlcmd -S MyDbServer -d DatabaseName -E -i "MyScript.sql"
The -E tells sqlcmd to use "trusted authentication" meaning your Windows login. If you're not using Windows Authentication then you'll want to use the -U and -P parameters.

Unable to properly call sqlcmd from powershell

I had this sqlcmd snippet working in batch but when I converted to powershell, it broke. This is what I am attempting to call using powershell
Sqlcmd -S localhost\instance -d database -U username -P password -i "sqlQuery.sql" -s "," > \sqlOutput.csv -I -W -k
I tried invoke-sqlcmd but i get the error "The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program"
In regards to the comment below, I have loaded the snapins required. This did help but I am now getting this error. "Invoke-sqlcmd : Parameter cannot be processed because the parameter name 's' is ambiguous.
If I put it in single quotes...
invoke-Sqlcmd '-S localhost\instance -d database -U username -P password -i "sqlQuery.sql" -s "," > \sqlOutput.csv -I -W -k'
I get the error "A network-related or instance-specific error occurred while establishing a connectiong to sql server. Which doesn't make sense to me as the same credentials i am using here (on the same box) work when called from batch.
You have to first call sqlps.exe from powershell or command prompt (assuming you have the SQL Server snap-ins for Powershell installed).
Once you do that, execute get-help Invoke-Sqlcmd to find your way around.
To your question, its not -S, but -ServerInstance
PS SQLSERVER:\> Invoke-Sqlcmd -ServerInstance "srvrName" -Database "master" -Username "user123" -Password "x$34fth"
-InputFile "C:\Users\testUser\Desktop\test.sql" | Out-File "C:\Users\testUser\Desktop\out.txt"
You can also use the MSDN documentation
Goto
Windows Explorer 
Right Click Computer 
Properties 
Advanced System Settings 
Environment Variables
Make sure PSModulePath is set with following string.
C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules

Psexec , cmd and batch file

I have a batch file named a.bat on a winserver2008 Desktop.
That batch file only write the SessionID (from environment variable) to a local eventlog.
I want to execute it remotely using cmd (otherwise the SessionName doesn't appear).
so I have tried
c:\PsTools\psexec.exe \\<Server> -u test2 -p <Password> -i 2 cmd "c:\Users\test-2\Desktop\a"
or
c:\PsTools\psexec.exe \\<server> -u test2 -p <Password> -i 2 "cmd \"c:\Users\test-2\Desktop\a\"";exit
all of these just open a terminal on the remote machine but don't execute the batch.
Any ides?
Best Regards,
Use a /c on the command line after cmd.
So, your first line would look like:
c:\PsTools\psexec.exe \\<Server> -u test2 -p <Password> -i 2 cmd /c "c:\Users\test-2\Desktop\a"
psexec \\<server> -s cmd.exe & whatever.bat
If you're trying to run a batch remotely then when cmd is open on the remote through your terminal connection, you have two options:
xcopy \\your_computer\filepath c:\wherever something.bat
run \\computername\c$\wherever_it_is_located