PowerShell run .ps1 with parameters remotely - powershell

How do I run a .ps1 script with parameters remotely? Currently I have come across
Invoke-Command -Session $Session -FilePath "./testdelete.ps1 -location $Folder"
I have tried $using:Folder it does not work. It does not seem to pass the folder info to the script.

The Invoke-Command cmdlet has a -ArgumentList parameter:
Enter the values in a comma-separated list. Values are associated with
variables in the order that they are listed.
Example:
Invoke-Command -Session $Session -FilePath "./testdelete.ps1" -ArgumentList $Folder

Related

Trigger external remote script

I have a powershell script located on a different server, and I want to trigger this script from the build server(TFS 2018) using a network path.
//MyServer/MyScripts/run.ps1
In the Powershell build step I need to specify script path under TFS source path. Is there a way to trigger the external script?
This is what I've tried without success....
Invoke-Command -ComputerName MyServer -ScriptBlock {Get-Process -Name //MyServer/MyScripts/run.ps1} -ArgumentList 'arg1', 'arg2'
Something like this:
Invoke-Command -Session $session -scriptblock {Pushd $rootFolder
.\$scriptname.ps1}
Or you could Invoke-Command -FilePath
Depends on where you want to run it.

How do I display the output of a remote exe locally when using Invoke-Command in Powershell?

I have the following script block:
$scriptBlock = {Start-Process ping.exe -ArgumentList localhost -Wait -NoNewWindow -PassThru}
Note: I am using a process as I want to be able to set the working directory the exe is executed in.
If I invoke it locally like this:
Invoke-Command -ScriptBlock $scriptBlock
I get the full ping output displayed. But when I invoke it remotely like this:
Invoke-Command -ComputerName RemoteComputerName -ScriptBlock $scriptBlock
I don't see any of the ping output. How can I get the remote output to display locally?
Drop the Start-Process and invoke the command directly:
$scriptBlock = {ping.exe localhost}
or via the call operator:
$scriptBlock = {& ping.exe localhost}
If you need to run the command from a particular directory, simply change to that directory before running the command:
$scriptBlock = {
Set-Location 'C:\some\folder'
& ping.exe localhost
}

How to use invoke-command in powershell, to run a script on remote machine

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

PowerShell function with parameters? [duplicate]

This question already has an answer here:
Parameter interpretation when running jobs
(1 answer)
Closed 6 years ago.
I have a PoSH script that I can't figure out is not running..
function Connect-AD
{
Param($mod,$cmd)
Write-Host "$mod $cmd"
Write-Host "`tConnecting to AD: $DC`n"
$ADSession = New-PSsession -ComputerName $DC -Credential $MyCredential
Invoke-Command -Command {Import-Module ('$mod') -Cmdlet ('$cmd')} -Session $ADSession
Import-PSSession -Session $ADSession -Module ('$mod') -Prefix r | Out-Null
}
I then try to call this with..
Connect-AD -mod 'ActiveDirectory' -cmd 'Get-ADUser,New-ADUser'
But no mater what I do I keep getting..
The specified module '$mod' was not loaded because no valid module file was found in any module directory.
The Write-Host inside the function outputs the parameters correctly, so it is getting that far. However it is not being passed into the Invoke-Command or Import-PSSession?
I've tried different ways to escape the parameters, etc.. but no luck.
What am I not doing correctly? Anyone able to help me out? Thanks.
Single quoted strings don't interpolate variables, '$mod' is a literal string "dollar m o d".
And you probably need to read all the similar questions on passing parameters to Invoke-Command, because the command {} is running on another computer - how will it know what the variable $mod is on your computer?
Passing string $variable to invoke-command scriptblock parameter -name
Powershell: How to pass parameter with invoke-command and -filepath remotely?
Something like
Invoke-Command -Command {param($mod, $cmd) Import-Module $mod -Cmdlet $cmd} -Session $ADSession -ArgumentList $mod,$cmd
Help Links (if available):
Invoke-Command
Import-Module

PowerShell Invoke-Command on an advanced function

I have an advanced function Copy-FilesHC that is available in a module file. This function copies some files from the Source to the Destination folder and generates some output for in a log file.
The function works fine locally:
Copy-FilesHC -Source $Src -Destination $Des *>> $Log
It also works on a remote machine:
# For remote use we need to make it available first
Import-Module (Get-Command Copy-FilesHC).ModuleName
Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList $LocalSrc, $LocalDes
However, I can't seem to figure out how I can have it pass the output to a log file like in the first command. When I try the following, it fails:
Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC *>> $Log} -ArgumentList $LocalSrc, $LocalDes
Invoke-Command : Cannot validate argument on parameter 'ScriptBlock'. The argument is null. Provide a vali
d value for the argument, and then try running the command again.
As indicated here I thought the $ sign for the ScriptBlock was incorrect. But this way I don't need to put my advanced function in a ScriptBlock to copy it over as it now happens automatically while it's only available within the module. So I just need to find out how to capture the output in the log file.
Thank you for your help.
Found the solution just a few minutes ago:
# For remote use we need to make it available first
Import-Module (Get-Command Copy-FilesHC).ModuleName
Invoke-Command -Credential $Cred -ComputerName $Host -ScriptBlock ${Function:Copy-FilesHC} -ArgumentList $LocalSrc, $LocalDes *>> $Log