rjani1
rjani1

Reputation: 17

Passing a script with Invoke-Command

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?

Upvotes: 1

Views: 620

Answers (1)

Aman Sharma
Aman Sharma

Reputation: 1990

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>
}

Upvotes: 1

Related Questions