Passing a script with Invoke-Command - powershell

I have a script which uses a set of cmdlets which are on a Windows 2012 Domain Controller. I am using a Windows 7 machine which does not have these (Get-DHCPServerV4Lease for example).
At first I created a script module and applied it like this:
Invoke-Command -Computername <Server> -scriptblock {Get-ComputerStatus}
I then get an error message stating that the cmdlet is not recognised. Then I just converted it into a straight-forward PS1 script:
icm -cn <server> -FilePath .\ComputerInfo.ps1 -ArgumentList "Computer"
After running this I get an error saying that the various cmdlets I am using in the script are not recognised.
Get-DnsServerResourceRecord : The term 'Get-DnsServerResourceRecord'
is not recognized as the name of a cmdlet,......
How can I run my script against a DC using Invoke-Command?

Adding to Frode F's comment, you need to first install the corresponding module on the target machine, which you specify as <server> in your Invoke-Command cmdlet. Then within your script, you need to import that module before you can execute cmdlets which are available via that module.
Invoke-Command -Computername <Server> -scriptblock
{
Import-Module <moduleName> -ErrorAction SilentlyContinue
Get-ComputerStatus
#<Any othe cmdlets from the module>
}

Related

how to obtain process module on remote computers using powershell, Get-Process -Module gives it only for local machine

how to obtain process module on remote computers using powershell, Get-Process -Module gives it only for local machine. basically i want to obtain absolute paths of .dll files that are being loaded/used by processes running on that remote server/computer.
Running invoke-Command should work.
C:\> Invoke-Command -ScriptBlock {Get-Process -Module} -ComputerName comp1
You are not able to pass the -ComputerName switch to Get-Process unfortunately.
C:\> Get-Process -Module -ComputerName comp1
Get-Process : Exception getting "Modules" or "FileVersion": "This feature is not
supported for remote computers.".

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.

Manage IIS Remotely through TFS2015

Currently using TFS 2015 update 3 for deployments and I have added "PowerShell on target machine" task, which calls for PowerShell script saved on IIS server to stop website before deployments:
icm -ComputerName $server -ScriptBlock {Import-Module WebAdministration; Stop-Website -Name $app}
with session variables as: $server = abc.xyz.com, $app = DefaultWebSite
The error I get is:
The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot validate argument on parameter 'Name'. The argument is null. Provide a valid value for the argument, and then try running the command again.”
The same script works if I hard code the server & application name.
With a scriptblock, you can't use the variables from your script scope unless you use param with an argument list or with PowerShell 3+ use the using: scope modifier.
icm -ComputerName $server -ScriptBlock {Import-Module WebAdministration; Stop-Website -Name $using:app}

PowerShell - Execute PowerShell Script on the C: drive of a remote server

PowerShell Version 5 build 10586
I am using the following code to remote connect to a server from my local PC
$cimSession = New-CimSession -ComputerName "SERVERNAME.DOMAIN.COM"
Invoke-Command -ComputerName SERVERNAME -ScriptBlock {Get-ChildItem “C:\Temp\ps”}
Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1"
Remove-CimSession -CimSession $cimSession
The first command is able to run successfully and sees the PowerShell file on the remote server.
The second command fails with an error:
Invoke-Command : Cannot find path '\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1' because it does not exist.
Is there another way to call/run a PowerShell script on the C Drive of a remote server?
I have granted my account full access to the specified file.
I have also tried to share the specified folder and given myself Read/Write access to the folder.
I have changed the file path to the share path and get the same result.
Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\ps\PS_SCRIPT_FILE.ps1"
Try this:
Invoke-Command -ComputerName SERVERNAME -ScriptBlock { Invoke-Command -FilePath "C:\Temp\ps\PS_SCRIPT_FILE.ps1" }
In your existing code the -FilePath parameter is processed on the calling machine. By including that as a parameter within the ScriptBlock it should be processed on the target machine.

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